Class: Renum::EnumeratedValue

Inherits:
Object
  • Object
show all
Extended by:
Enumerable, Forwardable
Includes:
Comparable
Defined in:
lib/renum/enumerated_value.rb

Overview

This is the superclass of your enumeration classes. The class methods defined here are intended to be called on your enumerated value classes. An enumeration class is Enumerable over its values and exposes them by numeric index via []. Values are also comparable, sorting into the order in which they're declared.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (EnumeratedValue) initialize(name)

You should never directly new-up an EnumeratedValue, so this is basically internal. It sets up the value with its class, so if you override, be sure to call super! Better yet define init as shown in the README.



46
47
48
49
50
# File 'lib/renum/enumerated_value.rb', line 46

def initialize name
  @name = name.to_s.freeze
  @index = self.class.values.size
  self.class.values << self
end

Instance Attribute Details

- (Object) index (readonly)

Returns the value of attribute index



41
42
43
# File 'lib/renum/enumerated_value.rb', line 41

def index
  @index
end

- (Object) name (readonly)

Returns the value of attribute name



41
42
43
# File 'lib/renum/enumerated_value.rb', line 41

def name
  @name
end

Class Method Details

+ (Array) values



18
19
20
# File 'lib/renum/enumerated_value.rb', line 18

def values
  @values ||= []
end

+ ({String => EnumeratedType}) values_by_name



30
31
32
33
34
35
# File 'lib/renum/enumerated_value.rb', line 30

def values_by_name
  @values_by_name ||= values.inject({}) do |memo, value|
    memo[value.name] = value
    memo
  end.freeze
end

+ (EnumeratedValue?) with_name(name)

Lookup by name.



25
26
27
# File 'lib/renum/enumerated_value.rb', line 25

def with_name name
  values_by_name[name]
end

Instance Method Details

- (Object) <=>(other)

Sorts enumerated values into the order in which they're declared.



60
61
62
# File 'lib/renum/enumerated_value.rb', line 60

def <=> other
  index <=> other.index
end

- (Object) ==(other)



64
65
66
# File 'lib/renum/enumerated_value.rb', line 64

def == other
  equal? other
end

- (Object) to_s

Returns the fully qualified name of the constant referring to this value. Don't override this if you're using Renum with the constantize_attribute plugin, which relies on this behavior.



55
56
57
# File 'lib/renum/enumerated_value.rb', line 55

def to_s
  "#{self.class}::#{name}"
end