Class: GenericModel

Inherits:
Object show all
Defined in:
lib/common/generic_model.rb

Overview

class FooBar < GenericModel

values [1, 'LinkedIn'],
       [2, 'Facebook'],
       [3, 'Twitter'],
       [4, 'Google'],
       [5, 'Email'],
       [6, 'Mobile']

def ico
  %{<img src="/images/type/#{code}.png" style="width:16px; height:16px; vertical-align:middle; " />}
end

end

Constant Summary collapse

@@values =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vals) ⇒ GenericModel

Returns a new instance of GenericModel.



54
55
56
# File 'lib/common/generic_model.rb', line 54

def initialize vals
  @_vals = vals
end

Class Method Details

.add_value(val) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/common/generic_model.rb', line 28

def add_value val
  o = new(val)
  for key in val.keys
    eval %[def o.#{key}; @_vals[:#{key}]; end]
  end
  @@values[self.to_s].push(o)
end

.allObject



43
44
45
# File 'lib/common/generic_model.rb', line 43

def all
  @@values[self.to_s]
end

.find(id) ⇒ Object



36
37
38
39
40
41
# File 'lib/common/generic_model.rb', line 36

def find id
  for el in all
    return el if el.id == id
  end
  nil
end

.values(vals) ⇒ Object



23
24
25
26
# File 'lib/common/generic_model.rb', line 23

def values vals
  @@values[self.to_s] = []
  vals.map { |el| add_value(el) }
end

.where(opts) ⇒ Object



47
48
49
# File 'lib/common/generic_model.rb', line 47

def where opts
  @@values[self.to_s].select{ |el| el[opts.keys[0]] == opts.values[0] }
end

Instance Method Details

#[](key) ⇒ Object



58
59
60
# File 'lib/common/generic_model.rb', line 58

def [] key
  @_vals[key]
end