Class: ConstEnum::Base
- Inherits:
-
Object
- Object
- ConstEnum::Base
- Extended by:
- Enumerable
- Defined in:
- lib/const_enum/base.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Class Method Summary collapse
- .[](value) ⇒ Object
- .each ⇒ Object
- .each_key ⇒ Object
- .each_label ⇒ Object
- .each_value ⇒ Object
- .include?(value) ⇒ Boolean
- .inherited(clazz) ⇒ Object
- .inspect ⇒ Object
- .key(value) ⇒ Object
- .keys ⇒ Object
- .label(value) ⇒ Object
- .labels ⇒ Object
- .size ⇒ Object
- .to_s ⇒ Object
- .values ⇒ Object
Instance Method Summary collapse
- #[](pos) ⇒ Object
-
#initialize(key, *attributes) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
- #label ⇒ Object
- #to_s ⇒ Object
- #value ⇒ Object
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
7 8 9 |
# File 'lib/const_enum/base.rb', line 7 def attributes @attributes end |
#key ⇒ Object (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 |
.each ⇒ Object
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_key ⇒ Object
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_label ⇒ Object
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_value ⇒ Object
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
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 |
.inspect ⇒ Object
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 |
.keys ⇒ Object
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 |
.labels ⇒ Object
68 69 70 |
# File 'lib/const_enum/base.rb', line 68 def labels @instances.values.map(&:label) end |
.size ⇒ Object
72 73 74 |
# File 'lib/const_enum/base.rb', line 72 def size @instances.size end |
.to_s ⇒ Object
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 |
.values ⇒ Object
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 |
#inspect ⇒ Object
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 |
#label ⇒ Object
24 25 26 |
# File 'lib/const_enum/base.rb', line 24 def label @attributes.second end |
#to_s ⇒ Object
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 |
#value ⇒ Object
20 21 22 |
# File 'lib/const_enum/base.rb', line 20 def value @attributes.first end |