Class: MongoModel::Associations::HasManyByIds::Proxy

Inherits:
Base::Proxy
  • Object
show all
Defined in:
lib/mongomodel/concerns/associations/has_many_by_ids.rb

Constant Summary collapse

OVERRIDE_METHODS =

Pass these methods to the association class rather than the Array target

[ :find ]

Instance Attribute Summary

Attributes inherited from Base::Proxy

#association

Instance Method Summary collapse

Methods inherited from Base::Proxy

#initialize, #loaded!, #loaded?, #proxy_respond_to?, #reset, #respond_to?, #target, #target=

Constructor Details

This class inherits a constructor from MongoModel::Associations::Base::Proxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &block) ⇒ Object (private)



188
189
190
191
192
193
194
# File 'lib/mongomodel/concerns/associations/has_many_by_ids.rb', line 188

def method_missing(method_id, *args, &block)
  if target.respond_to?(method_id) && !OVERRIDE_METHODS.include?(method_id.to_sym)
    super(method_id, *args, &block)
  else
    association.scoped.send(method_id, *args, &block)
  end
end

Instance Method Details

#<<(doc) ⇒ Object



117
118
119
120
121
122
# File 'lib/mongomodel/concerns/associations/has_many_by_ids.rb', line 117

def <<(doc)
  ensure_class(doc)
  super if loaded?
  ids << doc.id
  self
end

#[]=(index, doc) ⇒ Object



110
111
112
113
114
115
# File 'lib/mongomodel/concerns/associations/has_many_by_ids.rb', line 110

def []=(index, doc)
  ensure_class(doc)
  super if loaded?
  ids[index] = doc.id
  self
end

#build(*args, &block) ⇒ Object



98
99
100
101
102
# File 'lib/mongomodel/concerns/associations/has_many_by_ids.rb', line 98

def build(*args, &block)
  doc = association.build(*args, &block)
  self << doc
  doc
end

#clearObject



159
160
161
162
163
# File 'lib/mongomodel/concerns/associations/has_many_by_ids.rb', line 159

def clear
  super if loaded?
  ids.clear
  self
end

#concat(documents) ⇒ Object



124
125
126
127
128
129
# File 'lib/mongomodel/concerns/associations/has_many_by_ids.rb', line 124

def concat(documents)
  ensure_class(documents)
  super if loaded?
  ids.concat(documents.map { |doc| doc.id })
  self
end

#create(*args, &block) ⇒ Object



104
105
106
107
108
# File 'lib/mongomodel/concerns/associations/has_many_by_ids.rb', line 104

def create(*args, &block)
  doc = association.create(*args, &block)
  self << doc
  doc
end

#delete(doc) ⇒ Object



165
166
167
168
169
# File 'lib/mongomodel/concerns/associations/has_many_by_ids.rb', line 165

def delete(doc)
  super if loaded?
  ids.delete(doc.id)
  self
end

#delete_at(index) ⇒ Object



171
172
173
174
175
# File 'lib/mongomodel/concerns/associations/has_many_by_ids.rb', line 171

def delete_at(index)
  super if loaded?
  ids.delete_at(index)
  self
end

#delete_if(&block) ⇒ Object



177
178
179
180
181
# File 'lib/mongomodel/concerns/associations/has_many_by_ids.rb', line 177

def delete_if(&block)
  super
  ids.replace(map { |doc| doc.id })
  self
end

#idsObject



183
184
185
# File 'lib/mongomodel/concerns/associations/has_many_by_ids.rb', line 183

def ids
  association.ids
end

#insert(index, doc) ⇒ Object



131
132
133
134
135
136
# File 'lib/mongomodel/concerns/associations/has_many_by_ids.rb', line 131

def insert(index, doc)
  ensure_class(doc)
  super if loaded?
  ids.insert(index, doc.id)
  self
end

#push(*documents) ⇒ Object



145
146
147
148
149
150
# File 'lib/mongomodel/concerns/associations/has_many_by_ids.rb', line 145

def push(*documents)
  ensure_class(documents)
  super if loaded?
  ids.push(*documents.map { |doc| doc.id })
  self
end

#replace(documents) ⇒ Object



138
139
140
141
142
143
# File 'lib/mongomodel/concerns/associations/has_many_by_ids.rb', line 138

def replace(documents)
  ensure_class(documents)
  super if loaded?
  ids.replace(documents.map { |doc| doc.id })
  self
end

#unshift(*documents) ⇒ Object



152
153
154
155
156
157
# File 'lib/mongomodel/concerns/associations/has_many_by_ids.rb', line 152

def unshift(*documents)
  ensure_class(documents)
  super if loaded?
  ids.unshift(*documents.map { |doc| doc.id })
  self
end