README.rdoc in enumerate_it-0.2.0 vs README.rdoc in enumerate_it-0.3.0

- old
+ new

@@ -40,78 +40,91 @@ == Creating enumerations Enumerations are created as models, but you can put then anywhere in your application. In Rails applications, I put them inside models/. - class RelationshipStatus < EnumerateIt::Base - associate_values( - :single => [1, 'Single'], - :married => [2, 'Married'], - :widow => [3, 'Widow'], - :divorced => [4, 'Divorced'], - ) - end + class RelationshipStatus < EnumerateIt::Base + associate_values( + :single => [1, 'Single'], + :married => [2, 'Married'], + :widow => [3, 'Widow'], + :divorced => [4, 'Divorced'], + ) + end This will create some nice stuff: * Each enumeration's value will turn into a constant: - RelationshipsStatus::SINGLE # returns 1 - RelationshipStatus::MARRIED # returns 2 and so on... + RelationshipsStatus::SINGLE # returns 1 + RelationshipStatus::MARRIED # returns 2 and so on... * You can retrieve a list with all the enumeration codes: - RelationshipStatus.list # [1,2,3,4] + RelationshipStatus.list # [1,2,3,4] * You can get an array of options, ready to use with the 'select', 'select_tag', etc family of Rails helpers. - RelationshipStatus.to_a # [["Divorced", 4],["Married", 2],["Single", 1],["Widow", 3]] + RelationshipStatus.to_a # [["Divorced", 4],["Married", 2],["Single", 1],["Widow", 3]] * You can manipulate the hash used to create the enumeration: - RelationshipStatus.enumeration # returns the exact hash used to define the enumeration + RelationshipStatus.enumeration # returns the exact hash used to define the enumeration == Using enumerations The cool part is that you can use these enumerations with any class, be it an ActiveRecord instance or not. - class Person - include EnumerateIt - attr_accessor :relationship_status + class Person + include EnumerateIt + attr_accessor :relationship_status - has_enumeration_for :relationship_status, :with => RelationshipStatus - end + has_enumeration_for :relationship_status, :with => RelationshipStatus + end This will create: * A humanized description for the values of the enumerated attribute: - p = Person.new - p.relationship_status = RelationshipStatus::DIVORCED - p.relationsip_status_humanize # => 'Divorced' + p = Person.new + p.relationship_status = RelationshipStatus::DIVORCED + p.relationsip_status_humanize # => 'Divorced' +* If you don't supply a humanized string to represent an option, EnumerateIt will use a 'humanized' version of the hash's key to humanize the attribute's value: + + class RelationshipStatus < EnumerateIt::Base + associate_values( + :married => 1, + :single => 2 + ) + end + + p = Person.new + p.relationship_status = RelationshipStatus::MARRIED + p.relationship_status_humanize # => 'Married' + * If you pass the :create_helpers option as 'true', it will create a helper method for each enumeration option (this option defaults to false): - class Person < ActiveRecord::Base - has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_helpers => true - end + class Person < ActiveRecord::Base + has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_helpers => true + end - p = Person.new - p.relationship_status = RelationshipStatus::MARRIED - p.married? #=> true - p.divorced? #=> false + p = Person.new + p.relationship_status = RelationshipStatus::MARRIED + p.married? #=> true + p.divorced? #=> false * If your class can manage validations and responds to :validates_inclusion_of, it will create this validation: - class Person < ActiveRecord::Base - has_enumeration_for :relationship_status, :with => RelationshipStatus - end + class Person < ActiveRecord::Base + has_enumeration_for :relationship_status, :with => RelationshipStatus + end - p = Person.new :relationship_status => 6 # => there is no '6' value in the enumeration - p.valid? # => false - p.errors[:relationship_status] # => "is not included in the list" + p = Person.new :relationship_status => 6 # => there is no '6' value in the enumeration + p.valid? # => false + p.errors[:relationship_status] # => "is not included in the list" Remember that in Rails 3 you can add validations to any kind of class and not only to those derived from ActiveRecord::Base. == Installation @@ -123,9 +136,13 @@ * Create an initializer with the following code: ActiveRecord::Base.send :include, EnumerateIt * Add the 'enumerate_it' gem as a dependency in your environment.rb (Rails 2.3.x) or Gemfile (if you're using Bundler) + +== Ruby 1.9 + +EnumerateIt is fully compatible with Ruby 1.9.1 (all tests pass) == Why did you reinvent the wheel? There are other similar solutions to the problem out there, but I could not find one that worked both with strings and integers as the enumerations' codes. I had both situations in