Class: ReactiveRecord::Collection
- Inherits:
-
Object
- Object
- ReactiveRecord::Collection
show all
- Defined in:
- lib/reactive_record/active_record/reactive_record/collection.rb
Instance Method Summary
collapse
Constructor Details
#initialize(target_klass, owner = nil, association = nil, *vector) ⇒ Collection
Returns a new instance of Collection.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/reactive_record/active_record/reactive_record/collection.rb', line 5
def initialize(target_klass, owner = nil, association = nil, *vector)
if association and (association.macro != :has_many or association.klass != target_klass)
message = "unimplemented association #{owner} :#{association.macro} #{association.attribute}"
`console.error(#{message})`
end
@owner = owner @association = association
@target_klass = target_klass
if owner and !owner.id and !owner.vector
@synced_collection = @collection = []
else
@vector = vector.count == 0 ? [target_klass] : vector
end
@scopes = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
88
89
90
91
92
93
94
95
96
|
# File 'lib/reactive_record/active_record/reactive_record/collection.rb', line 88
def method_missing(method, *args, &block)
if [].respond_to? method
all.send(method, *args, &block)
elsif @target_klass.respond_to? method
apply_scope(method)
else
super
end
end
|
Instance Method Details
#<<(item) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/reactive_record/active_record/reactive_record/collection.rb', line 56
def <<(item)
inverse_of = @association.inverse_of
if @owner and inverse_of = @association.inverse_of
item.attributes[inverse_of].attributes[@association.attribute].delete(item) if item.attributes[inverse_of] and item.attributes[inverse_of].attributes[@association.attribute]
item.attributes[inverse_of] = @owner
backing_record = item.instance_variable_get(:@backing_record)
React::State.set_state(backing_record, inverse_of, @owner) unless backing_record.data_loading?
end
all << item unless all.include? item
self
end
|
#==(other_collection) ⇒ Object
37
38
39
40
41
42
43
|
# File 'lib/reactive_record/active_record/reactive_record/collection.rb', line 37
def ==(other_collection)
if @collection
@collection == other_collection.all
else
!other_collection.instance_variable_get(:@collection)
end
end
|
#all ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/reactive_record/active_record/reactive_record/collection.rb', line 21
def all
unless @collection
@collection = []
if ids = ReactiveRecord::Base.fetch_from_db([*@vector, "*all"])
ids.each do |id|
@collection << @target_klass.find_by(@target_klass.primary_key => id)
end
else
ReactiveRecord::Base.load_from_db(*@vector, "*all")
@collection << ReactiveRecord::Base.new_from_vector(@target_klass, nil, *@vector, "*")
end
end
@collection
end
|
#apply_scope(scope) ⇒ Object
45
46
47
48
49
|
# File 'lib/reactive_record/active_record/reactive_record/collection.rb', line 45
def apply_scope(scope)
@scopes[scope] ||= new(@target_klass, @owner, @association, *vector, scope)
end
|
#delete(item) ⇒ Object
79
80
81
82
83
84
85
86
|
# File 'lib/reactive_record/active_record/reactive_record/collection.rb', line 79
def delete(item)
if @owner and inverse_of = @association.inverse_of
item.attributes[inverse_of] = nil
backing_record = item.instance_variable_get(:@backing_record)
React::State.set_state(backing_record, inverse_of, nil) unless backing_record.data_loading?
end
all.delete(item)
end
|
#proxy_association ⇒ Object
51
52
53
|
# File 'lib/reactive_record/active_record/reactive_record/collection.rb', line 51
def proxy_association
@association
end
|
#replace(new_array) ⇒ Object
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/reactive_record/active_record/reactive_record/collection.rb', line 68
def replace(new_array)
return new_array if @collection == new_array
if @collection
@collection.dup.each { |item| delete(item) }
else
@collection = []
end
new_array.each { |item| self << item }
new_array
end
|