Class: JsDuck::Util::NullObject
- Inherits:
-
Object
- Object
- JsDuck::Util::NullObject
- Defined in:
- lib/jsduck/util/null_object.rb
Overview
A class that does nothing. Responds to all methods by returning self, unless a hash passed to constructor. See: en.wikipedia.org/wiki/Null_Object_pattern
Instance Method Summary collapse
-
#initialize(methods = {}) ⇒ NullObject
constructor
Optionally takes a hash of method_name => return_value pairs, making it return those values for those methods, sort of like OpenStruct, but for all other methods self is still returned and any number of arguments is accepted.
- #method_missing(meth, *args, &block) ⇒ Object
- #respond_to?(meth) ⇒ Boolean
Constructor Details
#initialize(methods = {}) ⇒ NullObject
Optionally takes a hash of method_name => return_value pairs, making it return those values for those methods, sort of like OpenStruct, but for all other methods self is still returned and any number of arguments is accepted.
13 14 15 |
# File 'lib/jsduck/util/null_object.rb', line 13 def initialize(methods={}) @methods = methods end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/jsduck/util/null_object.rb', line 17 def method_missing(meth, *args, &block) if @methods.has_key?(meth) value = @methods[meth] if value.respond_to?(:call) value.call(*args, &block) else value end else self end end |
Instance Method Details
#respond_to?(meth) ⇒ Boolean
30 31 32 |
# File 'lib/jsduck/util/null_object.rb', line 30 def respond_to?(meth) @methods.has_key?(meth) end |