Class: Fiddley::Enum
- Inherits:
-
Object
- Object
- Fiddley::Enum
- Defined in:
- lib/fiddley/enum.rb
Instance Attribute Summary collapse
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Instance Method Summary collapse
- #[](k) ⇒ Object (also: #find)
-
#initialize(*args) ⇒ Enum
constructor
A new instance of Enum.
- #symbol_map ⇒ Object (also: #to_h, #to_hash)
- #symbols ⇒ Object
Constructor Details
#initialize(*args) ⇒ Enum
Returns a new instance of Enum.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/fiddley/enum.rb', line 3 def initialize(*args) info, @tag = *args @map = {} if info last = nil value = 0 info.each do |k| if k.is_a?(Symbol) @map[k] = value last = k value += 1 elsif k.is_a?(Integer) @map[last] = k value = k + 1 else raise RuntimeError, "broken enum" end end end @rmap = @map.invert end |
Instance Attribute Details
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
25 26 27 |
# File 'lib/fiddley/enum.rb', line 25 def tag @tag end |
Instance Method Details
#[](k) ⇒ Object Also known as: find
31 32 33 34 35 36 37 |
# File 'lib/fiddley/enum.rb', line 31 def [](k) if k.is_a?(Symbol) @map[k] elsif k.is_a?(Integer) @rmap[k] end end |
#symbol_map ⇒ Object Also known as: to_h, to_hash
40 41 42 |
# File 'lib/fiddley/enum.rb', line 40 def symbol_map @map end |
#symbols ⇒ Object
27 28 29 |
# File 'lib/fiddley/enum.rb', line 27 def symbols @map.keys end |