Created
April 21, 2010 06:54
-
-
Save avit/373517 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Gender | |
include Comparable | |
attr_reader :gender | |
VALID_ENUMS = [:male, :female] | |
def initialize(value) | |
value = value.to_s.downcase.to_sym | |
@gender = value if VALID_ENUMS.include?(value) | |
end | |
def ==(other) | |
gender == other.gender | |
end | |
def <=>(other) | |
VALID_ENUMS.index(gender) <=> VALID_ENUMS.index(other.gender) | |
end | |
def to_s | |
gender.to_s | |
end | |
def male? | |
@gender == :male | |
end | |
def female? | |
@gender == :female | |
end | |
def self.male | |
new :male | |
end | |
def self.female | |
new :female | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person < ActiveRecord::Base | |
composed_of :gender | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment