Sha256: 25cbedddbb4e167e912ca92fbeb44911e057a93f81b3d234c6985b120e5e9ce0

Contents?: true

Size: 1.83 KB

Versions: 36

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  class Promise < BasicObject
    undef_method :==, :!, :!=

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

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

    # 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.
    def value
      return @value if defined? @value

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

    # Returns a new +ActiveRecord::Promise+ that will apply the passed block
    # when the value is accessed:
    #
    #   Post.async_pick(:title).then { |title| title.upcase }.value
    #   # => "POST TITLE"
    def then(&block)
      Promise.new(@future_result, @block ? @block >> block : block)
    end

    [:class, :respond_to?, :is_a?].each do |method|
      define_method(method, ::Object.instance_method(method))
    end

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

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

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

      class Complete < self # :nodoc:
        attr_reader :value

        def initialize(value)
          @value = value
        end

        def then
          Complete.new(yield @value)
        end

        def pending?
          false
        end

        private
          def status
            :complete
          end
      end
  end
end

Version data entries

36 entries across 36 versions & 5 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/promise.rb
activerecord-8.0.1 lib/active_record/promise.rb
activerecord-8.0.0.1 lib/active_record/promise.rb
activerecord-7.2.2.1 lib/active_record/promise.rb
activerecord-7.1.5.1 lib/active_record/promise.rb
activerecord-8.0.0 lib/active_record/promise.rb
activerecord-7.2.2 lib/active_record/promise.rb
activerecord-7.1.5 lib/active_record/promise.rb
activerecord-8.0.0.rc2 lib/active_record/promise.rb
activerecord-7.2.1.2 lib/active_record/promise.rb
activerecord-7.1.4.2 lib/active_record/promise.rb
activerecord-8.0.0.rc1 lib/active_record/promise.rb
activerecord-7.2.1.1 lib/active_record/promise.rb
activerecord-7.1.4.1 lib/active_record/promise.rb
activerecord-8.0.0.beta1 lib/active_record/promise.rb
omg-activerecord-8.0.0.alpha9 lib/active_record/promise.rb
omg-activerecord-8.0.0.alpha8 lib/active_record/promise.rb
omg-activerecord-8.0.0.alpha7 lib/active_record/promise.rb
omg-activerecord-8.0.0.alpha4 lib/active_record/promise.rb
omg-activerecord-8.0.0.alpha3 lib/active_record/promise.rb