Class: DataMapper::Associations::OneToMany::Proxy
- Inherits:
-
Object
- Object
- DataMapper::Associations::OneToMany::Proxy
show all
- Includes:
- DataMapper::Assertions
- Defined in:
- lib/dm-core/associations/one_to_many.rb
Overview
TODO: look at making this inherit from Collection. The API is almost identical, and it would make more sense for the relationship.get_children method to return a Proxy than a Collection that is wrapped in a Proxy.
Instance Method Summary
collapse
#assert_kind_of
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
299
300
301
302
303
304
305
|
# File 'lib/dm-core/associations/one_to_many.rb', line 299
def method_missing(method, *args, &block)
results = children.__send__(method, *args, &block) if children.respond_to?(method)
return self if LazyArray::RETURN_SELF.include?(method) && results.kind_of?(Array)
results
end
|
Instance Method Details
#<<(resource) ⇒ Object
92
93
94
95
96
97
98
|
# File 'lib/dm-core/associations/one_to_many.rb', line 92
def <<(resource)
assert_mutable
return self if !resource.new_record? && self.include?(resource)
super
relate_resource(resource)
self
end
|
#all(query = {}) ⇒ Object
FIXME: remove when RelationshipChain#get_children can return a Collection
78
79
80
|
# File 'lib/dm-core/associations/one_to_many.rb', line 78
def all(query = {})
query.empty? ? self : @relationship.get_children(@parent, query)
end
|
#build(attributes = {}) ⇒ Object
152
153
154
155
156
157
|
# File 'lib/dm-core/associations/one_to_many.rb', line 152
def build(attributes = {})
assert_mutable
attributes = default_attributes.merge(attributes)
resource = children.respond_to?(:build) ? super(attributes) : new_child(attributes)
resource
end
|
#clear ⇒ Object
145
146
147
148
149
150
|
# File 'lib/dm-core/associations/one_to_many.rb', line 145
def clear
assert_mutable
each { |resource| orphan_resource(resource) }
super
self
end
|
#create(attributes = {}) ⇒ Object
159
160
161
162
163
164
165
166
|
# File 'lib/dm-core/associations/one_to_many.rb', line 159
def create(attributes = {})
assert_mutable
raise UnsavedParentError, 'You cannot create until the parent is saved' if @parent.new_record?
attributes = default_attributes.merge(attributes)
resource = children.respond_to?(:create) ? super(attributes) : @relationship.child_model.create(attributes)
self << resource
resource
end
|
#delete(resource, &block) ⇒ Object
135
136
137
138
|
# File 'lib/dm-core/associations/one_to_many.rb', line 135
def delete(resource, &block)
assert_mutable
orphan_resource(super)
end
|
#delete_at(index) ⇒ Object
140
141
142
143
|
# File 'lib/dm-core/associations/one_to_many.rb', line 140
def delete_at(index)
assert_mutable
orphan_resource(super)
end
|
#destroy ⇒ Object
180
181
182
183
184
|
# File 'lib/dm-core/associations/one_to_many.rb', line 180
def destroy
assert_mutable
raise UnsavedParentError, 'You cannot mass-delete until the parent is saved' if @parent.new_record?
super
end
|
#destroy! ⇒ Object
186
187
188
189
190
|
# File 'lib/dm-core/associations/one_to_many.rb', line 186
def destroy!
assert_mutable
raise UnsavedParentError, 'You cannot mass-delete without validations until the parent is saved' if @parent.new_record?
super
end
|
#first(*args) ⇒ Object
FIXME: remove when RelationshipChain#get_children can return a Collection
83
84
85
86
87
88
89
90
|
# File 'lib/dm-core/associations/one_to_many.rb', line 83
def first(*args)
if args.last.respond_to?(:merge)
query = args.pop
@relationship.get_children(@parent, query, :first, *args)
else
super
end
end
|
#kind_of?(klass) ⇒ Boolean
222
223
224
|
# File 'lib/dm-core/associations/one_to_many.rb', line 222
def kind_of?(klass)
super || children.kind_of?(klass)
end
|
#pop ⇒ Object
125
126
127
128
|
# File 'lib/dm-core/associations/one_to_many.rb', line 125
def pop
assert_mutable
orphan_resource(super)
end
|
#push(*resources) ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/dm-core/associations/one_to_many.rb', line 100
def push(*resources)
assert_mutable
resources.reject { |resource| !resource.new_record? && self.include?(resource) }
super
resources.each { |resource| relate_resource(resource) }
self
end
|
#reload ⇒ Object
192
193
194
195
|
# File 'lib/dm-core/associations/one_to_many.rb', line 192
def reload
@children = nil
self
end
|
#replace(other) ⇒ Object
116
117
118
119
120
121
122
123
|
# File 'lib/dm-core/associations/one_to_many.rb', line 116
def replace(other)
assert_mutable
each { |resource| orphan_resource(resource) }
other = other.map { |resource| resource.kind_of?(Hash) ? new_child(resource) : resource }
super
other.each { |resource| relate_resource(resource) }
self
end
|
#respond_to?(method, include_private = false) ⇒ Boolean
226
227
228
|
# File 'lib/dm-core/associations/one_to_many.rb', line 226
def respond_to?(method, include_private = false)
super || children.respond_to?(method, include_private)
end
|
#save ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/dm-core/associations/one_to_many.rb', line 197
def save
return true if children.frozen?
each { |resource| save_resource(resource) }
@orphans.each do |resource|
begin
save_resource(resource, nil)
rescue
children << resource unless children.frozen? || children.include?(resource)
raise
end
end
if children.kind_of?(Array) && !children.frozen?
@children = @relationship.get_children(@parent).replace(children)
end
true
end
|
#shift ⇒ Object
130
131
132
133
|
# File 'lib/dm-core/associations/one_to_many.rb', line 130
def shift
assert_mutable
orphan_resource(super)
end
|
#unshift(*resources) ⇒ Object
108
109
110
111
112
113
114
|
# File 'lib/dm-core/associations/one_to_many.rb', line 108
def unshift(*resources)
assert_mutable
resources.reject { |resource| !resource.new_record? && self.include?(resource) }
super
resources.each { |resource| relate_resource(resource) }
self
end
|
#update(attributes = {}) ⇒ Object
168
169
170
171
172
|
# File 'lib/dm-core/associations/one_to_many.rb', line 168
def update(attributes = {})
assert_mutable
raise UnsavedParentError, 'You cannot mass-update until the parent is saved' if @parent.new_record?
super
end
|
#update!(attributes = {}) ⇒ Object
174
175
176
177
178
|
# File 'lib/dm-core/associations/one_to_many.rb', line 174
def update!(attributes = {})
assert_mutable
raise UnsavedParentError, 'You cannot mass-update without validations until the parent is saved' if @parent.new_record?
super
end
|