class Promising::Promise
Constants
- NOT_SET
Public Class Methods
_load(obj)
click to toggle source
Method used by Marshal to deserialize the object.
@param [Object] @return [Promise]
# File lib/promising/promise.rb, line 99 def self._load(obj) ::Marshal.load(obj) end
new(timeout:nil,&block)
click to toggle source
Creates a new promise.
@example Lazily evaluate a database call
result = promise { @db.query("SELECT * FROM TABLE") }
@yield [] The block to evaluate lazily. @see Kernel#promise
# File lib/promising/promise.rb, line 33 def initialize(timeout:nil,&block) if block.arity > 0 ::Kernel.raise ::ArgumentError, "Cannot store a promise that requires an argument" end @timeout = timeout @block = block @mutex = ::Mutex.new @result = NOT_SET @error = NOT_SET end
Public Instance Methods
__force__()
click to toggle source
Force the evaluation of this promise immediately
@return [Object]
# File lib/promising/promise.rb, line 48 def __force__ @mutex.synchronize do if @result.equal?(NOT_SET) && @error.equal?(NOT_SET) begin if @timeout ::Timeout.timeout(@timeout) do @result = @block.call end else @result = @block.call end rescue ::Exception => e @error = e end end end if @result.equal?(NOT_SET) && @error.equal?(NOT_SET) # BasicObject won't send raise to Kernel @error.equal?(NOT_SET) ? @result : ::Kernel.raise(@error) end
Also aliased as: force
_dump(limit)
click to toggle source
Method used by Marshal to serialize the object. Forces evaluation.
@param [Integer] limit – refer to Marshal doc @return [Object]
# File lib/promising/promise.rb, line 90 def _dump(limit) ::Marshal.dump(__force__, limit) end
respond_to?(method, include_all=false)
click to toggle source
Does this promise support the given method?
@param [Symbol, Boolean] @return [Boolean]
# File lib/promising/promise.rb, line 74 def respond_to?(method, include_all=false) # If the promised object implements marshal_dump, Marshal will use it in # preference to our _dump, so make sure that doesn't happen. return false if :marshal_dump.equal?(method) :_dump.equal?(method) || # for Marshal :force.equal?(method) || :__force__.equal?(method) || __force__.respond_to?(method, include_all) end
Private Instance Methods
method_missing(method, *args, &block)
click to toggle source
# File lib/promising/promise.rb, line 105 def method_missing(method, *args, &block) __force__.__send__(method, *args, &block) end