Class: Cistern::Collection
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
_load, aliases, attribute, attributes, identity, ignore_attributes, ignored_attributes
#_dump, #attributes, #attributes=, #dup, #identity, #identity=, #merge_attributes, #new_record?, #requires, #requires_one
Constructor Details
#initialize(attributes = {}) ⇒ Collection
Returns a new instance of Collection.
32
33
34
35
|
# File 'lib/cistern/collection.rb', line 32
def initialize(attributes = {})
@loaded = false
merge_attributes(attributes)
end
|
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
28
29
30
|
# File 'lib/cistern/collection.rb', line 28
def connection
@connection
end
|
Class Method Details
.model(new_model = nil) ⇒ Object
20
21
22
23
24
25
26
|
# File 'lib/cistern/collection.rb', line 20
def self.model(new_model=nil)
if new_model == nil
@model
else
@model = new_model
end
end
|
Instance Method Details
#build ⇒ Object
30
|
# File 'lib/cistern/collection.rb', line 30
alias build initialize
|
#clear ⇒ Object
37
38
39
40
|
# File 'lib/cistern/collection.rb', line 37
def clear
@loaded = true
super
end
|
#create(attributes = {}) ⇒ Object
42
43
44
|
# File 'lib/cistern/collection.rb', line 42
def create(attributes={})
self.new(attributes).save
end
|
#get(identity) ⇒ Object
46
47
48
|
# File 'lib/cistern/collection.rb', line 46
def get(identity)
raise NotImplementedError
end
|
#inspect ⇒ Object
50
51
52
53
|
# File 'lib/cistern/collection.rb', line 50
def inspect
lazy_load unless @loaded
Cistern.formatter.call(self)
end
|
#lazy_load ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
56
57
58
|
# File 'lib/cistern/collection.rb', line 56
def lazy_load
self.all
end
|
#load(objects) ⇒ Object
60
61
62
63
64
65
66
|
# File 'lib/cistern/collection.rb', line 60
def load(objects)
clear
for object in objects
self << new(object)
end
self
end
|
#model ⇒ Object
68
69
70
|
# File 'lib/cistern/collection.rb', line 68
def model
self.class.instance_variable_get('@model')
end
|
#new(attributes = {}) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/cistern/collection.rb', line 72
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(
{
:collection => self,
:connection => connection,
}.merge(attributes)
)
end
|
#reload ⇒ Object
84
85
86
87
88
|
# File 'lib/cistern/collection.rb', line 84
def reload
clear
lazy_load
self
end
|