Sha256: b4407cb9b9b4f2c1ad87042f742cd1d5f1f467622c642e2611d9d2fe34e50804

Contents?: true

Size: 1.26 KB

Versions: 6

Compression:

Stored size: 1.26 KB

Contents

module Her
  module Model
    module Associations
      class AssociationProxy < (ActiveSupport.const_defined?('ProxyObject') ? ActiveSupport::ProxyObject : ActiveSupport::BasicObject)

        # @private
        def self.install_proxy_methods(target_name, *names)
          names.each do |name|
            module_eval <<-RUBY, __FILE__, __LINE__ + 1
              def #{name}(*args, &block)
                #{target_name}.#{name}(*args, &block)
              end
            RUBY
          end
        end

        install_proxy_methods :association,
          :build, :create, :where, :find, :all, :assign_nested_attributes

        # @private
        def initialize(association)
          @_her_association = association
        end

        def association
          @_her_association
        end

        # @private
        def method_missing(name, *args, &block)
          if :object_id == name # avoid redefining object_id
            return association.fetch.object_id
          end

          # create a proxy to the fetched object's method
          metaclass = (class << self; self; end)
          metaclass.install_proxy_methods 'association.fetch', name

          # resend message to fetched object
          __send__(name, *args, &block)
        end

      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
her-0.7.4 lib/her/model/associations/association_proxy.rb
herr-0.7.3 lib/her/model/associations/association_proxy.rb
her-0.7.3 lib/her/model/associations/association_proxy.rb
her-0.7.2 lib/her/model/associations/association_proxy.rb
her-0.7.1 lib/her/model/associations/association_proxy.rb
her-0.7 lib/her/model/associations/association_proxy.rb