Bz proxies
Description
Several proxies and it's holder class module Owner This bunch of proxies is just a result of my first steps and experiments with ruby metaprogramming. It has some issues (perfomance and others), so it will be nesessary to fork project if you would like to use it in production.
Simple proxies use
First of all make a require call:
require 'bzproxies'
Then, write your custom proxy (if needed), or just use Proxies::Base
class MyProxy < Proxies::Base #alias Proxy
def additional_methods
"additional from proxy"
end
end
You can use some objects that mimics objects covered by proxies:
class SameFunctionalityClass
include Proxies::Stub
def additional_methods
"additional from stub"
end
end
TODO: create global function creating stub for concrete proxy (as same one in Delegate class).
Than you may use proxy_accessor to specify attribute that will cover some given values with proxy:
class MyProxyOwner
proxy_accessor :test, :proxy => MyProxy
end
owner = MyProxyOwner.new
owner.test = "test"
owner.test.additional # => "additional from proxy"
owner.test.proxy? # => true
owner.test = SameFunctionalityClass.new
owner.test.additional # => "additional from stub"
owner.test.proxy? # => false
Array proxy use
Array proxy is an example of how you can implement your own proxy derived from Proxies::Base. This proxy covers each element with other proxy on initialization, push, unshift, << and other operations:
class MyProxyOwner
proxy_accessor :children, :proxies => [ArrayProxy, MyProxy]
def initialize
self.children = []
end
end
owner = MyProxyOwner.new
owner.children << "some test" << 123 << true
owner.children.map{|i| i.proxy?} # => [true,true,true]
Limitations
- You can't use proxied boolean and nil objects in boolean expressions. Ruby doesn't support weak references so covered object will always return true even if it's target is nil or false.
- To setup object variables in methods use self.
= to stay them covered by proxy.