class FragileMethodChain

Invokes a method chain until one method returns a falsy value.

For example:

a._?.b.c.d._!
nested_hash._?[:a][:b][:c]._!

Public Class Methods

new(o, falsy: true) click to toggle source

Creates a FragileMethodChain which will send its first method to o

# File lib/mug/fragile-method-chain.rb, line 14
def initialize o, falsy: true
  @o = o
  @falsy = falsy
end

Public Instance Methods

_!() click to toggle source

Finalises the FragileMethodChain.

The final result will be the first nil or false value returned in the chain, or its end result.

# File lib/mug/fragile-method-chain.rb, line 25
def _!
  @o
end
__defer?() click to toggle source

Return true iff @o is deferable.

# File lib/mug/fragile-method-chain.rb, line 57
def __defer?
  return @o if @falsy
  ! @o.nil?
end
respond_to_missing?(meth, priv) click to toggle source

If the object is resolved, defer. Otherwise, sure, I respond to anything, I guess.

# File lib/mug/fragile-method-chain.rb, line 39
def respond_to_missing? meth, priv
  if __defer?
    @o.respond_to_missing? meth, priv
  else
    true
  end
end