Module: Amee::Model::ClassMethods

Defined in:
lib/amee/model.rb

Instance Method Summary collapse

Instance Method Details

#from_hash(hash, session = nil) {|instance| ... } ⇒ Object

Yields:

  • (instance)


24
25
26
27
28
# File 'lib/amee/model.rb', line 24

def from_hash(hash, session = nil)
  instance = new(hash, session)
  yield instance if block_given?
  instance
end

#item_populators(hash) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/amee/model.rb', line 30

def item_populators(hash)
  @lazy_populators ||= [] << hash.keys
  populator_item_getter(hash.keys)
  hash.each_pair do |key, value|
    
    define_method("#{key}=") do |definition|
      if session && !definition.nil?
        if self.resource_path
          definition["resource_path"] = self.resource_path+"/#{definition["path"]}" 
        end
        instance_variable_set("@#{key}", value[:class].new(definition, session))
      end 
    end
    
  end
end

#list_populators(hash) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/amee/model.rb', line 47

def list_populators(hash)
  @lazy_populators ||= [] << hash.keys
  populator_lists_getter(hash.keys)
  hash.each_pair do |key, value|
    
    define_method("#{key}=") do |list|
      instance_variable_set("@#{key}", list.map do |item|
        if self.resource_path
          item["resource_path"] = self.resource_path+"/#{item["path"]}" 
        end
        value[:class].new(item, session)
      end) if session
    end
      
  end
end

#populator_item_getter(pops) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/amee/model.rb', line 75

def populator_item_getter(pops)
  pops.each do |symbol|
     define_method(symbol) do
       if !populated? && self.resource_path
         populate! 
       end
       instance_variable_get("@#{symbol}")
     end
   end
end

#populator_lists_getter(pops) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/amee/model.rb', line 64

def populator_lists_getter(pops)
  pops.each do |symbol|
     define_method(symbol) do
       if !populated? && self.resource_path
         populate! 
       end
       instance_variable_get("@#{symbol}") || []
     end
   end
end