therealadam / attribute_mapper

Provides a transparent interface for mapping symbolic representations to a column in the database of a more primitive type.

This URL has Read+Write access

commit  e03cfb1ebf4c79f914360e5784515282ba9967bd
tree    186c53f9447e2d474d531fa03d42e7ecba10d292
parent  471018d2a450f8717f28e3f56ffccd90fa332d17
name age message
file .gitignore Tue Mar 10 20:56:51 -0700 2009 Rcov and ignores. [therealadam]
file LICENSE Tue Mar 10 21:03:38 -0700 2009 Tweak copy. [therealadam]
file README.md Loading commit data...
file Rakefile Tue Mar 10 21:07:17 -0700 2009 Be gone, vile CamelCase gem name! [therealadam]
file VERSION.yml
file attribute_mapper.gemspec Tue Mar 10 21:07:17 -0700 2009 Be gone, vile CamelCase gem name! [therealadam]
file init.rb Tue Oct 27 18:55:45 -0700 2009 Switch to explicitly including AttributeMapper ... [therealadam]
directory lib/ Tue Oct 27 18:55:45 -0700 2009 Switch to explicitly including AttributeMapper ... [therealadam]
directory test/ Tue Oct 27 18:55:45 -0700 2009 Switch to explicitly including AttributeMapper ... [therealadam]
README.md

AttributeMapper

AttributeMapper provides a transparent interface for mapping symbolic representations to a column in the database of a more primitive type. For example, rather than hardcoding values like 1 or 2 to represent that a Ticket model's status column is "open" or "closed" you would create the following mapping:

class Ticket < ActiveRecord::Base
  map_attribute :status, :to => {:open => 1, :closed => 2}
end

You can now get and set the status column symbolically:

ticket.status = :open
ticket.status # => :open

Internally, the integer 1 will be stored in the database.

An authoritative list of the mapping is available as a class method which is the pluralized version of the attribute:

Ticket.statuses # => {:open => 1, :closed => 2}

The primitive values of the mapping can always be used to assign the column, though the getter for the attribute will always return the higher level symbolic representation.

ticket.status = 1
ticket.status # => :open

Authors

Marcel Molina, Jr., Bruce Williams and Adam Keys

Released under the MIT License (see LICENSE).