Sha256: 90099df88394e2eedd148963e46a7bb4f95f7da406cfa88968c24d2ae0f99c83

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 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|
          self.send(name) << objects
        end
      end
    end
    
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dynamoid-0.0.2 lib/dynamoid/associations.rb