Module: Hashformer::Generate::Chain::ReceiverMethods

Included in:
BasicReceiver, DebuggableReceiver
Defined in:
lib/hashformer/generate.rb

Overview

Base module that defines methods included by BasicReceiver and DebuggableReceiver.

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Adds a method call or array dereference to the list of calls to apply. Does nothing if #__end has been called. Returns self for more chaining.



75
76
77
78
79
# File 'lib/hashformer/generate.rb', line 75

def method_missing(name, *args, &block)
  @__ended ||= false
  @__chain << {name: name, args: args, block: block} unless @__ended
  self
end

Instance Attribute Details

#__chainObject

An oddly named accessor is used instead of #initialize to avoid conflicts with any methods that might be chained.



70
71
72
# File 'lib/hashformer/generate.rb', line 70

def __chain
  @__chain
end

Instance Method Details

#__as(*args, &block) ⇒ Object

Adds a call to the given block to the chain like Object#tap, but returns the result of the block instead of the original object. Any arguments given will be passed to the block after the current value. Does nothing if #__end has been called. Returns self for more chaining.

This is similar in spirit (but not implementation) to stackoverflow.com/a/12849214



89
90
91
92
93
94
# File 'lib/hashformer/generate.rb', line 89

def __as(*args, &block)
  ::Kernel.raise 'No block given to #__as' unless ::Kernel.block_given?
  @__ended ||= false
  @__chain << {args: args, block: block} unless @__ended
  self
end

#__endObject

Disables further chaining. Any future method calls will just return the existing chain without modifying it.



98
99
100
101
# File 'lib/hashformer/generate.rb', line 98

def __end
  @__ended = true
  self
end