Module: ClassyEnum::Collection

Included in:
Base
Defined in:
lib/classy_enum/collection.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



29
30
31
# File 'lib/classy_enum/collection.rb', line 29

def self.included(klass)
  klass.extend ClassMethods
end

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
end

class Priority::Low < Priority; end
class Priority::Medium < Priority; end
class Priority::High < Priority; 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


21
22
23
24
25
26
27
# File 'lib/classy_enum/collection.rb', line 21

def <=> other
  if other.is_a?(Symbol) || other.is_a?(String)
    other = self.class.find(other)
  end

  index <=> other.index
end