lib/cistern/collection.rb in cistern-0.2.3 vs lib/cistern/collection.rb in cistern-0.3.0
- old
+ new
@@ -23,34 +23,54 @@
else
@model = new_model
end
end
+ attr_accessor :connection
+
+ alias build initialize
+
def initialize(attributes = {})
@loaded = false
merge_attributes(attributes)
end
+ def clear
+ @loaded = true
+ super
+ end
+
def create(attributes={})
self.new(attributes).save
end
def get(identity)
raise NotImplementedError
end
- def clear
- @loaded = true
- super
+ def inspect
+ lazy_load unless @loaded
+ Cistern.formatter.call(self)
end
+ # @api private
+ def lazy_load
+ self.all
+ end
+
+ def load(objects)
+ clear
+ for object in objects
+ self << new(object)
+ end
+ self
+ end
+
def model
self.class.instance_variable_get('@model')
end
- attr_accessor :connection
-
def new(attributes = {})
unless attributes.is_a?(::Hash)
raise(ArgumentError.new("Initialization parameters must be an attributes hash, got #{attributes.class} #{attributes.inspect}"))
end
model.new(
@@ -59,30 +79,11 @@
:connection => connection,
}.merge(attributes)
)
end
- def load(objects)
- clear
- for object in objects
- self << new(object)
- end
- self
- end
-
def reload
clear
lazy_load
self
- end
-
- def inspect
- lazy_load unless @loaded
- Cistern.formatter.call(self)
- end
-
- private
-
- def lazy_load
- self.all
end
end