Sha256: f3ce55975bfcb2d0ad41ff69692cb856cd1d567a7554cd639ada357276cf2443
Contents?: true
Size: 1.08 KB
Versions: 1
Compression:
Stored size: 1.08 KB
Contents
# 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 class LazyObject < BasicObject def self.version '0.2.0' end def initialize(&callable) @__callable__ = callable end def ==(other) __target_object__ == other end def !=(other) __target_object__ != other end def ! !__target_object__ end # Cached target object. def __target_object__ if defined?(@__target_object__) @__target_object__ else @__target_object__ = @__callable__.call end end # Forwards all method calls to the target object. def method_missing(method_name, ...) __target_object__.send(method_name, ...) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
lazy_object-0.2.0 | lib/lazy_object.rb |