Class: Zoidberg::Lazy

Inherits:
BasicObject
Defined in:
lib/zoidberg/lazy.rb

Overview

Provide lazy evaluation of an instance for future work

Instance Method Summary collapse

Constructor Details

#initialize(klass = nil) { ... } ⇒ self

Create a new lazy evaluator

Parameters:

  • klass (Class) (defaults to: nil)

    optional class to use for type checkin

Yields:

  • block to provide actual instance



13
14
15
16
17
18
19
20
# File 'lib/zoidberg/lazy.rb', line 13

def initialize(klass=nil, &block)
  @klass = klass
  @lock = ::Mutex.new
  unless(block)
    ::Kernel.raise ::ArgumentError.new('Block is required for providing instance!')
  end
  @instance_block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object

Proxy any calls to actual instance



23
24
25
# File 'lib/zoidberg/lazy.rb', line 23

def method_missing(*args, &block)
  _lazy_instance.send(*args, &block)
end

Instance Method Details

#is_a?(chk) ⇒ TrueClass, FalseClass

Customized check to allow for type checking prior to instance being available via lazy loading

Parameters:

  • chk (Class)

Returns:

  • (TrueClass, FalseClass)


32
33
34
35
36
37
38
# File 'lib/zoidberg/lazy.rb', line 32

def is_a?(chk)
  if(@klass)
    ([@klass] + @klass.ancestors).include?(chk)
  else
    method_missing(:is_a?, chk)
  end
end