activerecord-futures 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. data/README.md +34 -8
  2. data/lib/active_record/connection_adapters/future_enabled.rb +6 -5
  3. data/lib/active_record/connection_adapters/future_enabled_mysql2_adapter.rb +2 -1
  4. data/lib/active_record/connection_adapters/future_enabled_postgresql_adapter.rb +21 -7
  5. data/lib/active_record/futures.rb +11 -23
  6. data/lib/active_record/futures/calculation_methods.rb +34 -0
  7. data/lib/active_record/futures/delegation.rb +1 -1
  8. data/lib/active_record/futures/finder_methods.rb +59 -0
  9. data/lib/active_record/futures/future.rb +21 -57
  10. data/lib/active_record/futures/future_array.rb +23 -0
  11. data/lib/active_record/futures/future_registry.rb +33 -0
  12. data/lib/active_record/futures/future_value.rb +20 -0
  13. data/lib/active_record/futures/query_recording.rb +11 -4
  14. data/lib/activerecord-futures.rb +5 -4
  15. data/lib/activerecord-futures/version.rb +1 -1
  16. data/spec/active_record/futures/future_array_spec.rb +26 -0
  17. data/spec/active_record/futures/future_registry_spec.rb +86 -0
  18. data/spec/active_record/futures/future_spec.rb +42 -69
  19. data/spec/active_record/futures/future_value_spec.rb +26 -0
  20. data/spec/in_action/combination_of_futures_spec.rb +13 -9
  21. data/spec/in_action/future_all_execution_spec.rb +20 -0
  22. data/spec/in_action/future_count_execution_spec.rb +8 -56
  23. data/spec/in_action/future_exists_execution_spec.rb +22 -0
  24. data/spec/in_action/future_find_execution_spec.rb +68 -0
  25. data/spec/in_action/future_first_execution_spec.rb +20 -0
  26. data/spec/in_action/future_fulfillment_spec.rb +10 -7
  27. data/spec/in_action/future_last_execution_spec.rb +21 -0
  28. data/spec/in_action/future_pluck_execution_spec.rb +4 -27
  29. data/spec/in_action/future_relation_execution_spec.rb +8 -30
  30. data/spec/spec_helper.rb +2 -1
  31. data/spec/support/futurized_method_examples.rb +43 -0
  32. metadata +25 -12
  33. data/lib/active_record/futures/future_calculation.rb +0 -30
  34. data/lib/active_record/futures/future_calculation_array.rb +0 -8
  35. data/lib/active_record/futures/future_calculation_value.rb +0 -7
  36. data/lib/active_record/futures/future_relation.rb +0 -34
  37. data/spec/active_record/futures/future_calculation_array_spec.rb +0 -29
  38. data/spec/active_record/futures/future_calculation_value_spec.rb +0 -29
  39. data/spec/active_record/futures/future_relation_spec.rb +0 -81
@@ -1,30 +0,0 @@
1
- module ActiveRecord
2
- module Futures
3
- class FutureCalculation < Future
4
- attr_reader :query, :execution
5
- private :query, :execution
6
-
7
- def initialize(relation, query, execution)
8
- super(relation)
9
- @query = query
10
- @execution = execution
11
- end
12
-
13
- def to_sql
14
- query
15
- end
16
-
17
- private
18
-
19
- def execute
20
- @value = execution.call unless executed?
21
- @executed = true
22
- @value
23
- end
24
-
25
- def executed?
26
- @executed
27
- end
28
- end
29
- end
30
- end
@@ -1,8 +0,0 @@
1
- module ActiveRecord
2
- module Futures
3
- class FutureCalculationArray < FutureCalculation
4
- include ActiveRecord::Delegation
5
- fetch_with(:to_a)
6
- end
7
- end
8
- end
@@ -1,7 +0,0 @@
1
- module ActiveRecord
2
- module Futures
3
- class FutureCalculationValue < FutureCalculation
4
- fetch_with(:value)
5
- end
6
- end
7
- end
@@ -1,34 +0,0 @@
1
- module ActiveRecord
2
- module Futures
3
- class FutureRelation < Future
4
- include ActiveRecord::Delegation
5
-
6
- def initialize(relation)
7
- super
8
- @klass = relation.klass
9
-
10
- # Eagerly get sql from relation, since PostgreSQL adapter may use the
11
- # same method `exec_query` to retrieve the columns when executing
12
- # `to_sql`, and that will cause an infinite loop if a current future
13
- # exists
14
- @query = relation.to_sql
15
- end
16
-
17
- fetch_with(:to_a)
18
-
19
- def to_sql
20
- @query
21
- end
22
-
23
- private
24
-
25
- def execute
26
- relation.to_a
27
- end
28
-
29
- def executed?
30
- relation.loaded?
31
- end
32
- end
33
- end
34
- end
@@ -1,29 +0,0 @@
1
- require "spec_helper"
2
-
3
- module ActiveRecord::Futures
4
- describe FutureCalculationArray do
5
- let(:relation) do
6
- double(ActiveRecord::Relation, {
7
- connection: double("connection", supports_futures?: true)
8
- })
9
- end
10
-
11
- let(:query) { "select 1" }
12
- let(:exec_result) { double("exec result", inspect: nil) }
13
- let(:exec) { ->{ exec_result } }
14
-
15
- subject { FutureCalculationArray.new(relation, query, exec) }
16
-
17
- describe "#inspect" do
18
- before do
19
- subject.inspect
20
- end
21
-
22
- it { should be_executed }
23
-
24
- it "delegates to exec result's inspect" do
25
- exec_result.should have_received(:inspect)
26
- end
27
- end
28
- end
29
- end
@@ -1,29 +0,0 @@
1
- require "spec_helper"
2
-
3
- module ActiveRecord::Futures
4
- describe FutureCalculationValue do
5
- let(:relation) do
6
- double(ActiveRecord::Relation, {
7
- connection: double("connection", supports_futures?: true)
8
- })
9
- end
10
-
11
- let(:query) { "select 1" }
12
- let(:exec_result) { double("exec result", inspect: nil) }
13
- let(:exec) { ->{ exec_result } }
14
-
15
- subject { FutureCalculationValue.new(relation, query, exec) }
16
-
17
- describe "#inspect" do
18
- before do
19
- subject.inspect
20
- end
21
-
22
- it { should be_executed }
23
-
24
- it "delegates to exec result's inspect" do
25
- exec_result.should have_received(:inspect)
26
- end
27
- end
28
- end
29
- end
@@ -1,81 +0,0 @@
1
- require "spec_helper"
2
-
3
- module ActiveRecord::Futures
4
- describe FutureRelation do
5
- let(:relation) do
6
- double(ActiveRecord::Relation, {
7
- klass: Class.new,
8
- to_a: nil,
9
- to_sql: "select 1",
10
- connection: double("connection", supports_futures?: true)
11
- })
12
- end
13
-
14
- subject { FutureRelation.new(relation) }
15
-
16
- describe ".new" do
17
- before do
18
- subject
19
- end
20
-
21
- it "gets registered" do
22
- Future.all.should have(1).future
23
- Future.all.first.should == subject
24
- end
25
-
26
- its(:relation) { should eq relation }
27
-
28
- it { should_not be_fulfilled }
29
- end
30
-
31
- describe "#fulfill" do
32
- let(:result) { "Some cool result" }
33
-
34
- before do
35
- subject.fulfill(result)
36
- end
37
-
38
- it { should be_fulfilled }
39
- end
40
-
41
- describe "#load" do
42
- before do
43
- relation.stub(:to_a) do
44
- @current_future = Future.current
45
- nil
46
- end
47
-
48
- subject.load
49
- end
50
-
51
- it "calls #to_a in the relation" do
52
- relation.should have_received(:to_a)
53
- end
54
-
55
- it "sets the current future to itself while #to_a was being called in the relation" do
56
- @current_future.should == subject
57
- end
58
-
59
- it "sets to nil the current future afterwards" do
60
- Future.current.should == nil
61
- end
62
- end
63
-
64
- describe "#inspect" do
65
- let(:resulting_array) { double(Array, inspect: nil) }
66
-
67
- before do
68
- relation.stub(loaded?: true)
69
- relation.stub(:to_a).and_return(resulting_array)
70
-
71
- subject.inspect
72
- end
73
-
74
- it { should be_executed }
75
-
76
- it "delegates to relation.to_a.inspect" do
77
- resulting_array.should have_received(:inspect)
78
- end
79
- end
80
- end
81
- end