Class: ActiveRecord::Promise
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: | BasicObject |
Defined in: | activerecord/lib/active_record/promise.rb |
Class Method Summary
- .new(future_result, block) ⇒ Promise constructor Internal use only
Instance Attribute Summary
-
#pending? ⇒ Boolean
readonly
Returns whether the associated query is still being executed or not.
Instance Method Summary
-
#then(&block)
Returns a new
Promise
that will apply the passed block when the value is accessed: -
#value
Returns the query result.
- #status private
- #inspect Internal use only
- #pretty_print(q) Internal use only
Constructor Details
.new(future_result, block) ⇒ Promise
This method is for internal use only.
# File 'activerecord/lib/active_record/promise.rb', line 7
def initialize(future_result, block) # :nodoc: @future_result = future_result @block = block end
Instance Attribute Details
#pending? ⇒ Boolean
(readonly)
Returns whether the associated query is still being executed or not.
# File 'activerecord/lib/active_record/promise.rb', line 13
def pending? @future_result.pending? end
Instance Method Details
#inspect
This method is for internal use only.
[ GitHub ]
# File 'activerecord/lib/active_record/promise.rb', line 44
def inspect # :nodoc: "#<ActiveRecord::Promise status=#{status}>" end
#pretty_print(q)
This method is for internal use only.
[ GitHub ]
# File 'activerecord/lib/active_record/promise.rb', line 48
def pretty_print(q) # :nodoc: q.text(inspect) end
#status (private)
[ GitHub ]# File 'activerecord/lib/active_record/promise.rb', line 53
def status if @future_result.pending? :pending elsif @future_result.canceled? :canceled else :complete end end
#then(&block)
Returns a new Promise
that will apply the passed block when the value is accessed:
Post.async_pick(:title).then { |title| title.upcase }.value
# => "POST TITLE"
# File 'activerecord/lib/active_record/promise.rb', line 36
def then(&block) Promise.new(@future_result, @block ? @block >> block : block) end
#value
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.
# 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