Sha256: 116310c8d3cc8a167fe1dee47092ffc2af06732fa39a84bff265e7f625d4de75
Contents?: true
Size: 1.79 KB
Versions: 3
Compression:
Stored size: 1.79 KB
Contents
module ClassyEnum module Conversion # 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 def to_i self.class.instance_variable_get('@index') end alias :index :to_i # 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' def to_s self.class.instance_variable_get('@option').to_s end # 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 def to_sym to_s.to_sym end # Overrides as_json to remove owner reference recursion issues def as_json(options=nil) return to_s unless serialize_as_json json = super(options) json.delete('owner') json.delete('serialize_as_json') json end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
classy_enum-3.1.0 | lib/classy_enum/conversion.rb |
classy_enum-3.0.1 | lib/classy_enum/conversion.rb |
classy_enum-3.0.0 | lib/classy_enum/conversion.rb |