Class: Lookout::Stub
Overview
Object that only responds to a given set of methods and returns itself whenever it receives a method call it doesn’t know anything about.
Direct Known Subclasses
Class Method Summary collapse
-
.define(meta, method, value) ⇒ Object
Defines METHOD on an objects META object to VALUE.
Instance Method Summary collapse
-
#initialize(methods = {}) ⇒ Stub
constructor
Maps each method name key in METHODS to its value definition.
- #inspect ⇒ Object
Constructor Details
#initialize(methods = {}) ⇒ Stub
Maps each method name key in METHODS to its value definition. If the value of the key is a Proc it’ll be used as the method definition. Otherwise, the method definition will be set up to return VALUE.
38 39 40 41 42 43 44 |
# File 'lib/lookout-3.0/stub.rb', line 38 def initialize(methods = {}) (class << self; self; end).module_eval do methods.each do |name, value| Lookout::Stub.define self, name, value end end unless methods.empty? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ self (private)
Ignores any method calls that it doesn’t know anything about.
54 |
# File 'lib/lookout-3.0/stub.rb', line 54 def method_missing(method, *args) self end |
Class Method Details
.define(meta, method, value) ⇒ Object
Defines METHOD on an objects META object to VALUE. If value is a Proc it’ll be used as a method definition with rules that depend on if whether it’s a lambda or not. Otherwise, the method definition will be set up to return VALUE.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/lookout-3.0/stub.rb', line 14 def define(, method, value) .instance_eval{ case value when Proc if (value.lambda? rescue false) define_method method, value else define_method method do |*args, &block| value.call(*args, &block) end end else define_method method do |*| value end end } end |
Instance Method Details
#inspect ⇒ Object
46 |
# File 'lib/lookout-3.0/stub.rb', line 46 def inspect; 'stub' end |