1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

using metaprogramming to automatically generate all the lazy_loader methods

This commit is contained in:
Rodrigo Estebanez 2013-07-09 13:19:14 +02:00
parent 512f3bba40
commit 2cfae0c0df
2 changed files with 27 additions and 14 deletions

View file

@ -5,7 +5,7 @@ module Fog
class Vcloudng
class Catalog < Fog::Model
identity :id
attribute :name
@ -21,17 +21,20 @@ module Fog
def initialize(attrs={})
super(attrs)
[:description, :is_published].each { |attr| attributes[attr]= NonLoaded if attributes[attr].nil? }
lazy_load_attrs = self.class.attributes - attributes.keys
lazy_load_attrs.each do |attr|
attributes[attr]= NonLoaded if attributes[attr].nil?
make_lazy_load_method(attr)
end
end
def description
reload if attributes[:description] == NonLoaded and !@inspecting
attributes[:description]
end
def is_published
reload if attributes[:is_published] == NonLoaded and !@inspecting
attributes[:is_published]
def make_lazy_load_method(attr)
self.class.instance_eval do
define_method(attr) do
reload if attributes[attr] == NonLoaded and !@inspecting
attributes[attr]
end
end
end
def inspect

View file

@ -30,12 +30,21 @@ module Fog
def initialize(attrs={})
super(attrs)
[:description].each { |attr| attributes[attr]= NonLoaded if attributes[attr].nil? }
lazy_load_attrs = self.class.attributes - attributes.keys
lazy_load_attrs.each do |attr|
puts "-#{attr}-"
attributes[attr]= NonLoaded if attributes[attr].nil?
make_lazy_load_method(attr)
end
end
def description
reload if attributes[:description] == NonLoaded and !@inspecting
attributes[:description]
def make_lazy_load_method(attr)
self.class.instance_eval do
define_method(attr) do
reload if attributes[attr] == NonLoaded and !@inspecting
attributes[attr]
end
end
end
def inspect
@ -44,6 +53,7 @@ module Fog
@inspecting = false
out
end
end
end