class LazyObject

Lazy object wrapper.

Pass a block to the initializer, which returns an instance of the target object. Lazy object forwards all method calls to the target. The block only gets called the first time a method is forwarded.

Example:

lazy = LazyObject.new { VeryExpensiveObject.new } # At this point the VeryExpensiveObject hasn't been initialized yet. lazy.get_expensive_results(foo, bar) # Initializes VeryExpensiveObject and calls 'get_expensive_results' on it, passing in foo and bar

Public Class Methods

new(&callable) click to toggle source
# File lib/lazy_object.rb, line 16
def initialize(&callable)
  @__callable__ = callable
end
version() click to toggle source
# File lib/lazy_object.rb, line 12
def self.version
  '0.0.3'
end

Public Instance Methods

!() click to toggle source
# File lib/lazy_object.rb, line 28
def !
  !__target_object__
end
!=(item) click to toggle source
# File lib/lazy_object.rb, line 24
def !=(item)
  __target_object__ != item
end
==(item) click to toggle source
# File lib/lazy_object.rb, line 20
def ==(item)
  __target_object__ == item
end
__target_object__() click to toggle source

Cached target object.

# File lib/lazy_object.rb, line 33
def __target_object__
  @__target_object__ ||= @__callable__.call
end
method_missing(method_name, *args, &block) click to toggle source

Forwards all method calls to the target object.

# File lib/lazy_object.rb, line 38
def method_missing(method_name, *args, &block)
  __target_object__.send(method_name, *args, &block)
end