Class: ConstEnum::Base

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/const_enum/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, *attributes) ⇒ Base



9
10
11
12
13
# File 'lib/const_enum/base.rb', line 9

def initialize(key, *attributes)
  @key = key
  @attributes = attributes
  @attributes.freeze
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/const_enum/base.rb', line 6

def attributes
  @attributes
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/const_enum/base.rb', line 6

def key
  @key
end

Class Method Details

.[](value) ⇒ Object



47
48
49
# File 'lib/const_enum/base.rb', line 47

def [](value)
  @instances[value]
end

.eachObject



75
76
77
78
# File 'lib/const_enum/base.rb', line 75

def each
  return Enumerator.new(self) unless block_given?
  @instances.each {|value, obj| yield obj }
end

.each_keyObject



80
81
82
83
# File 'lib/const_enum/base.rb', line 80

def each_key
  return Enumerator.new(self, :each_key) unless block_given?
  keys.each {|key| yield key }
end

.each_labelObject



90
91
92
93
# File 'lib/const_enum/base.rb', line 90

def each_label
  return Enumerator.new(self, :each_label) unless block_given?
  labels.each {|label| yield label }
end

.each_valueObject



85
86
87
88
# File 'lib/const_enum/base.rb', line 85

def each_value
  return Enumerator.new(self, :each_value) unless block_given?
  values.each {|value| yield value }
end

.include?(value) ⇒ Boolean



43
44
45
# File 'lib/const_enum/base.rb', line 43

def include?(value)
  (ConstEnum === value) ? @instances.include?(value) : @instances.key?(value)
end

.inherited(clazz) ⇒ Object



37
38
39
40
41
# File 'lib/const_enum/base.rb', line 37

def inherited(clazz)
  hash_class = RUBY_VERSION < '1.9' ? ActiveSupport::OrderedHash : Hash 
  clazz.instance_variable_set(:@instances, hash_class.new)
  clazz.instance_variable_set(:@keys, hash_class.new)
end

.inspectObject



95
96
97
# File 'lib/const_enum/base.rb', line 95

def inspect
  to_s
end

.key(value) ⇒ Object



55
56
57
# File 'lib/const_enum/base.rb', line 55

def key(value)
  @keys[value]
end

.keysObject



59
60
61
# File 'lib/const_enum/base.rb', line 59

def keys
  @keys.values
end

.label(value) ⇒ Object



51
52
53
# File 'lib/const_enum/base.rb', line 51

def label(value)
  @instances[value].label
end

.labelsObject



67
68
69
# File 'lib/const_enum/base.rb', line 67

def labels
  @instances.values.map(&:label)
end

.sizeObject



71
72
73
# File 'lib/const_enum/base.rb', line 71

def size
  @instances.size
end

.to_sObject



99
100
101
102
103
104
105
106
# File 'lib/const_enum/base.rb', line 99

def to_s
  return super if ConstEnum::Base == self
  elems = []
  @keys.each do |value, key|
    elems << "#{key}[#{@instances[value]}]"
  end
  super << " { #{elems.join(', ')} }"
end

.valuesObject



63
64
65
# File 'lib/const_enum/base.rb', line 63

def values
  @instances.keys
end

Instance Method Details

#[](pos) ⇒ Object



15
16
17
# File 'lib/const_enum/base.rb', line 15

def [](pos)
  @attributes[pos]
end

#inspectObject



27
28
29
# File 'lib/const_enum/base.rb', line 27

def inspect
  "#{@attributes.first.inspect}:#{(@attributes[2 .. -1]||[]).unshift(label).map(&:inspect).join(', ')}"
end

#labelObject



23
24
25
# File 'lib/const_enum/base.rb', line 23

def label
  @attributes.second
end

#to_sObject



31
32
33
# File 'lib/const_enum/base.rb', line 31

def to_s
  "#{@attributes.first.inspect}:#{(@attributes[2 .. -1]||[]).unshift(label).map(&:inspect).join(', ')}"
end

#valueObject



19
20
21
# File 'lib/const_enum/base.rb', line 19

def value
  @attributes.first
end