Class: ActiveRecord::Futures::Future

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/futures/future.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation, query, binds, execution) ⇒ Future

Returns a new instance of Future.



7
8
9
10
11
12
13
# File 'lib/active_record/futures/future.rb', line 7

def initialize(relation, query, binds, execution)
  @relation = relation
  @query = query
  @binds = binds
  @execution = execution
  FutureRegistry.register(self)
end

Instance Attribute Details

#bindsObject (readonly)

Returns the value of attribute binds.



4
5
6
# File 'lib/active_record/futures/future.rb', line 4

def binds
  @binds
end

#queryObject (readonly)

Returns the value of attribute query.



4
5
6
# File 'lib/active_record/futures/future.rb', line 4

def query
  @query
end

#resultObject (readonly)

Returns the value of attribute result.



4
5
6
# File 'lib/active_record/futures/future.rb', line 4

def result
  @result
end

Instance Method Details

#execute(flush = true) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_record/futures/future.rb', line 33

def execute(flush = true)
  # Flush all the futures upon first attempt to exec a future
  FutureRegistry.flush if flush && !executed?

  unless executed?
    @value = execution.call
    @executed = true
  end

  @value
end

#executed?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/active_record/futures/future.rb', line 45

def executed?
  @executed
end

#fulfill(result) ⇒ Object



15
16
17
# File 'lib/active_record/futures/future.rb', line 15

def fulfill(result)
  @result = result
end

#fulfilled?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/active_record/futures/future.rb', line 19

def fulfilled?
  !result.nil?
end

#loadObject



23
24
25
26
27
28
29
30
31
# File 'lib/active_record/futures/future.rb', line 23

def load
  # Only perform a load if the adapter supports futures.
  # This allows to fallback to normal query execution in futures
  # when the adapter does not support futures.
  return unless connection_supports_futures?
  FutureRegistry.current = self
  execute(false)
  FutureRegistry.current = nil
end