class ProxyTo::Target

Constants

VALID

Attributes

methods[R]
methods?[R]
pretend[R]
pretend?[R]
target[R]

@!attribute [r] target @return [Symbol]

Public Class Methods

new(target, pretend: false, methods: false) click to toggle source

@param [String, Symbol] target

# File lib/proxy_to/target.rb, line 16
def initialize(target, pretend: false, methods: false)
  @target   = target.to_sym
  @pretend  = pretend
  @methods  = methods

  proxy_methods!
  pretend_methods! if pretend?
  methods_method! if methods?
end

Public Instance Methods

inspect() click to toggle source
# File lib/proxy_to/target.rb, line 26
def inspect
  "ProxyTo::Target(:to => :#{target}, :pretend => #{pretend})"
end

Private Instance Methods

methods_method!() click to toggle source
# File lib/proxy_to/target.rb, line 58
    def methods_method!
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def methods(all = true)
        super | #{target}.methods(all)
      end
      RUBY
    end
pretend_methods!() click to toggle source
# File lib/proxy_to/target.rb, line 48
    def pretend_methods!
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def kind_of?(klass)
        super or #{target}.kind_of?(klass)
      end

      alias is_a? kind_of?
      RUBY
    end
proxy_methods!() click to toggle source

@return [void]

# File lib/proxy_to/target.rb, line 32
    def proxy_methods!
      class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def respond_to?(method_name, include_all = false)
        super or #{target}.respond_to?(method_name, include_all)
      end

      def method_missing(method_name, *args, &block)
        if #{target}.respond_to? method_name
          #{target}.__send__(method_name, *args, &block)
        else
          super
        end
      end
      RUBY
    end