public
Rubygem
Description: ObjectProxy provides a proxied interface to Ruby objects. It lets you add methods to objects that don't normally support them.
Homepage: http://6brand.com
Clone URL: git://github.com/JackDanger/object_proxy.git
Search Repo:
object_proxy / lib / object_proxy.rb
100644 40 lines (28 sloc) 0.808 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
$:.unshift File.dirname(__FILE__)
 
require 'object_proxy_safe_hash'
 
class ObjectProxy
  
  VERSION = '1.0.2'
 
  SAFE_METHODS = [:__id__, :__send__, :nil, :nil?, :send, :send!, :proxy_class, :proxy_respond_to?]
  
  alias_method :proxy_class, :class
  alias_method :proxy_respond_to?, :respond_to?
  
  instance_methods.each do |method|
    undef_method method unless SAFE_METHODS.include?(method.to_sym)
  end
  
  def initialize(target)
    @target = target
  end
  
  def is_object_proxy?
    true
  end
  
  def target
    @target
  end
  
  def respond_to?(*args)
    proxy_respond_to?(*args) || @target.respond_to?(*args)
  end
 
  protected
 
    # delegate nearly all method calls to the @target object
    def method_missing(method, *args, &block)
      @target.send(method, *args, &block)
    end
end