Module: MethodChain

Included in:
Object
Defined in:
lib/methodchain/not-included.rb

Overview

:main: README

Instance Method Summary collapse

Instance Method Details

#chain(*messages, &guard) ⇒ Object

method chaining with a guard. If no guard block is given then guard against nil and false



44
45
46
47
48
49
# File 'lib/methodchain/not-included.rb', line 44

def chain *messages, &guard
  return self if messages.empty? or not(
    (block_given? ? (yield_or_eval(&guard)) : self))

  (send_as_function (messages.shift)).chain(*messages, &guard)
end

#else(arg = nil, &block) ⇒ Object

the inverse of then



63
64
65
66
67
68
69
70
# File 'lib/methodchain/not-included.rb', line 63

def else arg=nil, &block
  if self
    self
  else
    block_given? ? (yield_or_eval(&block)) : (arg || (fail \
      ArgumentError, "#else must be called with an argument or a bloc"))
  end
end

#tap(*messages, &block) ⇒ Object

send messages, evaluate blocks, but always return self



36
37
38
39
40
# File 'lib/methodchain/not-included.rb', line 36

def tap *messages, &block
  send_as_functions *messages unless messages.empty?
  yield_or_eval(&block) if block_given?
  self
end

#then(default = nil, &block) ⇒ Object

return self if self evaluates to false, otherwise evaluate the block or return the default argument



53
54
55
56
57
58
59
60
# File 'lib/methodchain/not-included.rb', line 53

def then default=nil, &block
  if self
    block_given? ? (yield_or_eval(&block)) : (default || (fail \
      ArgumentError, "#then must be called with an argument or a block"))
  else
    self
  end
end