Class: CLIntegracon::LazyStringProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/CLIntegracon/formatter.rb

Overview

A LazyStringProxy returns a LazyString for each call, which delegates the call as soon as the result is needed to the underlying formatter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(formatter) ⇒ LazyStringProxy

Initialize a LazyStringProxy, which returns for each call to an underlying formatter a new LazyString, whose #to_s method will evaluate to the result of the original call delegated to the formatter.

Parameters:



50
51
52
# File 'lib/CLIntegracon/formatter.rb', line 50

def initialize(formatter)
  @formatter = formatter
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ #to_s

Remember the call delegated to #formatter in a closure on an anonymous object, defined as method :to_s.

Returns:

  • (#to_s)


59
60
61
62
63
# File 'lib/CLIntegracon/formatter.rb', line 59

def method_missing(method, *args, &block)
  return LazyString.new do
    @formatter.send(method, *args, &block)
  end
end

Instance Attribute Details

#formatterFormatter (readonly)

Returns the formatter used to build the string.

Returns:

  • (Formatter)

    the formatter used to build the string



41
42
43
# File 'lib/CLIntegracon/formatter.rb', line 41

def formatter
  @formatter
end

Instance Method Details

#respond_to?(method) ⇒ Bool

Respond to all methods, which are beginning with ‘describe_` to which the #formatter also responds.

Returns:

  • (Bool)


70
71
72
73
74
75
76
# File 'lib/CLIntegracon/formatter.rb', line 70

def respond_to?(method)
  if /^describe_/.match(method.to_s) && @formatter.respond_to?(method)
    true
  else
    super
  end
end