Class: DataMapper::Associations::OneToMany::Proxy
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
308
309
310
311
|
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations/one_to_many.rb', line 308
def method_missing(method, *args, &block)
results = children.send(method, *args, &block)
results.equal?(children) ? self : results
end
|
Instance Method Details
#<<(resource) ⇒ Object
92
93
94
95
96
97
98
|
# File 'lib/gems/dm-core-0.9.9/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/gems/dm-core-0.9.9/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/gems/dm-core-0.9.9/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
|
145
146
147
148
149
150
|
# File 'lib/gems/dm-core-0.9.9/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
168
169
170
171
172
173
174
175
|
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations/one_to_many.rb', line 168
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) ⇒ Object
135
136
137
138
|
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations/one_to_many.rb', line 135
def delete(resource)
assert_mutable
orphan_resource(super)
end
|
#delete_at(index) ⇒ Object
140
141
142
143
|
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations/one_to_many.rb', line 140
def delete_at(index)
assert_mutable
orphan_resource(super)
end
|
189
190
191
192
193
|
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations/one_to_many.rb', line 189
def destroy
assert_mutable
raise UnsavedParentError, 'You cannot mass-delete until the parent is saved' if @parent.new_record?
super
end
|
195
196
197
198
199
|
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations/one_to_many.rb', line 195
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/gems/dm-core-0.9.9/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
231
232
233
|
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations/one_to_many.rb', line 231
def kind_of?(klass)
super || children.kind_of?(klass)
end
|
#new(attributes = {}) ⇒ Object
159
160
161
162
163
164
165
166
|
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations/one_to_many.rb', line 159
def new(attributes = {})
assert_mutable
raise UnsavedParentError, 'You cannot intialize until the parent is saved' if @parent.new_record?
attributes = default_attributes.merge(attributes)
resource = children.respond_to?(:new) ? super(attributes) : @relationship.child_model.new(attributes)
self << resource
resource
end
|
125
126
127
128
|
# File 'lib/gems/dm-core-0.9.9/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/gems/dm-core-0.9.9/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
|
201
202
203
204
|
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations/one_to_many.rb', line 201
def reload
@children = nil
self
end
|
#replace(other) ⇒ Object
116
117
118
119
120
121
122
123
|
# File 'lib/gems/dm-core-0.9.9/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
235
236
237
|
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations/one_to_many.rb', line 235
def respond_to?(method, include_private = false)
super || children.respond_to?(method, include_private)
end
|
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations/one_to_many.rb', line 206
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
|
130
131
132
133
|
# File 'lib/gems/dm-core-0.9.9/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/gems/dm-core-0.9.9/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
177
178
179
180
181
|
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations/one_to_many.rb', line 177
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
183
184
185
186
187
|
# File 'lib/gems/dm-core-0.9.9/lib/dm-core/associations/one_to_many.rb', line 183
def update!(attributes = {})
assert_mutable
raise UnsavedParentError, 'You cannot mass-update without validations until the parent is saved' if @parent.new_record?
super
end
|