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
-
#initialize(klass = nil) { ... } ⇒ self
constructor
Create a new lazy evaluator.
-
#is_a?(chk) ⇒ TrueClass, FalseClass
Customized check to allow for type checking prior to instance being available via lazy loading.
-
#method_missing(*args, &block) ⇒ Object
Proxy any calls to actual instance.
Constructor Details
#initialize(klass = nil) { ... } ⇒ self
Create a new lazy evaluator
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
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 |