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.deferred_init {
  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 =
"0.1.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 instance for subsequent method calls.

passes any method call through to the wrapped object after ensuring that the initialization block has successfully completed once (setting a valid instance of the object).

Examples:

calling a wrapped object - see samples/self_healing.rb

weather = client.city_weather("02201")


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

def method_missing(method, *args, &block)
  instance = safe_get_instance
  instance.send(method, *args, &block);
end

Class Method Details

.deferred_init(&initialization_block) ⇒ Manana

wraps an initialization block 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.deferred_init {
  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:

  • (Manana)

    a wrapped version of the object.



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

def self.deferred_init(&initialization_block)
  Manana.new(&initialization_block)
end