Sha256: 6866593c20a69cad24ae28d79e343bd18e90c65efdadb3be27052b075631f008

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# encoding: utf-8

module ROM

  module Proxy

    def self.included(descendant)
      descendant.send :undef_method, *descendant.superclass.public_instance_methods(false).map(&:to_s)
      descendant.extend(Constructor)
      super
    end

    module Constructor

      def new(*args)
        proxy = super(*args)
        decorated_object = args.first
        proxy.instance_variable_set '@__decorated_class', decorated_object.class
        proxy.instance_variable_set '@__decorated_object', decorated_object
        proxy.instance_variable_set '@__args', args[1..args.size]
        proxy
      end

    end

    private

    def method_missing(method, *args, &block)
      forwardable?(method) ? forward(method, *args, &block) : super
    end

    def forwardable?(method)
      @__decorated_object.respond_to?(method)
    end

    def forward(*args, &block)
      response = @__decorated_object.public_send(*args, &block)

      if response.equal?(@__decorated_object)
        self
      elsif response.kind_of?(@__decorated_class)
        self.class.new(response, *@__args)
      else
        response
      end
    end

  end # Proxy

end # ROM

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rom-session-0.1.1 lib/rom/support/proxy.rb
rom-session-0.1.0 lib/rom/support/proxy.rb