Skip to content

Instantly share code, notes, and snippets.

@cassiomarques
Created November 12, 2010 22:37
Show Gist options
  • Save cassiomarques/674824 to your computer and use it in GitHub Desktop.
Save cassiomarques/674824 to your computer and use it in GitHub Desktop.
New features for EnumerateIt
# Feature1: Change enumeration value based on bang methods (available only
# when passing true to the :create_helpers option)
class RelationshipStatus < EnumerateIt::Base
associate_values(
:single => 1,
:married => 2,
:widow => 3
)
end
class Person
include EnumerateIt
attr_accessor :relationship_status
has_enumeration_for :relationship_status, :create_helpers => true
end
person = Person.new
person.relationship_status = RelationshipStatus::SINGLE
person.relatioship_status # => 1
person.married!
person.relationship_status # => 2
# Feature 2: Allowing object's attribute to hold more than one value
# (useful when more than on option applies, like in forms with multiple
# checkboxes and so on)
class ContactType < EnumerateIt::Base
associate_values(
:email => 1,
:sms => 2,
:phone => 3,
:smoke_signal => 4
)
end
class Person
include EnumerateIt
attr_accessor :contact_type
has_enumeration_for :contact_type, :multiple => true
end
person = Person.new
# creates an alias for contact_type as contact_types (pluralized)
person.contact_types # => []
#can receive both an array or a string separated by commas or spaces
person.contact_types = [ContactType::EMAIL, ContactType::SMS] # or %w(EMAIL SMS) or "1,2" or "1 2", etc
person.contact_types # => [1, 2]
# for ActiveRecord instances, the values would be serialized to the database, probably as a string, but always returned as an array.
# Querying
# This scope would be automatically created and would accept a variable number of arguments, in any order. Gotta think more about that...
Person.with_contact_types(1,2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment