Sha256: 0004a685a57beec02e45abb488876832ea8ce80791c10b51273535d86053a879

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 KB

Contents

# encoding: utf-8
module Dynamoid #:nodoc:

  # The belongs_to association. For belongs_to, we reference only a single target instead of multiple records; that target is the 
  # object to which the association object is associated.
  module Associations
    class BelongsTo
      include SingleAssociation

      private

      # Find the target association, either has_many or has_one. Uses either options[:inverse_of] or the source class name and default parsing to
      # return the most likely name for the target association.
      #
      # @since 0.2.0
      def target_association
        has_many_key_name = options[:inverse_of] || source.class.to_s.underscore.pluralize.to_sym
        has_one_key_name = options[:inverse_of] || source.class.to_s.underscore.to_sym
        if !target_class.associations[has_many_key_name].nil?
          return has_many_key_name if target_class.associations[has_many_key_name][:type] == :has_many
        end

        if !target_class.associations[has_one_key_name].nil?
          return has_one_key_name if target_class.associations[has_one_key_name][:type] == :has_one
        end
      end
      
      # Associate a source object to this association.
      #
      # @since 0.2.0
      def associate_target(object)
        object.update_attribute(target_attribute, target_ids.merge(Array(source.hash_key)))
      end

      # Disassociate a source object from this association.
      #
      # @since 0.2.0      
      def disassociate_target(object)
        source.update_attribute(source_attribute, target_ids - Array(source.hash_key))
      end
    end
  end
  
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
dynamoid-1.3.4 lib/dynamoid/associations/belongs_to.rb
dynamoid-1.3.3 lib/dynamoid/associations/belongs_to.rb
dynamoid-1.3.2 lib/dynamoid/associations/belongs_to.rb
dynamoid-1.3.1 lib/dynamoid/associations/belongs_to.rb
dynamoid-1.3.0 lib/dynamoid/associations/belongs_to.rb
synamoid-1.2.1 lib/dynamoid/associations/belongs_to.rb