Module: ClassyEnum::InstanceMethods
- Defined in:
- lib/classy_enum/instance_methods.rb
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Sort an array of elements based on the order they are defined.
-
#index ⇒ Object
(also: #to_i)
Returns an integer representing the order that this element was defined in.
-
#is?(obj) ⇒ Boolean
Determine if the enum attribute is a particular member.
-
#name ⇒ Object
Returns string representing enum in Rails titleize format.
-
#to_s ⇒ Object
Returns the name or string corresponding to element.
-
#to_sym ⇒ Object
Returns a Symbol corresponding to a string representation of element, creating the symbol if it did not previously exist.
Instance Method Details
#<=>(other) ⇒ Object
Sort an array of elements based on the order they are defined
Example
# Create an Enum with some elements
class Priority < ClassyEnum::Base
enum_classes :low, :medium, :high
end
@low = Priority.build(:low)
@medium = Priority.build(:medium)
@high = Priority.build(:high)
priorities = [@low, @high, @medium]
priorities.sort # => [@low, @medium, @high]
priorities.max # => @high
priorities.min # => @low
78 79 80 |
# File 'lib/classy_enum/instance_methods.rb', line 78 def <=> other @index <=> other.index end |
#index ⇒ Object Also known as: to_i
Returns an integer representing the order that this element was defined in. Also used internally for sorting.
Example
# Create an Enum with some elements
class Priority < ClassyEnum::Base
enum_classes :low, :medium, :high
end
@priority = PriorityMedium.new
@priority.index # => 2
14 15 16 |
# File 'lib/classy_enum/instance_methods.rb', line 14 def index @index end |
#is?(obj) ⇒ Boolean
Determine if the enum attribute is a particular member. Accepts a symbol or string representing a member
Example
# Create an Enum with some elements
class Breed < ClassyEnum::Base
enum_classes :golden_retriever, :snoop
end
# Create an ActiveRecord class using the Breed enum
class Dog < ActiveRecord::Base
classy_enum_attr :breed
end
@dog = Dog.new(:breed => :snoop)
@dog.breed.is? :snoop # => true
@dog.breed.is? 'snoop' # => true
@dog.breed.is? :golden_retriever # => false
100 101 102 |
# File 'lib/classy_enum/instance_methods.rb', line 100 def is?(obj) obj.to_s == to_s end |
#name ⇒ Object
Returns string representing enum in Rails titleize format
Example
# Create an Enum with some elements
class Priority < ClassyEnum::Base
enum_classes :low, :medium, :high, :really_high
end
@priority = Priority.build(:really_high)
@priority.name # => "Really High"
59 60 61 |
# File 'lib/classy_enum/instance_methods.rb', line 59 def name @to_s.titleize end |
#to_s ⇒ Object
Returns the name or string corresponding to element
Example
# Create an Enum with some elements
class Priority < ClassyEnum::Base
enum_classes :low, :medium, :high
end
@priority = PriorityLow.new
@priority.to_s # => 'low'
30 31 32 |
# File 'lib/classy_enum/instance_methods.rb', line 30 def to_s @to_s end |
#to_sym ⇒ Object
Returns a Symbol corresponding to a string representation of element, creating the symbol if it did not previously exist
Example
# Create an Enum with some elements
class Priority < ClassyEnum::Base
enum_classes :low, :medium, :high
end
@priority = PriorityLow.new
@priority.to_sym # => :low
45 46 47 |
# File 'lib/classy_enum/instance_methods.rb', line 45 def to_sym @to_s.to_sym end |