map :Something, :other_thing, ->(v) { v.uppercase }
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
.rspec
.rvmrc
Gemfile
LICENSE
README
Rakefile
attribute_cartographer.gemspec

README

= Attribute Cartographer

* http://github.com/krishicks/attribute-cartographer

== DESCRIPTION

Attribute Cartographer allows you to map an attributes hash into similarly or differently named methods, using an optional lambda to map the values as well.

== INSTALL

Add attribute-cartographer to your Gemfile

  gem 'attribute-cartographer'

Then run:

  $ bundle

== USAGE
  class Mapper
    include AttributeCartographer

    # one-way mapping
    map "UnchangedKey"                                                              # map a single key, value untouched
    map %w{ UnchangedKey1 UnchangedKey2 }                                           # same as above, but for multiple in a single statement

    map "SameKeyNewValue", ->(v) { v.downcase }                                     # maps the key, using the lambda for the value
    map %w{ SameKeyNewValue1 SameKeyNewValue2 }, ->(v) { v.upcase }                 # same as above, but for multiple in a single statement

    map "OldKey", "new_key", ->(v) { v.downcase }                                   # maps the left key to the right key, using the lambda for the value
    map "DowncasedKeyAndValue", ->(k,v) { [k.downcase, v.downcase] }                # maps the key and value using the lambda for both
    map %w{ NewKeyAndValue1 NewKeyAndValue2 }, ->(k,v) { [k.downcase, v.downcase] } # same as the example above, but for multiple in a single statement

    # two-way mapping
    map "AnotherOldKey", "another_new_key"                                          # map the left key to the right key and vice-versa, value untouched
    map "ThisKey", "to_this_key", ->(v) { v.downcase }, ->(v) { v.upcase }          # map the left key to the right key with the first lambda,
                                                                                    # and the right key to the left key with the second lambda

  end

  Mapper.new("UnchangedKey" => "UnchangedValue", "OldKey" => "OldValue")            # etc. 

  For each mapping defined, an entry in mapped_attributes will be made using the
  logic defined in the mapping. You also get another method, original_attributes,
  that retains the entire hash that was passed to #initialize, whether the keys
  were mapped or not.

== REQUIREMENTS

* Ruby 1.9.x

== LICENSE

MIT