Class: ActiveRecord::Promise

Inherits:
BasicObject
Defined in:
activerecord/lib/active_record/promise.rb

Direct Known Subclasses

Complete

Defined Under Namespace

Classes: Complete

Instance Method Summary collapse

Constructor Details

#initialize(future_result, block) ⇒ Promise

:nodoc:



7
8
9
10
# File 'activerecord/lib/active_record/promise.rb', line 7

def initialize(future_result, block) # :nodoc:
  @future_result = future_result
  @block = block
end

Instance Method Details

#inspectObject

:nodoc:



44
45
46
# File 'activerecord/lib/active_record/promise.rb', line 44

def inspect # :nodoc:
  "#<ActiveRecord::Promise status=#{status}>"
end

#pending?Boolean

Returns whether the associated query is still being executed or not.

Returns:

  • (Boolean)


13
14
15
# File 'activerecord/lib/active_record/promise.rb', line 13

def pending?
  @future_result.pending?
end

#pretty_print(q) ⇒ Object

:nodoc:



48
49
50
# File 'activerecord/lib/active_record/promise.rb', line 48

def pretty_print(q) # :nodoc:
  q.text(inspect)
end

#then(&block) ⇒ Object

Returns a new ActiveRecord::Promise that will apply the passed block when the value is accessed:

Post.async_pluck(:title).then { |title| title.upcase }.value
# => "POST TITLE"


36
37
38
# File 'activerecord/lib/active_record/promise.rb', line 36

def then(&block)
  Promise.new(@future_result, @block ? @block >> block : block)
end

#valueObject

Returns the query result. If the query wasn’t completed yet, accessing #value will block until the query completes. If the query failed, #value will raise the corresponding error.



20
21
22
23
24
25
26
27
28
29
# File 'activerecord/lib/active_record/promise.rb', line 20

def value
  return @value if defined? @value

  result = @future_result.result
  @value = if @block
    @block.call(result)
  else
    result
  end
end