Last active
August 29, 2015 14:14
-
-
Save kwstannard/d4ea4a22b4cc1622d9d5 to your computer and use it in GitHub Desktop.
Ruby proxy method
This file contains hidden or 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 proxy(context, &blk) | |
@context = context | |
@blk = blk | |
end | |
def method_missing(method, *args, &blk) | |
__object.send(method, *args, &blk) | |
end | |
def __object | |
@object ||= @blk.call(@context) | |
end | |
end | |
p = Proxy.new | |
p.proxy(self) {|context| context.to_s } | |
p + 'HelloWorld' # => 'mainHelloWorld' | |
factory :user do | |
post Proxy.new.proxy(self) {|self| self.build :post } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment