Class: Manana

Inherits:
Object
  • Object
show all
Defined in:
lib/manana.rb,
lib/manana/version.rb

Overview

Manana lets you defer the initialization of an object until its methods are called.

Examples:

basic usage - see 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]
}

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Note:

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

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

Examples:

calling a wrapped object - see samples/self_healing.rb

weather = client.city_weather("02201")


40
41
42
# File 'lib/manana.rb', line 40

def method_missing(method, *args, &block)
  _object.send(method, *args, &block);
end

Class Method Details

.wrap(&initialization_block) ⇒ Deferrable

wraps the initialization of an object so that it can be deferred to a later time when object methods are called.

Examples:

wrap an object - see samples/self_healing.rb

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

Parameters:

  • initialization_block (Proc)

    object initialization. the block must return the object to be wrapped.

Returns:

  • (Deferrable)

    a wrapped version of the object.



29
30
31
# File 'lib/manana.rb', line 29

def self.wrap(&initialization_block)
  new(&initialization_block)
end

Instance Method Details

#_objectObject

allows direct access to the wrapped object -- useful for debugging

Returns:

  • (Object)

    the object being wrapped



46
47
48
49
50
# File 'lib/manana.rb', line 46

def _object 
  @mutex.synchronize do 
    @object ||= @deferred_initialization.call
  end
end