Class: ActiveFedora::Associations::AssociationProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/active_fedora/associations/association_proxy.rb

Overview

This is the root class of all association proxies:

AssociationProxy
  BelongsToAssociation
  AssociationCollection
    HasManyAssociation

Association proxies in Active Fedora are middlemen between the object that holds the association, known as the @owner, and the actual associated object, known as the @target. The kind of association any proxy is about is available in @reflection. That’s an instance of the class ActiveFedora::Reflection::AssociationReflection.

For example, given

class Blog < ActiveFedora::Base
  has_many :posts
end

blog = Blog.find('changeme:123')

the association proxy in blog.posts has the object in blog as @owner, the collection of its posts as @target, and the @reflection object represents a :has_many macro.

This class has most of the basic instance methods removed, and delegates unknown methods to @target via method_missing. As a corner case, it even removes the class method and that’s why you get

blog.posts.class # => Array

though the object behind blog.posts is not an Array, but an ActiveFedora::Associations::HasManyAssociation.

Direct Known Subclasses

AssociationCollection, BelongsToAssociation

Instance Method Summary collapse

Constructor Details

#initialize(owner, reflection) ⇒ AssociationProxy

Returns a new instance of AssociationProxy.



40
41
42
43
44
45
46
# File 'lib/active_fedora/associations/association_proxy.rb', line 40

def initialize(owner, reflection)
  @owner, @reflection = owner, reflection
  @updated = false
  # reflection.check_validity!
  # Array.wrap(reflection.options[:extend]).each { |ext| proxy_extend(ext) }
  reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/active_fedora/associations/association_proxy.rb', line 101

def method_missing(method, *args)
  if load_target
    unless @target.respond_to?(method)
      message = "undefined method `#{method.to_s}' for \"#{@target}\":#{@target.class.to_s}"
      raise NoMethodError, message
    end

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

Instance Method Details

#loadedObject

Asserts the target has been loaded setting the loaded flag to true.



67
68
69
# File 'lib/active_fedora/associations/association_proxy.rb', line 67

def loaded
  @loaded = true
end

#loaded?Boolean

Has the target been already loaded?

Returns:

  • (Boolean)


62
63
64
# File 'lib/active_fedora/associations/association_proxy.rb', line 62

def loaded?
  @loaded
end

#reloadObject

Reloads the target and returns self on success.



55
56
57
58
59
# File 'lib/active_fedora/associations/association_proxy.rb', line 55

def reload
  reset
  load_target
  self unless @target.nil?
end

#resetObject

Resets the loaded flag to false and sets the target to nil.



49
50
51
52
# File 'lib/active_fedora/associations/association_proxy.rb', line 49

def reset
  @loaded = false
  @target = nil
end

#targetObject

Returns the target of this proxy, same as proxy_target.



72
73
74
# File 'lib/active_fedora/associations/association_proxy.rb', line 72

def target
  @target
end

#target=(target) ⇒ Object

Sets the target of this proxy to \target, and the loaded flag to true.



77
78
79
80
# File 'lib/active_fedora/associations/association_proxy.rb', line 77

def target=(target)
  @target = target
  loaded
end