Skip to content

Commit a1d431c

Browse files
author
yann ARMAND
committed
[back_assignments] move CollectionOfProxy class in its own file
1 parent 9d54af6 commit a1d431c

File tree

4 files changed

+85
-79
lines changed

4 files changed

+85
-79
lines changed

lib/couchrest/model/associations.rb

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,15 @@ def create_collection_of_getter(attrib, options)
180180
def #{attrib}(reload = false)
181181
return @#{attrib} unless @#{attrib}.nil? or reload
182182
ary = self.#{options[:foreign_key]}.collect{|i| #{options[:proxy]}.get(i)}
183-
@#{attrib} = ::CouchRest::Model::CollectionOfProxy.new(ary, find_property('#{options[:foreign_key]}'), self)
183+
@#{attrib} = ::CouchRest::Model::Associations::CollectionOfProxy.new(ary, find_property('#{options[:foreign_key]}'), self)
184184
end
185185
EOS
186186
end
187187

188188
def create_collection_of_setter(attrib, options)
189189
class_eval <<-EOS, __FILE__, __LINE__ + 1
190190
def #{attrib}=(value)
191-
@#{attrib} = ::CouchRest::Model::CollectionOfProxy.new(value, find_property('#{options[:foreign_key]}'), self)
191+
@#{attrib} = ::CouchRest::Model::Associations::CollectionOfProxy.new(value, find_property('#{options[:foreign_key]}'), self)
192192
end
193193
EOS
194194
end
@@ -232,82 +232,6 @@ def save_dirty_association
232232

233233
end
234234

235-
# Special proxy for a collection of items so that adding and removing
236-
# to the list automatically updates the associated property.
237-
class CollectionOfProxy < CastedArray
238-
239-
def initialize(array, property, parent)
240-
(array ||= []).compact!
241-
super(array, property, parent)
242-
casted_by[casted_by_property.to_s] = [] # replace the original array!
243-
array.compact.each do |obj|
244-
check_obj(obj)
245-
casted_by[casted_by_property.to_s] << obj.id
246-
end
247-
end
248-
249-
def << obj
250-
add_to_collection_with(:<<, obj)
251-
super(obj)
252-
end
253-
254-
def push(obj)
255-
add_to_collection_with(:push, obj)
256-
super(obj)
257-
end
258-
259-
def unshift(obj)
260-
add_to_collection_with(:unshift, obj)
261-
super(obj)
262-
end
263-
264-
def []= index, obj
265-
add_to_collection_with(:[]=, obj, index)
266-
super(index, obj)
267-
end
268-
269-
def pop
270-
obj = casted_by.send(casted_by_property.options[:proxy_name]).last
271-
casted_by[casted_by_property.to_s].pop
272-
obj.set_back_association(nil, casted_by.class.name, casted_by_property.options[:reverse_association])
273-
casted_by.register_dirty_association(obj)
274-
super
275-
end
276-
277-
def shift
278-
obj = casted_by.send(casted_by_property.options[:proxy_name]).first
279-
casted_by[casted_by_property.to_s].shift
280-
obj.set_back_association(nil, casted_by.class.name, casted_by_property.options[:reverse_association])
281-
casted_by.register_dirty_association(obj)
282-
super
283-
end
284-
285-
protected
286-
287-
def check_obj(obj)
288-
raise "Object cannot be added to #{casted_by.class.to_s}##{casted_by_property.to_s} collection unless saved" if obj.new?
289-
end
290-
291-
def add_to_collection_with(method, obj, index=nil)
292-
check_obj(obj)
293-
args = [ obj.id ]
294-
args = args.insert(0, index) if index
295-
casted_by[casted_by_property.to_s].send(method, *args)
296-
obj.set_back_association(casted_by, casted_by.class.name, casted_by_property.options[:reverse_association])
297-
casted_by.register_dirty_association(obj)
298-
end
299-
300-
# Override CastedArray instantiation_and_cast method for a simpler
301-
# version that will not try to cast the model.
302-
def instantiate_and_cast(obj, change = true)
303-
couchrest_parent_will_change! if change && use_dirty?
304-
obj.casted_by = casted_by if obj.respond_to?(:casted_by)
305-
obj.casted_by_property = casted_by_property if obj.respond_to?(:casted_by_property)
306-
obj
307-
end
308-
309-
end
310-
311235
end
312236

313237
end
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
module CouchRest
2+
module Model
3+
module Associations
4+
# Special proxy for a collection of items so that adding and removing
5+
# to the list automatically updates the associated property.
6+
class CollectionOfProxy < CastedArray
7+
8+
def initialize(array, property, parent)
9+
(array ||= []).compact!
10+
super(array, property, parent)
11+
casted_by[casted_by_property.to_s] = [] # replace the original array!
12+
array.compact.each do |obj|
13+
check_obj(obj)
14+
casted_by[casted_by_property.to_s] << obj.id
15+
end
16+
end
17+
18+
def << obj
19+
add_to_collection_with(:<<, obj)
20+
super(obj)
21+
end
22+
23+
def push(obj)
24+
add_to_collection_with(:push, obj)
25+
super(obj)
26+
end
27+
28+
def unshift(obj)
29+
add_to_collection_with(:unshift, obj)
30+
super(obj)
31+
end
32+
33+
def []= index, obj
34+
add_to_collection_with(:[]=, obj, index)
35+
super(index, obj)
36+
end
37+
38+
def pop
39+
obj = casted_by.send(casted_by_property.options[:proxy_name]).last
40+
casted_by[casted_by_property.to_s].pop
41+
obj.set_back_association(nil, casted_by.class.name, casted_by_property.options[:reverse_association])
42+
casted_by.register_dirty_association(obj)
43+
super
44+
end
45+
46+
def shift
47+
obj = casted_by.send(casted_by_property.options[:proxy_name]).first
48+
casted_by[casted_by_property.to_s].shift
49+
obj.set_back_association(nil, casted_by.class.name, casted_by_property.options[:reverse_association])
50+
casted_by.register_dirty_association(obj)
51+
super
52+
end
53+
54+
protected
55+
56+
def check_obj(obj)
57+
raise "Object cannot be added to #{casted_by.class.to_s}##{casted_by_property.to_s} collection unless saved" if obj.new?
58+
end
59+
60+
def add_to_collection_with(method, obj, index=nil)
61+
check_obj(obj)
62+
args = [ obj.id ]
63+
args = args.insert(0, index) if index
64+
casted_by[casted_by_property.to_s].send(method, *args)
65+
obj.set_back_association(casted_by, casted_by.class.name, casted_by_property.options[:reverse_association])
66+
casted_by.register_dirty_association(obj)
67+
end
68+
69+
# Override CastedArray instantiation_and_cast method for a simpler
70+
# version that will not try to cast the model.
71+
def instantiate_and_cast(obj, change = true)
72+
couchrest_parent_will_change! if change && use_dirty?
73+
obj.casted_by = casted_by if obj.respond_to?(:casted_by)
74+
obj.casted_by_property = casted_by_property if obj.respond_to?(:casted_by_property)
75+
obj
76+
end
77+
78+
end
79+
end
80+
end
81+
end

lib/couchrest_model.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
require "couchrest/model/extended_attachments"
4141
require "couchrest/model/proxyable"
4242
require "couchrest/model/associations"
43+
require "couchrest/model/associations/collection_of_proxy"
4344
require "couchrest/model/configuration"
4445
require "couchrest/model/connection"
4546
require "couchrest/model/design"

spec/unit/assocations_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def SaleInvoice.merge_assoc_opts(*args)
111111
it "should create an associated property and collection proxy" do
112112
@invoice.respond_to?('entry_ids').should be_true
113113
@invoice.respond_to?('entry_ids=').should be_true
114-
@invoice.entries.class.should eql(::CouchRest::Model::CollectionOfProxy)
114+
@invoice.entries.class.should eql(::CouchRest::Model::Associations::CollectionOfProxy)
115115
end
116116

117117
it "should allow replacement of objects" do

0 commit comments

Comments
 (0)