Module: ClassyEnum::Conversion
- Included in:
- Base
- Defined in:
- lib/classy_enum/conversion.rb
Instance Method Summary collapse
-
#as_json(options = nil) ⇒ Object
Overrides as_json to remove owner reference recursion issues.
-
#to_i ⇒ Object
(also: #index)
Returns an integer representing the order that this element was defined in.
-
#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
#as_json(options = nil) ⇒ Object
Overrides as_json to remove owner reference recursion issues
60 61 62 |
# File 'lib/classy_enum/conversion.rb', line 60 def as_json(=nil) to_s end |
#to_i ⇒ Object Also known as: index
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
end
class Priority::Low < Priority; end
class Priority::Medium < Priority; end
class Priority::High < Priority; end
@priority = Priority::Medium.new
@priority.to_i # => 2
18 19 20 |
# File 'lib/classy_enum/conversion.rb', line 18 def to_i self.class.instance_variable_get('@index') end |
#to_s ⇒ Object
Returns the name or string corresponding to element
Example
# Create an Enum with some elements
class Priority < ClassyEnum::Base
end
class Priority::Low < Priority; end
class Priority::Medium < Priority; end
class Priority::High < Priority; end
@priority = Priority::Low.new
@priority.to_s # => 'low'
37 38 39 |
# File 'lib/classy_enum/conversion.rb', line 37 def to_s 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
end
class Priority::Low < Priority; end
class Priority::Medium < Priority; end
class Priority::High < Priority; end
@priority = Priority::Low.new
@priority.to_sym # => :low
55 56 57 |
# File 'lib/classy_enum/conversion.rb', line 55 def to_sym to_s.to_sym end |