Class: MaybeDelegator

Inherits:
Object show all
Defined in:
lib/mug/maybe.rb

Overview

Invokes methods on a wrapped object, if that object is truthy.

Instance Method Summary collapse

Constructor Details

#initialize(o) ⇒ MaybeDelegator

Creates a new MaybeDelegator, wrapping o

Parameters:

  • o (Object)

    the object to wrap



12
13
14
# File 'lib/mug/maybe.rb', line 12

def initialize o
  @o = o
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*a, &b) ⇒ Object

Calls the method on @o if it's truthy.



26
27
28
# File 'lib/mug/maybe.rb', line 26

def method_missing *a, &b #:nodoc:
  @o && @o.send(*a, &b)
end

Instance Method Details

#maybeMaybeDelegator

Returns this MaybeDelegator object.

Returns:



21
22
23
# File 'lib/mug/maybe.rb', line 21

def maybe
  self
end

#respond_to_missing?(meth, priv) ⇒ Boolean

This is a bit flakey, but I think it's meaningful.

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'lib/mug/maybe.rb', line 31

def respond_to_missing? meth, priv
  if @o
    @o.repond_to_missing? meth, priv
  else
    true
  end
end