Class: SimpleMapper::Associations::Association::Instance

Inherits:
Object
  • Object
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

#associationObject

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)
  # puts "Associating #{@instance.inspect} << #{object.inspect} (#{@association.primary? ? 'Primary' : ''} / #{@association.foreign? ? 'Foreign' : ''})\n#{@association.inspect}"
  # 1) Set the @association.primary attributes to @instance
  if @association.primary?
    # puts "Primary options: #{@association.primary_options.inspect}"
    @association.primary_options.each do |atr,k|
      # puts "\tSetting instance##{k} = #{object.respond_to?(atr) ? object.send(atr) : atr}"
      @instance.send("#{k}=", object.send(atr)) if @instance.respond_to?("#{k}=") && object.respond_to?(atr)
    end
  end
  # 2) Set the @association.foreign attributes to object
  if @association.foreign?
    # puts "Foreign options: #{@association.foreign_options.inspect}"
    @association.foreign_options.each do |k,atr|
      # puts "\tSetting object##{k} = #{@instance.respond_to?(atr) ? @instance.send(atr) : atr}"
      object.send("#{k}=", @instance.respond_to?(atr) ? @instance.send(atr) : atr) if object.respond_to?("#{k}=")
    end
  end
  # 3) If there is a @association.mirrored_association, call object.association_set(@association.mirrored_association_name).associate(@instance,false) on it.
  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

Returns:

  • (Boolean)


285
286
287
# File 'lib/simple_mapper/default_plugins/associations.rb', line 285

def dirty?
  @items && @items.any? { |item| item.dirty? }
end

#inspectObject



356
357
358
# File 'lib/simple_mapper/default_plugins/associations.rb', line 356

def inspect
  (item || items).inspect
end

#itemObject



352
353
354
# File 'lib/simple_mapper/default_plugins/associations.rb', line 352

def item
  items.length == 1 ? items[0] : nil
end

#itemsObject



344
345
346
347
348
349
350
351
# File 'lib/simple_mapper/default_plugins/associations.rb', line 344

def items
  # This will look more like it was made for set
  @items || begin
    @items = @association.associated_klass.all(@association.finder_options)
    @loaded = true
  end
  @items
end

#reload!Object



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

Returns:

  • (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