Provides the ability to wrap an object model without having specific access to that model.
Wraps each returned object in a new Proxy object.
class Application
def self.open_current
return Current.new()
end
end
class Current
def get_row(row)
Row.new(row)
end
end
class Row
def initialize(row)
@row = row
end
def data()
return {:index=>@row,:data=>[1,2,3]}
end
end
#Extend and replace `Application` model with `ProxyApplication` model
require './Proxy.rb'
class ProxyApplication < ProxyOverride
def self.test()
return true
end
end
class ProxyCurrent < ProxyOverride
def test()
return true
end
def get_row(index)
if UNAUTHORISED
throw "Authorisation Failed. You are unauthorised to execute this method."
else
return @obj.get_row(index)
end
end
end
class ProxyRow < ProxyOverride
def test()
return true
end
end
Proxy.enwrap(Application)
UNAUTHORISED=false
current = Application.open_current()
row = current.get_row(1)
p [Application.class, current.class, row.class, Application.test, current.test, row.test, row.data]
Results now depend on Unauthroised constant. if Unauthorised == true
then an error will be thrown. Otherwise if Unauthorised == false
we get the following results.
[Application, Current, Row, true, true, true, {:index=>1, :data=>[1, 2, 3]}]
Note: Generally speaking the reference to Application
is lost after enwrapping, so if this reference is required at a later date either:
- Modify the library to optionally overwrite the global reference
- Cache the global reference prior to overwriting:
localCache = {:app=>Application}
Proxy.enwrap(Application)