Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for Issues 167 with trash and indifferent access #174

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

**Note:** This version introduces several backward incompatible API changes. See [UPGRADING](UPGRADING.md) for details.

* [#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
Expand Up @@ -6,6 +6,22 @@ class DashWithIndifferentAccess < Hashie::Dash
property :name
end

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

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 'initialized with' do
it 'string' do
instance = DashWithIndifferentAccess.new('name' => 'Name')
Expand Down