simplify/consistency in lazy array stuff

This commit is contained in:
geemus 2010-06-07 18:25:34 -07:00
parent b9fb24963c
commit 57f0529da3
1 changed files with 17 additions and 12 deletions

View File

@ -7,7 +7,9 @@ module Fog
Array.public_instance_methods(false).each do |method| Array.public_instance_methods(false).each do |method|
class_eval <<-RUBY class_eval <<-RUBY
def #{method}(*args) def #{method}(*args)
lazy_load unless @loaded
lazy_load
end
super super
end end
RUBY RUBY
@ -16,9 +18,11 @@ module Fog
%w[reject select].each do |method| %w[reject select].each do |method|
class_eval <<-RUBY class_eval <<-RUBY
def #{method}(*args) def #{method}(*args)
lazy_load unless @loaded
lazy_load
end
data = super data = super
self.class.new(:connection => self.connection).load(data.map {|member| member.attributes}) result = self.clone.clear.concat(data)
end end
RUBY RUBY
end end
@ -33,6 +37,11 @@ module Fog
attr_accessor :connection attr_accessor :connection
def clear
@loaded = true
super
end
def create(attributes = {}) def create(attributes = {})
object = new(attributes) object = new(attributes)
object.save object.save
@ -41,7 +50,6 @@ module Fog
def initialize(attributes = {}) def initialize(attributes = {})
merge_attributes(attributes) merge_attributes(attributes)
@loaded = false
end end
def inspect def inspect
@ -69,10 +77,7 @@ module Fog
end end
def load(objects) def load(objects)
if @loaded clear
clear
end
@loaded = true
for object in objects for object in objects
self << new(object) self << new(object)
end end
@ -93,7 +98,9 @@ module Fog
end end
def reload def reload
self.clear.concat(all) clear
lazy_load
self
end end
def table(attributes = nil) def table(attributes = nil)
@ -107,9 +114,7 @@ module Fog
private private
def lazy_load def lazy_load
unless @loaded self.all
self.all
end
end end
end end