Class: Lazy::Promise
Overview
:nodoc:
Direct Known Subclasses
Constant Summary collapse
- DIVERGES =
Create this once here, rather than creating a proc object for every evaluation.
lambda { raise DivergenceError.new }
Instance Method Summary collapse
-
#__result__ ⇒ Object
:nodoc:.
-
#__synchronize__ ⇒ Object
:nodoc:.
-
#initialize(&computation) ⇒ Promise
constructor
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#method_missing(*args, &block) ⇒ Object
:nodoc:.
-
#respond_to?(message) ⇒ Boolean
:nodoc:.
Constructor Details
#initialize(&computation) ⇒ Promise
:nodoc:
68 69 70 |
# File 'lib/core/facets/lazy.rb', line 68 def initialize( &computation ) #:nodoc: @computation = computation end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &block) ⇒ Object
:nodoc:
124 125 126 |
# File 'lib/core/facets/lazy.rb', line 124 def method_missing( *args, &block ) #:nodoc: __result__.__send__( *args, &block ) end |
Instance Method Details
#__result__ ⇒ Object
:nodoc:
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/core/facets/lazy.rb', line 83 def __result__ #:nodoc: __synchronize__ do if @computation raise LazyException.new( @exception ) if @exception computation = @computation @computation = DIVERGES # trap divergence due to over-eager recursion begin @result = demand( computation.call( self ) ) @computation = nil rescue DivergenceError raise rescue Exception => exception # handle exceptions @exception = exception raise LazyException.new( @exception ) end end @result end end |
#__synchronize__ ⇒ Object
:nodoc:
71 72 73 |
# File 'lib/core/facets/lazy.rb', line 71 def __synchronize__ #:nodoc: yield end |
#inspect ⇒ Object
:nodoc:
107 108 109 110 111 112 113 114 115 |
# File 'lib/core/facets/lazy.rb', line 107 def inspect #:nodoc: __synchronize__ do if @computation "#<#{ __class__ } computation=#{ @computation.inspect }>" else @result.inspect end end end |
#respond_to?(message) ⇒ Boolean
:nodoc:
117 118 119 120 121 122 |
# File 'lib/core/facets/lazy.rb', line 117 def respond_to?( ) #:nodoc: = .to_sym == :__result__ or == :inspect or __result__.respond_to? end |