lib/fog/collection.rb in fog-0.0.38 vs lib/fog/collection.rb in fog-0.0.39
- old
+ new
@@ -1,8 +1,17 @@
module Fog
class Collection < Array
+ Array.public_instance_methods(false).each do |method|
+ class_eval <<-RUBY
+ def #{method}(*args)
+ lazy_load
+ super
+ end
+ RUBY
+ end
+
def self._load(marhsalled)
new(Marshal.load(marshalled))
end
def self.attribute(name, other_names = [])
@@ -54,10 +63,11 @@
object
end
def initialize(attributes = {})
merge_attributes(attributes)
+ @loaded = false
end
def inspect
data = "#<#{self.class.name}"
for attribute in self.class.attributes
@@ -98,9 +108,15 @@
def reload
self.clear.concat(all)
end
private
+
+ def lazy_load
+ unless @loaded
+ self.all
+ end
+ end
def remap_attributes(attributes, mapping)
for key, value in mapping
if attributes.key?(key)
attributes[value] = attributes.delete(key)