Created
February 3, 2015 15:29
-
-
Save kwstannard/2ae681e98bb10d33795f to your computer and use it in GitHub Desktop.
Ruby Proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Proxy | |
def self.proxy(context, &blk) | |
p = new(context) | |
p.__proxy &blk | |
p | |
end | |
def initialize(context) | |
@context = context | |
end | |
def method_missing(method, *args, &blk) | |
__object.send(method, *args, &blk) | |
end | |
def __proxy &blk | |
@blk = blk | |
end | |
def __object | |
@object ||= @blk.call(@context) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment