Sha256: d907861dfe7fbdc8a7524d89f160d93e490bfa048329122eb94a4acb06562a74

Contents?: true

Size: 1.53 KB

Versions: 5

Compression:

Stored size: 1.53 KB

Contents

module GreyscaleRecord
  module Associatable
    extend ActiveSupport::Concern

    included do
      class_attribute :__associations
      self.__associations = {}

      attr_accessor :_cached_associations

      class << self

        def belongs_to( name, opts = {} )
          association = Associations::BelongsTo.new( self, name, opts )
          process association
          index association.key
        end

        def has_one( name, opts = {} )
          process Associations::HasOne.new( self, name, opts )
        end

        def has_many( name, opts = {} )
          process Associations::HasMany.new( self, name, opts )
        end
        
        protected

        def process( association )
          associate association
          methodize association
        end

        def associate( association )
          self.__associations = __associations.merge association.name => association
        end

        def methodize( association )
          define_method association.name do
            begin
              self._cached_associations ||= {}

              unless self._cached_associations.has_key? association.name
                result = association.klass( self ).send( association.action, association.query( self ) )
                
                self._cached_associations[association.name] = result
              end
              self._cached_associations[association.name]
            rescue GreyscaleRecord::Errors::RecordNotFound => e
              nil
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
greyscale_record-1.0.3 lib/greyscale_record/associatable.rb
greyscale_record-1.0.2 lib/greyscale_record/associatable.rb
greyscale_record-1.0.1 lib/greyscale_record/associatable.rb
greyscale_record-1.0.0 lib/greyscale_record/associatable.rb
greyscale_record-0.0.1 lib/greyscale_record/associatable.rb