Class: ActiveZuora::HasManyProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/active_zuora/has_many_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, scope, options = {}) ⇒ HasManyProxy

Returns a new instance of HasManyProxy.


11
12
13
14
15
# File 'lib/active_zuora/has_many_proxy.rb', line 11

def initialize(owner, scope, options={})
  @owner, @scope = owner, scope
  # inverse_of by default. You can opt out with :inverse_of => false
  @inverse_of = (options[:inverse_of] || owner.zuora_object_name.underscore) unless options[:inverse_of] == false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)


37
38
39
40
41
42
43
44
45
# File 'lib/active_zuora/has_many_proxy.rb', line 37

def method_missing(method, *args, &block)
  # If we do anything that needs loading the scope, then we'll load it.
  if Array.method_defined?(method)
    to_a.send(method, *args, &block)
  else
    # Otherwise send all messages to the @scope.
    @scope.send(method, *args, &block)
  end
end

Instance Attribute Details

#ownerObject (readonly)

Wraps around the Relation representing a has_many association to add features like inverse_of loading.


7
8
9
# File 'lib/active_zuora/has_many_proxy.rb', line 7

def owner
  @owner
end

#scopeObject (readonly)

Wraps around the Relation representing a has_many association to add features like inverse_of loading.


7
8
9
# File 'lib/active_zuora/has_many_proxy.rb', line 7

def scope
  @scope
end

Instance Method Details

#reloadObject


28
29
30
31
32
33
# File 'lib/active_zuora/has_many_proxy.rb', line 28

def reload
  # If reload is called directly on the scope, it will reload
  # without our extra functionality, like inverse_of loading.
  @scope.unload
  to_a
end

#to_aObject Also known as: all


17
18
19
20
21
22
23
24
# File 'lib/active_zuora/has_many_proxy.rb', line 17

def to_a
  if @scope.loaded? || !@inverse_of
    @scope.to_a
  else
    @scope.to_a.each { |record| record.send("#{@inverse_of}=", owner) }
    @scope.to_a
  end
end