Class: SimpleMapper::Associations::Association::Instance
- Inherits:
-
Object
- Object
- SimpleMapper::Associations::Association::Instance
show all
- Defined in:
- lib/simple_mapper/default_plugins/associations.rb
Direct Known Subclasses
Set
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(instance, association) ⇒ Instance
Returns a new instance of Instance.
274
275
276
277
278
279
|
# File 'lib/simple_mapper/default_plugins/associations.rb', line 274
def initialize(instance, association)
@instance = instance
@association = association
@items = nil
@loaded = false
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
281
282
283
|
# File 'lib/simple_mapper/default_plugins/associations.rb', line 281
def method_missing(method, *args)
(item || items).send(method, *args)
end
|
Instance Attribute Details
#association ⇒ Object
Returns the value of attribute association.
272
273
274
|
# File 'lib/simple_mapper/default_plugins/associations.rb', line 272
def association
@association
end
|
Instance Method Details
#associate(object, associate_other = true) ⇒ Object
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
# File 'lib/simple_mapper/default_plugins/associations.rb', line 293
def associate(object,associate_other=true)
if @association.primary?
@association.primary_options.each do |atr,k|
@instance.send("#{k}=", object.send(atr)) if @instance.respond_to?("#{k}=") && object.respond_to?(atr)
end
end
if @association.foreign?
@association.foreign_options.each do |k,atr|
object.send("#{k}=", @instance.respond_to?(atr) ? @instance.send(atr) : atr) if object.respond_to?("#{k}=")
end
end
if associate_other && @association.mirrored_association
object.association_set(@association.mirrored_association_name).associate(@instance,false)
end
(@items ||= []) << object
end
|
#associate!(object) ⇒ Object
289
290
291
292
|
# File 'lib/simple_mapper/default_plugins/associations.rb', line 289
def associate!(object)
associate(object)
object.save
end
|
#build(options) ⇒ Object
def disassociate(item=nil)
dis_items = item || items
(dis_items.is_a?(Array) ? dis_items : [dis_items]).each do |item|
item.instance_variable_set("@#{@association.foreign_key}", nil) unless item.new_record? || @association.foreign_key == :none
@items = @items - [item]
end
item || items
end
326
327
328
|
# File 'lib/simple_mapper/default_plugins/associations.rb', line 326
def build(options)
associate(@association.associated_klass.new)
end
|
#create(options) ⇒ Object
329
330
331
332
333
|
# File 'lib/simple_mapper/default_plugins/associations.rb', line 329
def create(options)
object = @association.associated_klass.new
associate(object)
object.save
end
|
#dirty? ⇒ Boolean
285
286
287
|
# File 'lib/simple_mapper/default_plugins/associations.rb', line 285
def dirty?
@items && @items.any? { |item| item.dirty? }
end
|
356
357
358
|
# File 'lib/simple_mapper/default_plugins/associations.rb', line 356
def inspect
(item || items).inspect
end
|
352
353
354
|
# File 'lib/simple_mapper/default_plugins/associations.rb', line 352
def item
items.length == 1 ? items[0] : nil
end
|
344
345
346
347
348
349
350
351
|
# File 'lib/simple_mapper/default_plugins/associations.rb', line 344
def items
@items || begin
@items = @association.associated_klass.all(@association.finder_options)
@loaded = true
end
@items
end
|
334
335
336
337
338
|
# File 'lib/simple_mapper/default_plugins/associations.rb', line 334
def reload!
@items = nil
@loaded = false
true
end
|
#respond_to?(symbol) ⇒ Boolean
340
341
342
|
# File 'lib/simple_mapper/default_plugins/associations.rb', line 340
def respond_to?(symbol)
(item || items).respond_to?(symbol) || super
end
|