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

Returns a new instance of Base.



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

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

Class Method Details

.[](value) ⇒ Object



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

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

.eachObject



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

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

.each_keyObject



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

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

.each_labelObject



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

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

.each_valueObject



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

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

.include?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.inherited(clazz) ⇒ Object



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

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



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

def inspect
  to_s
end

.key(value) ⇒ Object



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

def key(value)
  @keys[value]
end

.keysObject



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

def keys
  @keys.values
end

.label(value) ⇒ Object



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

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

.labelsObject



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

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

.sizeObject



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

def size
  @instances.size
end

.to_sObject



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

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



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

def values
  @instances.keys
end

Instance Method Details

#[](pos) ⇒ Object



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

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

#inspectObject



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

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

#labelObject



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

def label
  @attributes.second
end

#to_sObject



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

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

#valueObject



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

def value
  @attributes.first
end