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 Method Summary collapse
-
.property(property_name, options = {}) ⇒ Object
Defines a property on the Trash.
Instance Method Summary collapse
-
#[]=(property, value) ⇒ Object
Set a value on the Dash in a Hash-like way.
Methods inherited from Dash
#[], inherited, #initialize, property?
Methods included from PrettyInspect
Methods inherited from Hash
Methods included from HashExtensions
#hashie_stringify_keys, #hashie_stringify_keys!, included, #to_mash
Constructor Details
This class inherits a constructor from Hashie::Dash
Class Method Details
.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.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/hashie/trash.rb', line 17 def self.property(property_name, = {}) super if [:from] translations << [:from].to_sym class_eval <<-RUBY def #{[:from]}=(val) self[:#{property_name}] = val end RUBY end end |
Instance Method Details
#[]=(property, value) ⇒ Object
Set a value on the Dash in a Hash-like way. Only works on pre-existing properties.
32 33 34 35 36 37 38 |
# File 'lib/hashie/trash.rb', line 32 def []=(property, value) if self.class.translations.include? property.to_sym send("#{property}=", value) elsif property_exists? property super end end |