Class: Cistern::Collection
Constant Summary
collapse
- BLACKLISTED_ARRAY_METHODS =
[
:compact!, :flatten!, :reject!, :reverse!, :rotate!, :map!,
:shuffle!, :slice!, :sort!, :sort_by!, :delete_if,
:keep_if, :pop, :shift, :delete_at, :compact
].to_set
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
_load, aliases, attribute, attributes, identity, ignore_attributes, ignored_attributes
#_dump, #attributes, #attributes=, #changed, #dirty?, #dirty_attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #read_attribute, #requires, #requires_one, #write_attribute
Constructor Details
#initialize(attributes = {}) ⇒ Collection
Returns a new instance of Collection.
25
26
27
|
# File 'lib/cistern/collection.rb', line 25
def initialize(attributes = {})
merge_attributes(attributes)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
110
111
112
113
114
115
116
|
# File 'lib/cistern/collection.rb', line 110
def method_missing(method, *args, &block)
if array_delegable?(method)
to_a.public_send(method, *args, &block)
else
super
end
end
|
Instance Attribute Details
#connection ⇒ Object
Also known as:
service
11
12
13
|
# File 'lib/cistern/collection.rb', line 11
def connection
@connection
end
|
#loaded ⇒ Object
11
12
13
|
# File 'lib/cistern/collection.rb', line 11
def loaded
@loaded
end
|
#records ⇒ Object
11
12
13
|
# File 'lib/cistern/collection.rb', line 11
def records
@records
end
|
Class Method Details
.model(new_model = nil) ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/cistern/collection.rb', line 15
def self.model(new_model=nil)
if new_model == nil
@model
else
@model = new_model
end
end
|
Instance Method Details
#==(comparison_object) ⇒ Object
98
99
100
101
102
|
# File 'lib/cistern/collection.rb', line 98
def ==(comparison_object)
comparison_object.equal?(self) ||
(comparison_object.is_a?(self.class) &&
comparison_object.to_a == self.to_a)
end
|
#all(identity) ⇒ Object
29
30
31
|
# File 'lib/cistern/collection.rb', line 29
def all(identity)
raise NotImplementedError
end
|
#build ⇒ Object
23
|
# File 'lib/cistern/collection.rb', line 23
alias build initialize
|
#clear ⇒ Object
41
42
43
44
|
# File 'lib/cistern/collection.rb', line 41
def clear
self.loaded = false
records && records.clear
end
|
#create(attributes = {}) ⇒ Object
33
34
35
|
# File 'lib/cistern/collection.rb', line 33
def create(attributes={})
self.new(attributes).save
end
|
#get(identity) ⇒ Object
37
38
39
|
# File 'lib/cistern/collection.rb', line 37
def get(identity)
raise NotImplementedError
end
|
#load(objects) ⇒ Object
Should be called within #all to load records into the collection
61
62
63
64
65
|
# File 'lib/cistern/collection.rb', line 61
def load(objects)
self.records = (objects || []).map { |object| new(object) }
self.loaded = true
self
end
|
#load_records ⇒ 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.
54
55
56
|
# File 'lib/cistern/collection.rb', line 54
def load_records
self.all unless self.loaded
end
|
#model ⇒ Object
67
68
69
|
# File 'lib/cistern/collection.rb', line 67
def model
self.class.instance_variable_get('@model')
end
|
#new(attributes = {}) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/cistern/collection.rb', line 71
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
83
84
85
86
87
|
# File 'lib/cistern/collection.rb', line 83
def reload
clear
load_records
self
end
|
#respond_to?(method, include_private = false) ⇒ Boolean
94
95
96
|
# File 'lib/cistern/collection.rb', line 94
def respond_to?(method, include_private = false)
super || array_delegable?(method)
end
|
#to_a ⇒ Object
89
90
91
92
|
# File 'lib/cistern/collection.rb', line 89
def to_a
load_records
self.records || []
end
|