diff options
author | Michael Herold <michael.j.herold@gmail.com> | 2015-04-29 22:38:42 -0500 |
---|---|---|
committer | Michael Herold <michael.j.herold@gmail.com> | 2015-04-29 22:38:42 -0500 |
commit | 7543dc1f0ea67156afbfc858a2dd031082f5712d (patch) | |
tree | 29e5ce94b4cb101b3a3483e00c00069170f0b551 | |
parent | 717e30cae1427224dc67d83d47a531aed961bdb0 (diff) | |
download | hashie-7543dc1f0ea67156afbfc858a2dd031082f5712d.tar.gz |
Update readme with PropertyTranslation docs
-rw-r--r-- | README.md | 47 |
1 files changed, 46 insertions, 1 deletions
@@ -524,13 +524,58 @@ p = Tricky.new('trick' => 'two') p.trick # => NoMethodError ``` +### Dash Extension: PropertyTranslation + +The `Hashie::Extensions::Dash::PropertyTranslation` mixin extends a Dash with +the ability to remap keys from a source hash. + +### Example from inconsistent APIs + +Property translation is useful when you need to read data from another +application -- such as a Java API -- where the keys are named differently from +Ruby conventions. + +```ruby +class PersonHash < Hashie::Dash + include Hashie::Extensions::Dash::PropertyTranslation + + property :first_name, from: :firstName + property :last_name, from: :lastName + property :first_name, from: :f_name + property :last_name, from: :l_name +end + +person = PersonHash.new(firstName: 'Michael', l_name: 'Bleigh') +person[:first_name] #=> 'Michael' +person[:last_name] #=> 'Bleigh +``` + +### Example using translation lambdas + +You can also use a lambda to translate the value. This is particularly useful +when you want to ensure the type of data you're wrapping. + +```ruby +class DataModelHash < Hashie::Dash + include Hashie::Extensions::Dash::PropertyTranslation + + property :id, transform_with: ->(value) { value.to_i } + property :created_at, from: :created, with: ->(value) { Time.parse(value) } +end + +model = DataModelHash.new(id: '123', created: '2014-04-25 22:35:28') +model.id.class #=> Fixnum +model.created_at.class #=> Time +``` + ### Mash and Rails 4 Strong Parameters To enable compatibility with Rails 4 use the [hashie_rails](http://rubygems.org/gems/hashie_rails) gem. ## Trash -A Trash is a Dash that allows you to translate keys on initialization. It is used like so: +A Trash is a Dash that allows you to translate keys on initialization. It mixes +in the PropertyTranslation mixin by default and is used like so: ```ruby class Person < Hashie::Trash |