Module: Cistern::Collection
- Includes:
- HashSupport
- Defined in:
- lib/cistern/collection.rb
Defined Under Namespace
Modules: ClassMethods
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
#hash_except, #hash_except!, #hash_slice, #hash_stringify_keys
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
145
146
147
148
149
150
151
|
# File 'lib/cistern/collection.rb', line 145
def method_missing(method, *args, &block)
if array_delegable?(method)
to_a.public_send(method, *args, &block)
else
super
end
end
|
Instance Attribute Details
#cistern ⇒ Object
Returns the value of attribute cistern.
20
21
22
|
# File 'lib/cistern/collection.rb', line 20
def cistern
@cistern
end
|
#loaded ⇒ Object
Returns the value of attribute loaded.
20
21
22
|
# File 'lib/cistern/collection.rb', line 20
def loaded
@loaded
end
|
#records ⇒ Object
Returns the value of attribute records.
20
21
22
|
# File 'lib/cistern/collection.rb', line 20
def records
@records
end
|
Class Method Details
.cistern_collection(cistern, klass, name) ⇒ Object
12
13
14
15
16
17
18
|
# File 'lib/cistern/collection.rb', line 12
def self.cistern_collection(cistern, klass, name)
cistern.const_get(:Collections).module_eval <<-EOS, __FILE__, __LINE__
def #{name}(attributes={})
#{klass.name}.new(attributes.merge(cistern: self))
end
EOS
end
|
Instance Method Details
#==(comparison_object) ⇒ Object
133
134
135
136
137
|
# File 'lib/cistern/collection.rb', line 133
def ==(comparison_object)
comparison_object.equal?(self) ||
(comparison_object.is_a?(self.class) &&
comparison_object.to_a == to_a)
end
|
#all(_ = {}) ⇒ Object
64
65
66
|
# File 'lib/cistern/collection.rb', line 64
def all(_ = {})
fail NotImplementedError
end
|
#build ⇒ Object
57
|
# File 'lib/cistern/collection.rb', line 57
alias_method :build, :initialize
|
#clear ⇒ Object
76
77
78
79
|
# File 'lib/cistern/collection.rb', line 76
def clear
self.loaded = false
records && records.clear
end
|
#create(attributes = {}) ⇒ Object
68
69
70
|
# File 'lib/cistern/collection.rb', line 68
def create(attributes = {})
new(attributes).save
end
|
#get(_identity) ⇒ Object
72
73
74
|
# File 'lib/cistern/collection.rb', line 72
def get(_identity)
fail NotImplementedError
end
|
#initialize(attributes = {}) ⇒ Object
59
60
61
62
|
# File 'lib/cistern/collection.rb', line 59
def initialize(attributes = {})
@loaded = false
merge_attributes(attributes)
end
|
#load(objects) ⇒ Object
Should be called within #all to load records into the collection
96
97
98
99
100
|
# File 'lib/cistern/collection.rb', line 96
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.
89
90
91
|
# File 'lib/cistern/collection.rb', line 89
def load_records
all unless loaded
end
|
#model ⇒ Object
102
103
104
|
# File 'lib/cistern/collection.rb', line 102
def model
self.class.model
end
|
#new(attributes = {}) ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/cistern/collection.rb', line 106
def new(attributes = {})
unless attributes.is_a?(::Hash)
fail(ArgumentError.new("Initialization parameters must be an attributes hash, got #{attributes.class} #{attributes.inspect}"))
end
model.new(
{
collection: self,
cistern: cistern,
}.merge(attributes)
)
end
|
#reload ⇒ Object
118
119
120
121
122
|
# File 'lib/cistern/collection.rb', line 118
def reload
clear
load_records
self
end
|
#respond_to?(method, include_private = false) ⇒ Boolean
129
130
131
|
# File 'lib/cistern/collection.rb', line 129
def respond_to?(method, include_private = false)
super || array_delegable?(method)
end
|
#service ⇒ Object
30
31
32
33
34
35
36
|
# File 'lib/cistern/collection.rb', line 30
def service
Cistern.deprecation(
'#service is deprecated. Please use #cistern',
caller[0]
)
@cistern
end
|
#service=(service) ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/cistern/collection.rb', line 22
def service=(service)
Cistern.deprecation(
'#service= is deprecated. Please use #cistern=',
caller[0]
)
@cistern = service
end
|
#to_a ⇒ Object
124
125
126
127
|
# File 'lib/cistern/collection.rb', line 124
def to_a
load_records
records || []
end
|