Class: EagerBeaver::MethodHandler
- Inherits:
-
Object
- Object
- EagerBeaver::MethodHandler
- Defined in:
- lib/eager_beaver/method_handler.rb
Instance Attribute Summary collapse
-
#handle ⇒ Object
Returns the value of attribute handle.
-
#match ⇒ Object
Returns the value of attribute match.
-
#missing_method_name ⇒ Object
Returns the value of attribute missing_method_name.
-
#original_receiver ⇒ Object
Returns the value of attribute original_receiver.
Instance Method Summary collapse
- #evaluate(inner) ⇒ Object
- #handles?(method_name) ⇒ Boolean
-
#initialize(&block) ⇒ MethodHandler
constructor
A new instance of MethodHandler.
- #method_missing(method_name, *args, &block) ⇒ Object
Constructor Details
#initialize(&block) ⇒ MethodHandler
Returns a new instance of MethodHandler.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/eager_beaver/method_handler.rb', line 10 def initialize(&block) block.call(self) raise "match must be given" \ if match.nil? raise "match must be a lambda" \ unless match.lambda? raise "handle must be given" \ if handle.nil? raise "handle must be a lambda" \ unless handle.lambda? self end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/eager_beaver/method_handler.rb', line 39 def method_missing(method_name, *args, &block) if /\A(?<attr_name>[a-zA-Z]\w*)=?\z/ =~ method_name code = %Q{ attr_accessor :#{attr_name} } self.singleton_class.instance_eval code, __FILE__, __LINE__ + 1 return self.send(method_name, *args, &block) end super end |
Instance Attribute Details
#handle ⇒ Object
Returns the value of attribute handle.
8 9 10 |
# File 'lib/eager_beaver/method_handler.rb', line 8 def handle @handle end |
#match ⇒ Object
Returns the value of attribute match.
7 8 9 |
# File 'lib/eager_beaver/method_handler.rb', line 7 def match @match end |
#missing_method_name ⇒ Object
Returns the value of attribute missing_method_name.
6 7 8 |
# File 'lib/eager_beaver/method_handler.rb', line 6 def missing_method_name @missing_method_name end |
#original_receiver ⇒ Object
Returns the value of attribute original_receiver.
5 6 7 |
# File 'lib/eager_beaver/method_handler.rb', line 5 def original_receiver @original_receiver end |
Instance Method Details
#evaluate(inner) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/eager_beaver/method_handler.rb', line 31 def evaluate(inner) outer = lambda { |*args| args.shift inner.call(*args) } self.instance_eval &outer end |
#handles?(method_name) ⇒ Boolean
26 27 28 29 |
# File 'lib/eager_beaver/method_handler.rb', line 26 def handles?(method_name) self.missing_method_name = method_name.to_s return evaluate(match) end |