class ActiveFedora::Associations::AssociationProxy

def flatten_deeper(array)

Going one level deeper solves the majority of the problems.
Array#flatten has problems with recursive arrays before Ruby 1.9.2.
def flatten_deeper(array)
  array.collect { |element| (element.respond_to?(:flatten) && !element.is_a?(Hash)) ? element.flatten : element }.flatten
end

def flatten_deeper(array)

def flatten_deeper(array)
  array.flatten
end

def foreign_key_present

this scenario.
still being a new record). Currently, only +belongs_to+ presents
available for an association without having the object itself (and
Can be overwritten by associations that might have the foreign key
def foreign_key_present
  false
end

def initialize(owner, reflection)

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

def load_target

not reraised. The proxy is \reset and +nil+ is the return value.
ActiveFedora::ObjectNotFoundError is rescued within the method, and it is

+load_target+ unconditionally to get the \target.
If the \target is already \loaded it is just returned. Thus, you can call

which is expected to be provided by descendants.
This method is abstract in the sense that it relies on +find_target+,

Loads the \target if needed and returns it.
def load_target
  return nil unless defined?(@loaded)
  if !loaded? and (!@owner.new_record? || foreign_key_present)
    @target = find_target
  end
  if @target.nil?
    reset
  else
    @loaded = true
    @target
  end
end

def loaded

Asserts the \target has been loaded setting the \loaded flag to +true+.
def loaded
  @loaded = true
end

def loaded?

Has the \target been already \loaded?
def loaded?
  @loaded
end

def method_missing(method, *args)

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

def raise_on_type_mismatch(record)

a sanity check when you are about to assign an associated record.
the kind of the class of the associated objects. Meant to be used as
Raises ActiveFedora::AssociationTypeMismatch unless +record+ is of
def raise_on_type_mismatch(record)
  unless record.is_a?(@reflection.klass) || record.is_a?(@reflection.class_name.constantize)
    message = "#{@reflection.class_name}(##{@reflection.klass.object_id}) expected, got #{record.class}(##{record.class.object_id})"
    raise ActiveFedora::AssociationTypeMismatch, message
  end
end

def reload

Reloads the \target and returns +self+ on success.
def reload
  reset
  load_target
  self unless @target.nil?
end

def reset

Resets the \loaded flag to +false+ and sets the \target to +nil+.
def reset
  @loaded = false
  @target = nil
end

def set_belongs_to_association_for(record)

If the association is polymorphic the type of the owner is also set.
Assigns the ID of the owner to the corresponding foreign key in +record+.
def set_belongs_to_association_for(record)
  unless @owner.new_record?
    record.add_relationship(@reflection.options[:property], @owner)
  end
end

def target

Returns the target of this proxy, same as +proxy_target+.
def target
  @target
end

def target=(target)

Sets the target of this proxy to \target, and the \loaded flag to +true+.
def target=(target)
  @target = target
  loaded
end