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



9
10
11
# File 'lib/mug/maybe.rb', line 9

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.



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

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

Instance Method Details

#maybeObject

Returns this MaybeDelegator object.



16
17
18
# File 'lib/mug/maybe.rb', line 16

def maybe
  self
end

#respond_to_missing?(meth, priv) ⇒ Boolean

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

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/mug/maybe.rb', line 26

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