Skip to content

Commit

Permalink
Fixed from and transform_with Trash features when IndifferentAccess i…
Browse files Browse the repository at this point in the history
…s included, related to #167.
  • Loading branch information
gregory authored and dblock committed Jun 19, 2014
1 parent 1144521 commit 3ab15f2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
**Note:** This version introduces several backward incompatible API changes. See [UPGRADING](UPGRADING.md) for details.

* [#172](https://github.com/intridea/hashie/pull/172): Added Dash and Trash#update_attributes! - [@gregory](https://github.com/gregory).
* [#173](https://github.com/intridea/hashie/pull/173): Auto include Dash::IndifferentAccess when Extensions::IndiferentAccess is included in Dash - [@gregory](https://github.com/gregory).
* [#173](https://github.com/intridea/hashie/pull/173): Auto include Dash::IndifferentAccess when IndiferentAccess is included in Dash - [@gregory](https://github.com/gregory).
* [#174](https://github.com/intridea/hashie/pull/174): Fixed `from` and `transform_with` Trash features when IndifferentAccess is included - [@gregory](https://github.com/gregory).
* [#169](https://github.com/intridea/hashie/pull/169): Hash#to_hash will also convert nested objects that implement to_hash - [@gregory](https://github.com/gregory).
* [#150](https://github.com/intridea/hashie/pull/159): Handle nil intermediate object on deep fetch - [@stephenaument](https://github.com/stephenaument).
* [#146](https://github.com/intridea/hashie/issues/146): Mash#respond_to? inconsistent with #method_missing and does not respond to #permitted? - [@dblock](https://github.com/dblock).
Expand Down
15 changes: 15 additions & 0 deletions lib/hashie/extensions/dash/indifferent_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ def property?(name)
name = name.to_s
!!properties.find { |property| property.to_s == name }
end

def translation_exists?(name)
name = name.to_s
!!translations.keys.find { |key| key.to_s == name }
end

def transformed_property(property_name, value)
transform = transforms[property_name] || transforms[:"#{property_name}"]
transform.call(value)
end

def transformation_exists?(name)
name = name.to_s
!!transforms.keys.find { |key| key.to_s == name }
end
end
end
end
Expand Down
18 changes: 15 additions & 3 deletions lib/hashie/trash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,27 @@ def self.property(property_name, options = {})
# Set a value on the Dash in a Hash-like way. Only works
# on pre-existing properties.
def []=(property, value)
if self.class.translations.key? property
if self.class.translation_exists? property
send("#{property}=", value)
elsif self.class.transforms.key? property
super property, self.class.transforms[property].call(value)
elsif self.class.transformation_exists? property
super property, self.class.transformed_property(property, value)
elsif property_exists? property
super
end
end

def self.transformed_property(property_name, value)
transforms[property_name].call(value)
end

def self.translation_exists?(name)
translations.key? name
end

def self.transformation_exists?(name)
transforms.key? name
end

def self.permitted_input_keys
@permitted_input_keys ||= properties.map { |property| inverse_translations.fetch property, property }
end
Expand Down
16 changes: 16 additions & 0 deletions spec/hashie/extensions/dash/indifferent_access_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
require 'spec_helper'

describe Hashie::Extensions::Dash::IndifferentAccess do
class TrashWithIndifferentAccess < Hashie::Trash
include Hashie::Extensions::Dash::IndifferentAccess
property :per_page, transform_with: lambda { |v| v.to_i }
property :total, from: :total_pages
end

class DashWithIndifferentAccess < Hashie::Dash
include Hashie::Extensions::Dash::IndifferentAccess
property :name
end

context 'when included in Trash' do
let(:params) { { per_page: '1', total_pages: 2 } }
subject { TrashWithIndifferentAccess.new(params) }

it 'gets the expected behaviour' do
expect(subject.per_page).to eq params[:per_page].to_i
expect(subject.total).to eq params[:total_pages]
end
end

context 'when included in Dash' do
let(:patch) { Hashie::Extensions::Dash::IndifferentAccess::ClassMethods }
let(:dash_class) { Class.new(Hashie::Dash) }
Expand Down

0 comments on commit 3ab15f2

Please sign in to comment.