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.



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

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)



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

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.



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

def loaded
  @loaded = true
end

#loaded?Boolean

Has the target been already loaded?

Returns:

  • (Boolean)


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

def loaded?
  @loaded
end

#reloadObject

Reloads the target and returns self on success.



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

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

#resetObject

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



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

def reset
  @loaded = false
  @target = nil
end

#targetObject

Returns the target of this proxy, same as proxy_target.



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

def target
  @target
end

#target=(target) ⇒ Object

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



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

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