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

name age message
file .gitignore Sun Dec 27 17:23:08 -0800 2009 Ignore yardoc poop. [therealadam]
file LICENSE Tue Mar 10 21:03:38 -0700 2009 Tweak copy. [therealadam]
file README.md Tue Oct 27 18:59:40 -0700 2009 Update README too. [therealadam]
file Rakefile Fri Jan 01 19:27:20 -0800 2010 Move test setup bits into test helper. [therealadam]
file VERSION.yml Tue Dec 22 17:41:11 -0800 2009 Version bump to 1.0.0 [therealadam]
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/ Fri Jan 01 19:44:02 -0800 2010 More docs. [therealadam]
directory test/ Fri Jan 01 19:39:14 -0800 2010 Define predicates for each mapped attribute value. [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
  include AttributeMapper

  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).