Class: BlankSlate
Overview
BlankSlate provides an abstract base class with no predefined methods (except for _\send_
and _\id_
). BlankSlate is useful as a base class when writing classes that depend upon method_missing
(e.g. dynamic proxies).
Class Method Summary collapse
- .find_hidden_method(name) ⇒ Object
-
.hide(name) ⇒ Object
Hide the method named
name
in the BlankSlate class. -
.reveal(name) ⇒ Object
Redefine a previously hidden method so that it may be called on a blank slate object.
Instance Method Summary collapse
-
#initialize(parent) ⇒ BlankSlate
constructor
A new instance of BlankSlate.
- #method_missing(method_id, *arguments, &block) ⇒ Object
Constructor Details
#initialize(parent) ⇒ BlankSlate
Returns a new instance of BlankSlate.
56 57 58 |
# File 'lib/grifter/blankslate.rb', line 56 def initialize(parent) @parent = parent end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *arguments, &block) ⇒ Object
60 61 62 |
# File 'lib/grifter/blankslate.rb', line 60 def method_missing(method_id, *arguments, &block) @parent.send(method_id, *arguments, &block) end |
Class Method Details
.find_hidden_method(name) ⇒ Object
40 41 42 43 |
# File 'lib/grifter/blankslate.rb', line 40 def find_hidden_method(name) @hidden_methods ||= {} @hidden_methods[name] || superclass.find_hidden_method(name) end |
.hide(name) ⇒ Object
Hide the method named name
in the BlankSlate class. Don’t hide instance_eval
or any method beginning with “__”.
30 31 32 33 34 35 36 37 38 |
# File 'lib/grifter/blankslate.rb', line 30 def hide(name) methods = instance_methods.map(&:to_sym) if methods.include?(name.to_sym) and name !~ /^(__|instance_eval)/ @hidden_methods ||= {} @hidden_methods[name.to_sym] = instance_method(name) undef_method name end end |
.reveal(name) ⇒ Object
Redefine a previously hidden method so that it may be called on a blank slate object.
47 48 49 50 51 |
# File 'lib/grifter/blankslate.rb', line 47 def reveal(name) hidden_method = find_hidden_method(name) fail "Don't know how to reveal method '#{name}'" unless hidden_method define_method(name, hidden_method) end |