Module: ClassyEnum::InstanceMethods
- Included in:
- Base
- 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.
-
#as_json(options = nil) ⇒ Object
Overrides as_json to remove owner reference recursion issues.
-
#index ⇒ Object
(also: #to_i)
Returns an integer representing the order that this element was defined in.
-
#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
79 80 81 |
# File 'lib/classy_enum/instance_methods.rb', line 79 def <=> other index <=> other.index end |
#as_json(options = nil) ⇒ Object
Overrides as_json to remove owner reference recursion issues
84 85 86 87 88 89 90 |
# File 'lib/classy_enum/instance_methods.rb', line 84 def as_json(=nil) return to_s unless serialize_as_json json = super() json.delete('owner') json.delete('serialize_as_json') json 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 self.class.instance_variable_get('@index') 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 62 |
# File 'lib/classy_enum/instance_methods.rb', line 59 def name ActiveSupport::Deprecation.warn('name is deprecated, and will be removed in ClassyEnum 3.0. Replace calls with to_s.titleize.', caller) 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 self.class.instance_variable_get('@option').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 |