Sha256: 5f4b16edca006d2cdf52b8949c4e45d4e42ceffa719647022b40775a9d77b0c0
Contents?: true
Size: 1.84 KB
Versions: 13
Compression:
Stored size: 1.84 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 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 private def option self.class.instance_variable_get(:@option) end end end
Version data entries
13 entries across 13 versions & 1 rubygems