Class: Elekk::Enum

Inherits:
Object
  • Object
show all
Defined in:
lib/elekk/data.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, id) ⇒ Enum

Returns a new instance of Enum.



5
6
7
8
# File 'lib/elekk/data.rb', line 5

def initialize(name, id)
  @name = name
  @id = id
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/elekk/data.rb', line 3

def id
  @id
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/elekk/data.rb', line 3

def name
  @name
end

Class Method Details

.[](idx) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/elekk/data.rb', line 18

def self.[](idx)
  if idx.is_a? Integer
    @array[idx]
  elsif idx.is_a? String
    @hash[idx.gsub(/\W/,'').to_sym]
  elsif idx.is_a? Symbol
    @hash[idx]
  elsif idx.is_a? Enum
    idx
  else
    nil
  end
end

.add_item(name, id = nil?) ) ⇒ Object



32
33
34
35
36
37
# File 'lib/elekk/data.rb', line 32

def self.add_item(name, id=nil?)
  @array ||= []
  id ||= @array.length
  obj = name ? self.new(name, id) : nil
  add_obj obj, nil, id
end

.add_items(*arr) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/elekk/data.rb', line 54

def self.add_items(*arr)
  start = arr.pop if arr.last.is_a? Integer
  arr.each do |a|
    self.add_item a, start
    start = nil
  end
end

.add_obj(obj, sym = nil, id = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/elekk/data.rb', line 39

def self.add_obj(obj, sym=nil, id=nil)
  @hash ||= {}
  @array ||= []
  if obj and @hash[obj.to_sym]
    # creating an alias
    @hash[sym] = obj if sym
    @array[id] = obj if id
  else
    # adding for the first time
    @hash[obj.to_sym] = obj if obj
    id ||= @array.length
    @array[id] = obj
  end
end

.const_missing(name) ⇒ Object



14
15
16
# File 'lib/elekk/data.rb', line 14

def self.const_missing(name)
  @hash[name]
end

.eachObject



62
63
64
65
66
# File 'lib/elekk/data.rb', line 62

def self.each
  @array.each do |v|
    yield v unless v.nil?
  end
end

.find(cond = nil, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/elekk/data.rb', line 68

def self.find(cond=nil, &block)
  each do |v|
    if cond and not cond === v.to_s
      next
    end
    if block and not yield v
      next
    end
    return v
  end
  return nil
end

Instance Method Details

#to_iObject



85
86
87
# File 'lib/elekk/data.rb', line 85

def to_i
  @id
end

#to_sObject



81
82
83
# File 'lib/elekk/data.rb', line 81

def to_s
  @name
end

#to_symObject



10
11
12
# File 'lib/elekk/data.rb', line 10

def to_sym
  @symbol ||= @name.gsub(/\W/,'').to_sym
end