Class: Enum::Base

Inherits:
BasicObject
Defined in:
lib/enum/base.rb

Class Method Summary collapse

Class Method Details

.allObject



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

def all
  history.to_a
end

.enum(t) ⇒ Object



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

def enum(t)
  exists(t.to_sym)
  t.to_sym
end

.enums(*tokens) ⇒ Object



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

def enums(*tokens)
  tokens.map { |token| enum(token) }
end

.exists(token) ⇒ Object



42
43
44
45
46
# File 'lib/enum/base.rb', line 42

def exists(token)
  unless history.include?(token.to_sym)
    raise(TokenNotFoundError, "Token '#{token}' not found in #{self}")
  end
end

.find_value(v) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/enum/base.rb', line 53

def find_value(v)
  key = nil
  store.each do |e|
    key = e.keys[0] if e.values[0] == v
  end
  unless key
    raise(TokenNotFoundError, "Token for value '#{v}' not found in #{self}")
  end
  enum(key)
end

.include?(token) ⇒ Boolean

Returns:

  • (Boolean)


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

def include?(token)
  history.include?(token.to_sym)
end

.index(token) ⇒ Object

def name(t)

  translate(enum(t))
end


69
70
71
72
73
74
75
# File 'lib/enum/base.rb', line 69

def index(token)
  exists(token)
  store.index do |h|
    key, value = h.first
    key == token.to_sym
  end
end

.indexesObject



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

def indexes
  (0...store.size).to_a
end

.inherited(child) ⇒ Object



6
7
8
9
10
# File 'lib/enum/base.rb', line 6

def inherited(child)
  return if self == Enum

  init_child_class(child)
end

.real_enum(t) ⇒ Object



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

def real_enum(t)
  ts = t.to_sym
  store[index(t)]
end

.value(t) ⇒ Object



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

def value(t)
  e = real_enum(t)
  e[t]
end

.values(*ary) ⇒ Object



12
13
14
# File 'lib/enum/base.rb', line 12

def values(*ary)
  add_value(ary.first) if ary.first
end