Giter VIP home page Giter VIP logo

attribute-cartographer's Introduction

= 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

attribute-cartographer's People

Contributors

krishicks avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.