def add_owner(klass, inverse, attribute=nil)
      if inverse.nil? then
        raise ValidationError.new("Owner #{klass.qp} missing dependent attribute for dependent #{qp}")
      end
      logger.debug { "Adding #{qp} owner #{klass.qp}#{' attribute ' + attribute.to_s if attribute} with inverse #{inverse}..." }
      if @owner_prop_hash then
        raise MetadataError.new("Can't add #{qp} owner #{klass.qp} after dependencies have been accessed")
      end
      
      # Detect the owner attribute, if necessary.
      attribute ||= detect_owner_attribute(klass, inverse)
      hash = local_owner_property_hash
      # Guard against a conflicting owner reference attribute.
      if hash[klass] then
        raise MetadataError.new("Cannot set #{qp} owner attribute to #{attribute or 'nil'} since it is already set to #{hash[klass]}")
      end
      # Add the owner class => attribute entry.
      # The attribute is nil if the dependency is unidirectional, i.e. there is an owner class which
      # references this class via a dependency attribute but there is no inverse owner attribute.
      prop = property(attribute) if attribute
      hash[klass] = prop
      # If the dependency is unidirectional, then our job is done.
      if attribute.nil? then
        logger.debug { "#{qp} owner #{klass.qp} has unidirectional dependent attribute #{inverse}." }
        return
      end

      # Bi-directional: add the owner property.
      local_owner_properties << prop
      # Set the inverse if necessary.
      unless prop.inverse then
        set_attribute_inverse(attribute, inverse)
      end
      # Set the owner flag if necessary.
      prop.qualify(:owner) unless prop.owner?

      # Redefine the writer method to issue a warning if the owner is changed.
      rdr, wtr = prop.accessors
      redefine_method(wtr) do |old_wtr|
        lambda do |ref|
          prev = send(rdr)
          send(old_wtr, ref)
          if prev and prev != ref then
            if ref.nil? then
              logger.warn("Unset the #{self} owner #{attribute} #{prev}.")
            elsif ref.key != prev.key then
              logger.warn("Reset the #{self} owner #{attribute} from #{prev} to #{ref}.")
            end
          end
          ref
        end
      end
      logger.debug { "Injected owner change warning into #{qp}.#{attribute} writer method #{wtr}." }
      logger.debug { "#{qp} owner #{klass.qp} attribute is #{attribute} with inverse #{inverse}." }
    end
def add_owner(klass, inverse, attribute=nil)
      if inverse.nil? then
        raise ValidationError.new("Owner #{klass.qp} missing dependent attribute for dependent #{qp}")
      end
      logger.debug { "Adding #{qp} owner #{klass.qp}#{' attribute ' + attribute.to_s if attribute} with inverse #{inverse}..." }
      if @owner_prop_hash then
        raise MetadataError.new("Can't add #{qp} owner #{klass.qp} after dependencies have been accessed")
      end
      
      # Detect the owner attribute, if necessary.
      attribute ||= detect_owner_attribute(klass, inverse)
      hash = local_owner_property_hash
      # Guard against a conflicting owner reference attribute.
      if hash[klass] then
        raise MetadataError.new("Cannot set #{qp} owner attribute to #{attribute or 'nil'} since it is already set to #{hash[klass]}")
      end
      # Add the owner class => attribute entry.
      # The attribute is nil if the dependency is unidirectional, i.e. there is an owner class which
      # references this class via a dependency attribute but there is no inverse owner attribute.
      prop = property(attribute) if attribute
      hash[klass] = prop
      # If the dependency is unidirectional, then our job is done.
      if attribute.nil? then
        logger.debug { "#{qp} owner #{klass.qp} has unidirectional dependent attribute #{inverse}." }
        return
      end

      # Bi-directional: add the owner property.
      local_owner_properties << prop
      # Set the inverse if necessary.
      unless prop.inverse then
        set_attribute_inverse(attribute, inverse)
      end
      # Set the owner flag if necessary.
      prop.qualify(:owner) unless prop.owner?

      # Redefine the writer method to issue a warning if the owner is changed.
      rdr, wtr = prop.accessors
      redefine_method(wtr) do |old_wtr|
        lambda do |ref|
          prev = send(rdr)
          send(old_wtr, ref)
          if prev and prev != ref then
            if ref.nil? then
              logger.warn("Unset the #{self} owner #{attribute} #{prev}.")
            elsif ref.key != prev.key then
              logger.warn("Reset the #{self} owner #{attribute} from #{prev} to #{ref}.")
            end
          end
          ref
        end
      end
      logger.debug { "Injected owner change warning into #{qp}.#{attribute} writer method #{wtr}." }
      logger.debug { "#{qp} owner #{klass.qp} attribute is #{attribute} with inverse #{inverse}." }
    end
Adds the given owner class to this dependent class.
 This method must be called before any dependent attribute is accessed.
 If the attribute is given, then the attribute inverse is set.
 Otherwise, if there is not already an owner attribute, then a new owner attribute is created.
 The name of the new attribute is the lower-case demodulized owner class name.

 @param [Class] the owner class
 @param [Symbol] inverse the owner -> dependent attribute
 @param [Symbol, nil] attribute the dependent -> owner attribute, if known
 @raise [ValidationError] if the inverse is nil