Class: MongoModel::Associations::HasManyByForeignKey::Proxy

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

Constant Summary collapse

OVERRIDE_METHODS =

Pass these methods to the scope rather than the Array target

[ :find, :first, :last, :count, :paginate ]

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, *args, &block) ⇒ Object (private)



201
202
203
204
205
206
207
208
209
# File 'lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb', line 201

def method_missing(method, *args, &block)
  if Array.method_defined?(method) && !OVERRIDE_METHODS.include?(method)
    target.send(method, *args, &block)
  elsif association.scoped.respond_to?(method)
    association.scoped.send(method, *args, &block)
  else
    super(method, *args, &block)
  end
end

Instance Method Details

#<<(*documents) ⇒ Object Also known as: push, concat



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

def <<(*documents)
  documents.flatten!
  ensure_class(documents)
  documents.each { |doc| association.assign(doc) }
  super if loaded?
  self
end

#[]=(index, doc) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb', line 143

def []=(index, doc)
  ensure_class(doc)
  association.unset(target[index]) if target[index]
  association.assign(doc)
  super if loaded?
  self
end

#build(*args) ⇒ Object



122
123
124
125
126
127
# File 'lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb', line 122

def build(*args)
  association.build(*args) do |doc|
    self << doc
    yield doc if block_given?
  end
end

#create(*args) ⇒ Object



129
130
131
132
133
134
# File 'lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb', line 129

def create(*args)
  association.create(*args) do |doc|
    self << doc
    yield doc if block_given?
  end
end

#create!(*args) ⇒ Object



136
137
138
139
140
141
# File 'lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb', line 136

def create!(*args)
  association.create!(*args) do |doc|
    self << doc
    yield doc if block_given?
  end
end

#delete(doc) ⇒ Object



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

def delete(doc)
  association.unset(doc)
  super
  self
end

#delete_at(index) ⇒ Object



182
183
184
185
186
# File 'lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb', line 182

def delete_at(index)
  association.unset(target[index])
  super
  self
end

#idsObject



188
189
190
# File 'lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb', line 188

def ids
  target.map { |doc| doc.id }
end

#insert(index, doc) ⇒ Object



162
163
164
165
166
167
# File 'lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb', line 162

def insert(index, doc)
  ensure_class(doc)
  association.assign(doc)
  super if loaded?
  self
end

#select(*args, &block) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb', line 192

def select(*args, &block)
  if args.empty?
    target.select(&block)
  else
    association.scoped.send(:select, *args)
  end
end

#unshift(*documents) ⇒ Object



169
170
171
172
173
174
# File 'lib/mongomodel/concerns/associations/has_many_by_foreign_key.rb', line 169

def unshift(*documents)
  ensure_class(documents)
  documents.each { |doc| association.assign(doc) }
  super if loaded?
  self
end