Class: Hashie::Trash
Overview
A Trash is a 'translated' Dash where the keys can be remapped from a source hash.
Trashes are useful when you need to read data from another application, such as a Java api, where the keys are named differently from how we would in Ruby.
Class Attribute Summary collapse
-
.transforms ⇒ Object
readonly
Returns the value of attribute transforms.
-
.translations_hash ⇒ Object
readonly
Returns the value of attribute translations_hash.
Class Method Summary collapse
- .inherited(klass) ⇒ Object
- .permitted_input_keys ⇒ Object
-
.property(property_name, options = {}) ⇒ Object
Defines a property on the Trash.
- .transformation_exists?(name) ⇒ Boolean
- .transformed_property(property_name, value) ⇒ Object
- .translation_exists?(name) ⇒ Boolean
Instance Method Summary collapse
-
#[]=(property, value) ⇒ Object
Set a value on the Dash in a Hash-like way.
Methods inherited from Dash
#[], #initialize, #merge, #merge!, property?, #replace, required?, #update_attributes!
Methods included from Extensions::PrettyInspect
Methods inherited from Hash
Methods included from Extensions::StringifyKeys
#stringify_keys, #stringify_keys!
Methods included from Extensions::StringifyKeys::ClassMethods
#stringify_keys, #stringify_keys!, #stringify_keys_recursively!
Constructor Details
This class inherits a constructor from Hashie::Dash
Class Attribute Details
.transforms ⇒ Object (readonly)
Returns the value of attribute transforms.
45 46 47 |
# File 'lib/hashie/trash.rb', line 45 def transforms @transforms end |
.translations_hash ⇒ Object (readonly)
Returns the value of attribute translations_hash.
45 46 47 |
# File 'lib/hashie/trash.rb', line 45 def translations_hash @translations_hash end |
Class Method Details
.inherited(klass) ⇒ Object
50 51 52 53 54 |
# File 'lib/hashie/trash.rb', line 50 def self.inherited(klass) super klass.instance_variable_set('@transforms', transforms.dup) klass.instance_variable_set('@translations_hash', translations_hash.dup) end |
.permitted_input_keys ⇒ Object
80 81 82 |
# File 'lib/hashie/trash.rb', line 80 def self.permitted_input_keys @permitted_input_keys ||= properties.map { |property| inverse_translations.fetch property, property } end |
.property(property_name, options = {}) ⇒ Object
Defines a property on the Trash. Options are as follows:
- :default - Specify a default value for this property, to be returned before a value is set on the property in a new Dash.
- :from - Specify the original key name that will be write only.
- :with - Specify a lambda to be used to convert value.
- :transform_with - Specify a lambda to be used to convert value without using the :from option. It transform the property itself.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/hashie/trash.rb', line 19 def self.property(property_name, = {}) super [:from] = [:from] if [:from] if [:from] if property_name == [:from] fail ArgumentError, "Property name (#{property_name}) and :from option must not be the same" end translations_hash[[:from]] ||= {} translations_hash[[:from]][property_name] = [:with] || [:transform_with] define_method "#{[:from]}=" do |val| self.class.translations_hash[[:from]].each do |name, with| self[name] = with.respond_to?(:call) ? with.call(val) : val end end else if [:transform_with].respond_to? :call transforms[property_name] = [:transform_with] end end end |
.transformation_exists?(name) ⇒ Boolean
76 77 78 |
# File 'lib/hashie/trash.rb', line 76 def self.transformation_exists?(name) transforms.key? name end |
.transformed_property(property_name, value) ⇒ Object
68 69 70 |
# File 'lib/hashie/trash.rb', line 68 def self.transformed_property(property_name, value) transforms[property_name].call(value) end |
.translation_exists?(name) ⇒ Boolean
72 73 74 |
# File 'lib/hashie/trash.rb', line 72 def self.translation_exists?(name) translations_hash.key? name end |
Instance Method Details
#[]=(property, value) ⇒ Object
Set a value on the Dash in a Hash-like way. Only works on pre-existing properties.
58 59 60 61 62 63 64 65 66 |
# File 'lib/hashie/trash.rb', line 58 def []=(property, value) if self.class.translation_exists? property send("#{property}=", value) elsif self.class.transformation_exists? property super property, self.class.transformed_property(property, value) elsif property_exists? property super end end |