Class: JsDuck::NullObject

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/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.



11
12
13
# File 'lib/jsduck/null_object.rb', line 11

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



15
16
17
# File 'lib/jsduck/null_object.rb', line 15

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