Sha256: 030e2bb75db6a781eeb30f8f0a5a3d5dfc11ccf4e980f9120e1c59612813dd53

Contents?: true

Size: 1.88 KB

Versions: 12

Compression:

Stored size: 1.88 KB

Contents

module MongoMapper
  module Associations
    class Proxy < BasicObject
      attr_reader :owner, :association
      
      def initialize(owner, association)
        @owner = owner
        @association = association
        @association.options[:extend].each { |ext| class << self; self; end.instance_eval { include ext } }
        reset
      end

      def respond_to?(*methods)
        (load_target && @target.respond_to?(*methods))
      end

      def reset
        @target = nil
      end

      def reload_target
        reset
        load_target
        self
      end

      def send(method, *args)
        metaclass_instance_methods = class << self; self; end.instance_methods
        
        if metaclass_instance_methods.any? { |m| m.to_s == method.to_s }
          return __send__(method, *args)
        end
        
        load_target
        @target.send(method, *args)
      end

      def replace(v)
        raise NotImplementedError
      end
      
      def inspect
        load_target
        @target.inspect
      end
      
      def nil?
        load_target
        @target.nil?
      end
      
      protected
        def method_missing(method, *args, &block)
          if load_target
            if block.nil?
              @target.send(method, *args)
            else
              @target.send(method, *args)  { |*block_args| block.call(*block_args) }
            end
          end
        end

        def load_target
          @target ||= find_target
        end

        def find_target
          raise NotImplementedError
        end
        
        # Array#flatten has problems with recursive arrays. Going one level
        # deeper solves the majority of the problems.
        def flatten_deeper(array)
          array.collect do |element|
            (element.respond_to?(:flatten) && !element.is_a?(Hash)) ? element.flatten : element
          end.flatten
        end
    end
  end
end

Version data entries

12 entries across 12 versions & 4 rubygems

Version Path
mongo_mapper-0.6.6 lib/mongo_mapper/associations/proxy.rb
mongo_mapper-unstable-2009.12.4 lib/mongo_mapper/associations/proxy.rb
danielharan-mongo_mapper-0.6.5 lib/mongo_mapper/associations/proxy.rb
mongo_mapper-0.6.5 lib/mongo_mapper/associations/proxy.rb
webbynode-mongo_mapper-0.6.4 lib/mongo_mapper/associations/proxy.rb
mongo_mapper-0.6.4 lib/mongo_mapper/associations/proxy.rb
mongo_mapper-0.6.3 lib/mongo_mapper/associations/proxy.rb
mongo_mapper-0.6.2 lib/mongo_mapper/associations/proxy.rb
mongo_mapper-0.6.1 lib/mongo_mapper/associations/proxy.rb
mongo_mapper-unstable-2009.11.18 lib/mongo_mapper/associations/proxy.rb
mongo_mapper-0.6.0 lib/mongo_mapper/associations/proxy.rb
mongo_mapper-unstable-2009.11.8 lib/mongo_mapper/associations/proxy.rb