Class: EagerBeaver::MethodMatcher
- Inherits:
-
Object
- Object
- EagerBeaver::MethodMatcher
- Defined in:
- lib/eager_beaver/method_matcher.rb
Instance Attribute Summary collapse
-
#matcher ⇒ Object
Returns the value of attribute matcher.
-
#missing_method_name ⇒ Object
Returns the value of attribute missing_method_name.
-
#new_method_code_maker ⇒ Object
Returns the value of attribute new_method_code_maker.
-
#original_receiver ⇒ Object
Returns the value of attribute original_receiver.
Instance Method Summary collapse
- #evaluate(inner) ⇒ Object
-
#initialize(&block) ⇒ MethodMatcher
constructor
A new instance of MethodMatcher.
- #match=(lambda_proc) ⇒ Object
- #match?(method_name) ⇒ Boolean
- #method_missing(method_name, *args, &block) ⇒ Object
- #new_method_code=(lambda_proc) ⇒ Object
Constructor Details
#initialize(&block) ⇒ MethodMatcher
Returns a new instance of MethodMatcher.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/eager_beaver/method_matcher.rb', line 10 def initialize(&block) block.call(self) raise "matcher must be given" \ if matcher.nil? raise "matcher lmust be a lambda" \ unless matcher.lambda? raise "new_method_code_maker must be given" \ if new_method_code_maker.nil? raise "new_method_code_maker must be a lambda" \ unless new_method_code_maker.lambda? self end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/eager_beaver/method_matcher.rb', line 47 def method_missing(method_name, *args, &block) if /\A(?<attr_name>\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
#matcher ⇒ Object
Returns the value of attribute matcher.
6 7 8 |
# File 'lib/eager_beaver/method_matcher.rb', line 6 def matcher @matcher end |
#missing_method_name ⇒ Object
Returns the value of attribute missing_method_name.
8 9 10 |
# File 'lib/eager_beaver/method_matcher.rb', line 8 def missing_method_name @missing_method_name end |
#new_method_code_maker ⇒ Object
Returns the value of attribute new_method_code_maker.
7 8 9 |
# File 'lib/eager_beaver/method_matcher.rb', line 7 def new_method_code_maker @new_method_code_maker end |
#original_receiver ⇒ Object
Returns the value of attribute original_receiver.
5 6 7 |
# File 'lib/eager_beaver/method_matcher.rb', line 5 def original_receiver @original_receiver end |
Instance Method Details
#evaluate(inner) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/eager_beaver/method_matcher.rb', line 39 def evaluate(inner) outer = lambda { |*args| args.shift inner.call(*args) } self.instance_eval &outer end |
#match=(lambda_proc) ⇒ Object
26 27 28 |
# File 'lib/eager_beaver/method_matcher.rb', line 26 def match=(lambda_proc) self.matcher = lambda_proc end |
#match?(method_name) ⇒ Boolean
30 31 32 33 |
# File 'lib/eager_beaver/method_matcher.rb', line 30 def match?(method_name) self.missing_method_name = method_name.to_s return evaluate(matcher) end |
#new_method_code=(lambda_proc) ⇒ Object
35 36 37 |
# File 'lib/eager_beaver/method_matcher.rb', line 35 def new_method_code=(lambda_proc) self.new_method_code_maker = lambda_proc end |