class Enum::Base
Public Class Methods
all()
click to toggle source
# File lib/enum/base.rb, line 16 def all history.to_a end
enum(t)
click to toggle source
# File lib/enum/base.rb, line 32 def enum(t) exists(t.to_sym) t.to_sym end
enums(*tokens)
click to toggle source
# File lib/enum/base.rb, line 28 def enums(*tokens) tokens.map { |token| enum(token) } end
exists(token)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/enum/base.rb, line 24 def include?(token) history.include?(token.to_sym) end
index(token)
click to toggle source
def name(t)
translate(enum(t))
end
# 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
indexes()
click to toggle source
# File lib/enum/base.rb, line 20 def indexes (0...store.size).to_a end
inherited(child)
click to toggle source
# File lib/enum/base.rb, line 6 def inherited(child) return if self == Enum init_child_class(child) end
real_enum(t)
click to toggle source
# File lib/enum/base.rb, line 37 def real_enum(t) ts = t.to_sym store[index(t)] end
value(t)
click to toggle source
# File lib/enum/base.rb, line 48 def value(t) e = real_enum(t) e[t] end
values(*ary)
click to toggle source
# File lib/enum/base.rb, line 12 def values(*ary) add_value(ary.first) if ary.first end
Protected Class Methods
history()
click to toggle source
# File lib/enum/base.rb, line 87 def history @history ||= Set.new end
history=(set)
click to toggle source
# File lib/enum/base.rb, line 91 def history=(set) @history = set end
store()
click to toggle source
# File lib/enum/base.rb, line 79 def store @store ||= Array.new end
store=(ary)
click to toggle source
# File lib/enum/base.rb, line 83 def store=(ary) @store = ary end
Private Class Methods
add_value(val)
click to toggle source
def translate(token, options = {})
I18n.t(token, scope: "enum.#{self}", exception_handler: proc do if superclass == Enum::Base I18n.t(token, options.merge(scope: "enum.#{self}")) else superclass.translate(token, exception_handler: proc do I18n.t(token, scope: "enum.#{self}") end) end end)
end
# File lib/enum/base.rb, line 110 def add_value(val) raise(InitNotHashError, "'#{val}' is not a hash and cannot be processed") unless val.is_a? Hash val.each do |newKey, newValue| raise(KeyInUseError, "'#{key}' is already a key existing in enum") if history.include?(newKey.to_sym) store.push(Hash[newKey, newValue]) history.add(newKey) end end
init_child_class(child)
click to toggle source
# File lib/enum/base.rb, line 119 def init_child_class(child) child.store = self.store.clone child.history = self.history.clone end