ar-ondemand 1.1.2 → 1.1.3
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.
- checksums.yaml +8 -8
- data/Gemfile.lock +1 -1
- data/lib/ar-ondemand/for_reading.rb +1 -1
- data/lib/ar-ondemand/on_demand.rb +1 -1
- data/lib/ar-ondemand/result.rb +28 -10
- data/lib/ar-ondemand/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NWUwZjI4MzczNTc2NTkxNGEyMDRmNjBlZjNjMWU4YmE5Zjk3MGZiZg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MjhiYTMxYTgxZGI3MGFmOGQxODc2MTcwZDI5OGQ4MDgwOWM0Y2JhOQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
Y2E4Zjk0MTQzNjliM2RjOWZiZWUwZTA1ZGI1NTUyMTk5YTcwY2Y2ZTFiMzZi
|
10
|
+
OWZlZGYzYjE2NTA3MjIzN2UyOWM2MDhhNGY0NzFiZGU5NTViYzhiYWMyYWM5
|
11
|
+
NzFkNTg5NTc0ODE0ZGNmNmE2Nzk0NmRmZjBkOTJjODkyNjQ4NWU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZTU4MTg4NWY0MzU3NWY2NzE3ZTQ0YjM0NWNlMDZiMjM1ODg5NWMxNDk4NTc0
|
14
|
+
ZTI1NGFhOTAwMWE4NDdhMTRhOGM4NjNlZWQ4NDI4MjNhMzc1ZmNlYTYzNmZl
|
15
|
+
ZWFhY2I5ZDE3MzkxMTI4MzcyNjAzOTY4ODY3NGI2MDEyZDZmNGQ=
|
data/Gemfile.lock
CHANGED
@@ -57,7 +57,7 @@ module ActiveRecord
|
|
57
57
|
|
58
58
|
def query_for_reading(ar)
|
59
59
|
results = ::ActiveRecord::Base.connection.exec_query ar.to_sql
|
60
|
-
::ActiveRecord::OnDemand::ResultSet.new self.arel.engine, results
|
60
|
+
::ActiveRecord::OnDemand::ResultSet.new self.arel.engine, results, readonly: true
|
61
61
|
end
|
62
62
|
|
63
63
|
def batch_order_for_reading
|
data/lib/ar-ondemand/result.rb
CHANGED
@@ -3,11 +3,13 @@ module ActiveRecord
|
|
3
3
|
class ResultSet
|
4
4
|
include ::Enumerable
|
5
5
|
|
6
|
-
def initialize(model, results)
|
6
|
+
def initialize(model, results, options = {})
|
7
7
|
@model = model
|
8
8
|
@results = results
|
9
9
|
@column_types = Hash[@model.columns.map { |x| [x.name, x] }]
|
10
10
|
@col_indexes = HashWithIndifferentAccess[@results.columns.each_with_index.map { |x, i| [x,i] }]
|
11
|
+
@readonly = options.delete :readonly
|
12
|
+
@readonly_klass = @readonly ? create_readonly_class : nil
|
11
13
|
end
|
12
14
|
|
13
15
|
def ids
|
@@ -22,25 +24,33 @@ module ActiveRecord
|
|
22
24
|
alias_method :size, :length
|
23
25
|
|
24
26
|
def each
|
25
|
-
@results.rows.each
|
26
|
-
yield ::ActiveRecord::OnDemand::Record.new(convert_to_hash(row), @model, nil)
|
27
|
-
end
|
27
|
+
@results.rows.each { |row| yield result_to_record(row) }
|
28
28
|
end
|
29
29
|
|
30
30
|
def first
|
31
|
-
|
32
|
-
return nil if row.nil?
|
33
|
-
::ActiveRecord::OnDemand::Record.new convert_to_hash(row), @model, nil
|
31
|
+
result_to_record @results.rows.first
|
34
32
|
end
|
35
33
|
|
36
34
|
def last
|
37
|
-
|
38
|
-
return nil if row.nil?
|
39
|
-
::ActiveRecord::OnDemand::Record.new convert_to_hash(row), @model, nil
|
35
|
+
result_to_record @results.rows.last
|
40
36
|
end
|
41
37
|
|
42
38
|
protected
|
43
39
|
|
40
|
+
def result_to_record(row)
|
41
|
+
return nil if row.nil?
|
42
|
+
if @readonly
|
43
|
+
convert_to_struct @readonly_klass, row
|
44
|
+
else
|
45
|
+
::ActiveRecord::OnDemand::Record.new convert_to_hash(row), @model, nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def create_readonly_class
|
50
|
+
attrs = @col_indexes.keys.map(&:to_sym)
|
51
|
+
::Struct.new *attrs
|
52
|
+
end
|
53
|
+
|
44
54
|
def convert_to_hash(rec)
|
45
55
|
# TODO: Is using HashWithIndifferentAccess[] more efficient?
|
46
56
|
h = {}
|
@@ -49,6 +59,14 @@ module ActiveRecord
|
|
49
59
|
end
|
50
60
|
h.with_indifferent_access
|
51
61
|
end
|
62
|
+
|
63
|
+
def convert_to_struct(klass, rec)
|
64
|
+
vals = []
|
65
|
+
@col_indexes.each_pair do |k, v|
|
66
|
+
vals << @column_types[k].type_cast(rec[v])
|
67
|
+
end
|
68
|
+
klass.new *vals
|
69
|
+
end
|
52
70
|
end
|
53
71
|
end
|
54
72
|
end
|
data/lib/ar-ondemand/version.rb
CHANGED