Sha256: 2f33a261b9114703e12be097bc541715b4d96414f126289c17d6925e898b643f

Contents?: true

Size: 1.68 KB

Versions: 2

Compression:

Stored size: 1.68 KB

Contents

module Scrooge
  class Callsite
    
    # Represents a Callsite and is a container for any columns and 
    # associations ( coming soon ) referenced at the callsite.
    #
    
    Mutex = Mutex.new
    
    attr_accessor :klass,
                  :signature,
                  :columns,
                  :associations
    
    def initialize( klass, signature )
      @klass = klass
      @signature = signature
      @columns = setup_columns 
      @associations = setup_associations
    end
    
    # Flag a column as seen
    #
    def column!( column )
      Mutex.synchronize do 
        @columns << column
      end
    end
    
    # Flag an association as seen
    #
    def association!( association )
      Mutex.synchronize do
        @associations << association
      end
    end
    
    private
    
      # Is the table a container for STI models ?
      # 
      def inheritable?
        @klass.column_names.include?( inheritance_column )
      end
    
      # Ensure that at least the primary key and optionally the inheritance
      # column ( for STI ) is set. 
      #
      def setup_columns
        if inheritable?
          Set.new([primary_key, inheritance_column])
        else
          Set.new([primary_key])
        end    
      end
    
      # Stubbed for future use
      #
      def setup_associations
        Set.new
      end
    
      # Memoize a string representation of the inheritance column
      #
      def inheritance_column
        @inheritance_column ||= @klass.inheritance_column.to_s
      end

      # Memoize a string representation of the primary
      #    
      def primary_key
        @primary_key ||= @klass.primary_key.to_s
      end    
    
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
methodmissing-scrooge-2.2.1 lib/callsite.rb
methodmissing-scrooge-2.2.2 lib/callsite.rb