Sha256: a0239f4f16ffc27b7d8825b37931685d2924d67585b628a3500c3b89a834451f

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

module Scorpion
  module Rails
    module ActiveRecord

      # Adds dependency injection to ActiveRecord::Base model associations.
      module Association
        include Scorpion::Stinger

        # ============================================================================
        # @!group Attributes
        #

        # @!attribute
        # @return [Scorpion] the scorpion serving the association.
          attr_accessor :scorpion
          def scorpion
            @scorpion || owner.scorpion
          end

        #
        # @!endgroup Attributes


        # Make sure we override the methods of child classes as well.
        def self.prepended( base )
          infect base
          super
        end

        # Propagate the module inheritance to all derived classes so that we can
        # always overlay our interception methods on the top-most overriden
        # method.
        def self.infect( klass )
          klass.class_exec do
            def self.inherited( from )
              Scorpion::Rails::ActiveRecord::Association.infect( from )

              super
            end
          end
          overlay( klass )
        end

        # Overlay interception methods on the klass.
        def self.overlay( klass )
          [ :load_target, :target, :reader, :writer, :scope ].each do |method|
            next unless klass.instance_methods.include? method

            mod = Module.new do
              module_eval <<-EOS, __FILE__, __LINE__ + 1
                def #{ method }( *args, &block )
                  sting! super
                end
              EOS
            end

            klass.prepend mod
          end
        end

      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
scorpion-ioc-0.4.0 lib/scorpion/rails/active_record/association.rb