Sha256: 3b74b6cd8d640edb6b409bc53d1d95b38628ceceb641a8b01b0542233cf7f40b

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module DataMapper
  module Associations
    
    class AdvancedHasManyAssociation
      include Enumerable
      
      def initialize(klass, association_name, options)
        @association_name = association_name.to_sym
        @options = options

        # Define the association instance method (i.e. Project#tasks)
        klass.class_eval <<-EOS
          def #{association_name}
            @#{association_name} || (@#{association_name} = HasManyAssociation.new(self, "#{association_name}", #{options.inspect}))
          end
        EOS
      end
      
      def name
        @association_name
      end

      def constant
        @associated_class || @associated_class = if @options.has_key?(:class) || @options.has_key?(:class_name)
          associated_class_name = (@options[:class] || @options[:class_name])
          if associated_class_name.kind_of?(String)
            Kernel.const_get(Inflector.classify(associated_class_name))
          else
            associated_class_name
          end
        else            
          Kernel.const_get(Inflector.classify(association_name))
        end
      end
      
      def foreign_key
        @foreign_key || (@foreign_key = (@options[:foreign_key] || @instance.session.schema[@instance.class].default_foreign_key))
      end

    end
    
    module AdvancedHasMany
      def self.included(base)
        base.extend(ClassMethods)
      end
      
      module ClassMethods
        def advanced_has_many(association_name, options = {})
          database.schema[self].associations << AdvancedHasManyAssociation.new(self, association_name, options)
        end
      end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
datamapper-0.1.1 lib/data_mapper/associations/advanced_has_many_association.rb