class Manana

Manana lets you defer the initialization of an object until its methods are called. @example basic usage - see {github.com/coldnebo/manana/blob/master/samples/self_healing.rb samples/self_healing.rb}

#   initialization...
client = Manana.wrap {
  Weather.setup
  Weather
}

runtime_loop {
  #   wait for next interval
  weather = client.city_weather("02201")  # deferred initialization happens here once
  puts "At %s the temperature is currently %s F and the humidity is %s." % [weather.city, weather.temperature, weather.relative_humidity]
}

Constants

VERSION

Public Class Methods

new(&initialization_block) click to toggle source
# File lib/manana.rb, line 54
def initialize(&initialization_block)
  @mutex = ::Mutex.new
  @deferred_initialization = initialization_block
end
wrap(&initialization_block) click to toggle source

wraps the initialization of an object so that it can be deferred to a later time when object methods are called. @example wrap an object - see {github.com/coldnebo/manana/blob/master/samples/self_healing.rb samples/self_healing.rb}

client = Manana.wrap {
  Weather.setup  # initialize the class
  Weather        # return the Weather class
}

@param initialization_block [Proc] object initialization. the block must return the object to be wrapped. @return [Deferrable] a wrapped version of the object.

# File lib/manana.rb, line 29
def self.wrap(&initialization_block)
  new(&initialization_block)
end

Public Instance Methods

_object() click to toggle source

allows direct access to the wrapped object – useful for debugging @return [Object] the object being wrapped

# File lib/manana.rb, line 46
def _object 
  @mutex.synchronize do 
    @object ||= @deferred_initialization.call
  end
end
method_missing(method, *args, &block) click to toggle source

passes any method call through to the wrapped object after ensuring that the initialization block has successfully completed once (thereby initializing the wrapped object).

@note Once the initialization block succeeds, it keeps the resulting object for subsequent method calls.

@example calling a wrapped object - see {github.com/coldnebo/manana/blob/master/samples/self_healing.rb samples/self_healing.rb}

weather = client.city_weather("02201")
# File lib/manana.rb, line 40
def method_missing(method, *args, &block)
  _object.send(method, *args, &block);
end