Class: Howkast::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/howkast/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(processor, data, &block) ⇒ Model

Returns a new instance of Model.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/howkast/model.rb', line 16

def initialize processor, data, &block
  data.each do |field, value| 
    value = if block and value.respond_to? :each
      block[field, value]
    elsif value.nil?
      processor.default_for field
    else
      value
    end
    instance_variable_set :"@#{field}", self.class.parse(value)
  end
end

Class Method Details

.parse(value) ⇒ Object



33
34
35
# File 'lib/howkast/model.rb', line 33

def self.parse value
  value.instance_of?(String) ? value.parse : value
end

.synthesize(name, data) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/howkast/model.rb', line 4

def self.synthesize name, data
  classname = name.modulize
  attrdef   = Proc.new{ data.keys.each{ |field| attr_reader field } }
  if const_defined? classname
    klass = const_get classname
    klass.class_eval &attrdef
    klass
  else
    const_set classname, Class.new(self, &attrdef)
  end
end

Instance Method Details

#instance_attributesObject



29
30
31
# File 'lib/howkast/model.rb', line 29

def instance_attributes
  instance_variables.map{ |name| "#{name}"[1 .. -1].to_sym }
end