Module: Cistern::Collection
- 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
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
116
117
118
119
120
121
122
|
# File 'lib/cistern/collection.rb', line 116
def method_missing(method, *args, &block)
if array_delegable?(method)
to_a.public_send(method, *args, &block)
else
super
end
end
|
Instance Attribute Details
#loaded ⇒ Object
Returns the value of attribute loaded.
17
18
19
|
# File 'lib/cistern/collection.rb', line 17
def loaded
@loaded
end
|
#records ⇒ Object
Returns the value of attribute records.
17
18
19
|
# File 'lib/cistern/collection.rb', line 17
def records
@records
end
|
#service ⇒ Object
Returns the value of attribute service.
17
18
19
|
# File 'lib/cistern/collection.rb', line 17
def service
@service
end
|
Class Method Details
.service_collection(service, klass, name) ⇒ Object
9
10
11
12
13
14
15
|
# File 'lib/cistern/collection.rb', line 9
def self.service_collection(service, klass, name)
service.const_get(:Collections).module_eval " def \#{name}(attributes={})\n \#{klass.name}.new(attributes.merge(service: self))\n end\n EOS\nend\n", __FILE__, __LINE__
|
Instance Method Details
#==(comparison_object) ⇒ Object
104
105
106
107
108
|
# File 'lib/cistern/collection.rb', line 104
def ==(comparison_object)
comparison_object.equal?(self) ||
(comparison_object.is_a?(self.class) &&
comparison_object.to_a == self.to_a)
end
|
#all(identity) ⇒ Object
35
36
37
|
# File 'lib/cistern/collection.rb', line 35
def all(identity)
raise NotImplementedError
end
|
#build ⇒ Object
29
|
# File 'lib/cistern/collection.rb', line 29
alias build initialize
|
#clear ⇒ Object
47
48
49
50
|
# File 'lib/cistern/collection.rb', line 47
def clear
self.loaded = false
records && records.clear
end
|
#create(attributes = {}) ⇒ Object
39
40
41
|
# File 'lib/cistern/collection.rb', line 39
def create(attributes={})
self.new(attributes).save
end
|
#get(identity) ⇒ Object
43
44
45
|
# File 'lib/cistern/collection.rb', line 43
def get(identity)
raise NotImplementedError
end
|
#initialize(attributes = {}) ⇒ Object
31
32
33
|
# File 'lib/cistern/collection.rb', line 31
def initialize(attributes = {})
merge_attributes(attributes)
end
|
#inspect ⇒ Object
52
53
54
55
56
57
|
# File 'lib/cistern/collection.rb', line 52
def inspect
if Cistern.formatter
Cistern.formatter.call(self)
else super
end
end
|
#load(objects) ⇒ Object
Should be called within #all to load records into the collection
67
68
69
70
71
|
# File 'lib/cistern/collection.rb', line 67
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.
60
61
62
|
# File 'lib/cistern/collection.rb', line 60
def load_records
self.all unless self.loaded
end
|
#model ⇒ Object
73
74
75
|
# File 'lib/cistern/collection.rb', line 73
def model
self.class.model
end
|
#new(attributes = {}) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/cistern/collection.rb', line 77
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,
:service => service,
}.merge(attributes)
)
end
|
#reload ⇒ Object
89
90
91
92
93
|
# File 'lib/cistern/collection.rb', line 89
def reload
clear
load_records
self
end
|
#respond_to?(method, include_private = false) ⇒ Boolean
100
101
102
|
# File 'lib/cistern/collection.rb', line 100
def respond_to?(method, include_private = false)
super || array_delegable?(method)
end
|
#to_a ⇒ Object
95
96
97
98
|
# File 'lib/cistern/collection.rb', line 95
def to_a
load_records
self.records || []
end
|