Sha256: d7aa2a5854d91933e6d4f09ed9a4328c9cb64cbcf83c313dcd281bbf7b8c7d47

Contents?: true

Size: 914 Bytes

Versions: 9

Compression:

Stored size: 914 Bytes

Contents

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

  # The habtm association.
  module Associations
    class HasAndBelongsToMany
      include Dynamoid::Associations::Association
      
      def ==(other)
        records == Array(other)
      end
      
      private
      
      def target_association
        key_name = source.class.to_s.pluralize.downcase.to_sym
        guess = target_class.associations[key_name]
        return nil if guess.nil? || guess[:type] != :has_and_belongs_to_many
        key_name
      end
            
      def associate_target(object)
        ids = object.send(target_attribute) || Set.new
        object.update_attribute(target_attribute, ids.merge(Array(source.id)))
      end
      
      def disassociate_target(object)
        ids = object.send(target_attribute) || Set.new
        object.update_attribute(target_attribute, ids - Array(source.id))
      end
    end
  end
  
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dynamoid-0.2.0 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-0.1.2 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-0.1.1 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-0.1.0 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-0.0.7 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-0.0.6 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-0.0.5 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-0.0.4 lib/dynamoid/associations/has_and_belongs_to_many.rb
dynamoid-0.0.3 lib/dynamoid/associations/has_and_belongs_to_many.rb