Class: CattrProxy
Instance Method Summary collapse
-
#initialize(host) ⇒ CattrProxy
constructor
A new instance of CattrProxy.
- #method_missing(key, value = nil) ⇒ Object
Constructor Details
#initialize(host) ⇒ CattrProxy
Returns a new instance of CattrProxy.
2 3 4 |
# File 'lib/lib/proxy.rb', line 2 def initialize host @host = host end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key, value = nil) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/lib/proxy.rb', line 6 def method_missing key, value = nil name = '@cattr_%s' % key if name.sub!(/=$/, '') @host.instance_variable_set name, value else unless value.nil? raise ArgumentError, 'Plese use setter cattr.%s= to set argument' % key end for el in @host.ancestors if el.respond_to?(:superclass) && el != Object && el.instance_variable_defined?(name) local = el.instance_variable_get name if local === Proc local = @host.instance_exec &local end return local end end raise ArgumentError, 'Cattr class variable "cattr.%s" not defined on "%s".' % [name.sub('@cattr_', ''), @host] end end |