class ActiveRecord::Promise

def initialize(future_result, block) # :nodoc:

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

def inspect # :nodoc:

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

def pending?

Returns whether the associated query is still being executed or not.
def pending?
  @future_result.pending?
end

def pretty_print(q) # :nodoc:

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

def status

def status
  if @future_result.pending?
    :pending
  elsif @future_result.canceled?
    :canceled
  else
    :complete
  end
end

def then(&block)

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

when the value is accessed:
Returns a new +ActiveRecord::Promise+ that will apply the passed block
def then(&block)
  Promise.new(@future_result, @block ? @block >> block : block)
end

def value

If the query failed, +#value+ will raise the corresponding error.
If the query wasn't completed yet, accessing +#value+ will block until the query completes.
Returns the query result.
def value
  return @value if defined? @value
  result = @future_result.result
  @value = if @block
    @block.call(result)
  else
    result
  end
end