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 :a
map :b, ->(v) { v.downcase }
map :e, :f, ->(v) { v.downcase }
map :O, ->(k,v) { [k.downcase, v.downcase] }
map [:i, :j, :k]
map [:l, :m, :n], ->(v) { v.upcase }
# two-way mapping
map :c, :d
map :g, :h, ->(v) { v.downcase }, ->(v) { v.upcase }
map [:P, :Q, :R], ->(k,v) { [k.downcase, v.downcase] }
end
Mapper.new(a: 2, b: "STRING")
For each mapping defined, an instance method is created that returns the mapped
value. This can be mapped directly, with a lambda to map the value or key and
value, with an explicit key mapping and lambda value mapping (with an optional
reverse-value map lambda), with a directly-mapped array, a directly-mapped array
with value mapping, or with an array with keys and values mapped with lambdas.
Mapper#original_attributes is the original hash passed in
Mapper#mapped_attributes is a hash representing the mapped keys and their mapped values
REQUIREMENTS
-
Ruby 1.9.x
LICENSE
MIT