diff options
author | Fabio Napoleoni <f.napoleoni@gmail.com> | 2012-04-17 00:49:10 +0200 |
---|---|---|
committer | Fabio Napoleoni <f.napoleoni@gmail.com> | 2012-04-17 00:49:10 +0200 |
commit | f565859abb4ee969d5a15e63b93642b0ab61704c (patch) | |
tree | 36df92a91ec1aac703ec0a8c42b476773dcc1272 /lib/hashie/trash.rb | |
parent | 11f45346c26297ffbcadc8d118a061c07dadff92 (diff) | |
download | hashie-f565859abb4ee969d5a15e63b93642b0ab61704c.tar.gz |
Added :transform_with option
Diffstat (limited to 'lib/hashie/trash.rb')
-rw-r--r-- | lib/hashie/trash.rb | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/hashie/trash.rb b/lib/hashie/trash.rb index 8e93140..39d6e8e 100644 --- a/lib/hashie/trash.rb +++ b/lib/hashie/trash.rb @@ -15,6 +15,8 @@ module Hashie # returned before a value is set on the property in a new Dash. # * <tt>:from</tt> - Specify the original key name that will be write only. # * <tt>:with</tt> - Specify a lambda to be used to convert value. + # * <tt>:transform_with</tt> - Specify a lambda to be used to convert value + # without using the :from option. It transform the property itself. def self.property(property_name, options = {}) super @@ -36,6 +38,8 @@ module Hashie end RUBY end + elsif options[:transform_with].respond_to? :call + transforms[property_name.to_sym] = options[:transform_with] end end @@ -44,6 +48,8 @@ module Hashie def []=(property, value) if self.class.translations.include? property.to_sym send("#{property}=", value) + elsif self.class.transforms.key? property.to_sym + super property, self.class.transforms[property.to_sym].call(value) elsif property_exists? property super end @@ -55,6 +61,10 @@ module Hashie @translations ||= [] end + def self.transforms + @transforms ||= {} + end + # Raises an NoMethodError if the property doesn't exist # def property_exists?(property) |