Sha256: 6122d9951bc7e727fa9dd0d100fd4fcd695d168fc3bf7588913ccc45ac11b2ad

Contents?: true

Size: 1.56 KB

Versions: 7

Compression:

Stored size: 1.56 KB

Contents

require 'dynamoid/associations/association'
require 'dynamoid/associations/has_many'
require 'dynamoid/associations/belongs_to'
require 'dynamoid/associations/has_one'
require 'dynamoid/associations/has_and_belongs_to_many'

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

  # Connects models together through the magic of associations.
  module Associations
    extend ActiveSupport::Concern
    
    included do
      class_attribute :associations
      
      self.associations = {}
    end

    module ClassMethods
      def has_many(name, options = {})
        association(:has_many, name, options)
      end
      
      def has_one(name, options = {})
        association(:has_one, name, options)
      end
      
      def belongs_to(name, options = {})
        association(:belongs_to, name, options)
      end
      
      def has_and_belongs_to_many(name, options = {})
        association(:has_and_belongs_to_many, name, options)
      end
      
      private
      
      def association(type, name, options = {})
        field "#{name}_ids".to_sym
        self.associations[name] = options.merge(:type => type)
        define_method(name) do
          @associations ||= {}
          @associations[name] ||= Dynamoid::Associations.const_get(type.to_s.camelcase).new(self, name, options)
        end
        define_method("#{name}=".to_sym) do |objects|
          @associations ||= {}
          @associations[name] ||= Dynamoid::Associations.const_get(type.to_s.camelcase).new(self, name, options)
          @associations[name].setter(objects)
        end
      end
    end
    
  end
  
end

Version data entries

7 entries across 7 versions & 1 rubygems

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