Class: Fiddley::Enum

Inherits:
Object
  • Object
show all
Defined in:
lib/fiddley/enum.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#tagObject (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_mapObject Also known as: to_h, to_hash



40
41
42
# File 'lib/fiddley/enum.rb', line 40

def symbol_map
  @map
end

#symbolsObject



27
28
29
# File 'lib/fiddley/enum.rb', line 27

def symbols
  @map.keys
end