Skip to content

Instantly share code, notes, and snippets.

@kwstannard
Last active August 29, 2015 14:14
Show Gist options
  • Save kwstannard/d4ea4a22b4cc1622d9d5 to your computer and use it in GitHub Desktop.
Save kwstannard/d4ea4a22b4cc1622d9d5 to your computer and use it in GitHub Desktop.
Ruby proxy method
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