Class: Lazy::Promise

Inherits:
Object show all
Defined in:
lib/core/facets/lazy.rb

Overview

:nodoc:

Direct Known Subclasses

PromiseSafe

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

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

#inspectObject

: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:

Returns:

  • (Boolean)


117
118
119
120
121
122
# File 'lib/core/facets/lazy.rb', line 117

def respond_to?( message ) #:nodoc:
  message = message.to_sym
  message == :__result__ or
  message == :inspect or
  __result__.respond_to? message
end