summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgregory <greg2502@gmail.com>2014-06-19 07:38:58 -0400
committerdblock <dblock@dblock.org>2014-06-19 07:38:58 -0400
commit3ab15f24beba02b20f2edb69ddc861c86d3abe3c (patch)
tree07c3fa97f92fc7903650f7ae58be5e8c0c53e84a
parent11445218ecaa3bd0a1cb05f8bc50f697f46e3f0b (diff)
downloadhashie-3ab15f24beba02b20f2edb69ddc861c86d3abe3c.tar.gz
Fixed from and transform_with Trash features when IndifferentAccess is included, related to #167.
-rw-r--r--CHANGELOG.md3
-rw-r--r--lib/hashie/extensions/dash/indifferent_access.rb15
-rw-r--r--lib/hashie/trash.rb18
-rw-r--r--spec/hashie/extensions/dash/indifferent_access_spec.rb16
4 files changed, 48 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b8b0e8d..49fccf4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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).
diff --git a/lib/hashie/extensions/dash/indifferent_access.rb b/lib/hashie/extensions/dash/indifferent_access.rb
index aafaa39..d0d9b99 100644
--- a/lib/hashie/extensions/dash/indifferent_access.rb
+++ b/lib/hashie/extensions/dash/indifferent_access.rb
@@ -14,6 +14,21 @@ module Hashie
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
diff --git a/lib/hashie/trash.rb b/lib/hashie/trash.rb
index ec60d5c..fbad3a6 100644
--- a/lib/hashie/trash.rb
+++ b/lib/hashie/trash.rb
@@ -42,15 +42,27 @@ module Hashie
# 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
diff --git a/spec/hashie/extensions/dash/indifferent_access_spec.rb b/spec/hashie/extensions/dash/indifferent_access_spec.rb
index f88a280..2342b95 100644
--- a/spec/hashie/extensions/dash/indifferent_access_spec.rb
+++ b/spec/hashie/extensions/dash/indifferent_access_spec.rb
@@ -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) }