Class: Tenacity::AssociatesProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/tenacity/associates_proxy.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(parent, target, association) ⇒ AssociatesProxy



6
7
8
9
10
# File 'lib/tenacity/associates_proxy.rb', line 6

def initialize(parent, target, association)
  @parent = parent
  @target = target
  @association = association
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



59
60
61
62
63
64
65
# File 'lib/tenacity/associates_proxy.rb', line 59

def method_missing(method, *args)
  if block_given?
    @target.send(method, *args) { |*block_args| yield(*block_args) }
  else
    @target.send(method, *args)
  end
end

Instance Method Details

#<<(object) ⇒ Object



21
22
23
24
# File 'lib/tenacity/associates_proxy.rb', line 21

def <<(object)
  object._t_save_if_dirty unless @parent.id.nil?
  @target << AssociateProxy.new(object, @association)
end

#===(other) ⇒ Object

Explicitly proxy === because the instance method removal above doesn’t catch it.



17
18
19
# File 'lib/tenacity/associates_proxy.rb', line 17

def ===(other)
  other === @target
end

#concat(objects) ⇒ Object



32
33
34
35
36
# File 'lib/tenacity/associates_proxy.rb', line 32

def concat(objects)
  objects.each { |object| object._t_save_if_dirty } unless @parent.id.nil?
  proxies = objects.map { |object| AssociateProxy.new(object, @association) }
  @target.concat(proxies)
end

#delete_allObject



43
44
45
46
# File 'lib/tenacity/associates_proxy.rb', line 43

def delete_all
  remove_associates_from_parent
  @association.associate_class._t_delete(@parent._t_get_associate_ids(@association), false)
end

#destroy_allObject



38
39
40
41
# File 'lib/tenacity/associates_proxy.rb', line 38

def destroy_all
  remove_associates_from_parent
  @association.associate_class._t_delete(@parent._t_get_associate_ids(@association))
end

#inspectObject



48
49
50
# File 'lib/tenacity/associates_proxy.rb', line 48

def inspect
  @target.inspect
end

#proxy_respond_to?Object



3
# File 'lib/tenacity/associates_proxy.rb', line 3

alias_method :proxy_respond_to?, :respond_to?

#push(*objects) ⇒ Object



26
27
28
29
30
# File 'lib/tenacity/associates_proxy.rb', line 26

def push(*objects)
  objects.each { |object| object._t_save_if_dirty } unless @parent.id.nil?
  proxies = objects.map { |object| AssociateProxy.new(object, @association) }
  @target.push(*proxies)
end

#respond_to?(*args) ⇒ Boolean



12
13
14
# File 'lib/tenacity/associates_proxy.rb', line 12

def respond_to?(*args)
  proxy_respond_to?(*args) || @target.respond_to?(*args)
end