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



39
40
41
# File 'lib/classy_enum/collection.rb', line 39

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
28
29
30
31
32
33
# File 'lib/classy_enum/collection.rb', line 21

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

  if other.is_a? ClassyEnum::Base
    index <=> other.index
  else
    nil
  end
end

#enum_optionsObject



35
36
37
# File 'lib/classy_enum/collection.rb', line 35

def enum_options
  self.class.enum_options
end