Class: JsDuck::Util::NullObject

Inherits:
Object
  • Object
show all
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

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
# File 'lib/jsduck/util/null_object.rb', line 17

def method_missing(meth, *args, &block)
  @methods.has_key?(meth) ? @methods[meth] : self
end