ar-ondemand 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZThhOTU2ZmNlZGRlOTZmNWZiZTNlMDQxNjIwNzgwMzEyMTU0MGZhOA==
4
+ NWUwZjI4MzczNTc2NTkxNGEyMDRmNjBlZjNjMWU4YmE5Zjk3MGZiZg==
5
5
  data.tar.gz: !binary |-
6
- MWMyMTgzMGFkMjRhZjQ2YzNlNWI1OGIxNWM4OWIwMjJkMWQ0NWY2Yw==
6
+ MjhiYTMxYTgxZGI3MGFmOGQxODc2MTcwZDI5OGQ4MDgwOWM0Y2JhOQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OWI0NWYxZDA1ODY3MDNmNjYwOWNiZjRkMmE4MDdlODA4MDA0NjBiOTliOWFm
10
- MTA4NTVjYjBjNTU3MTQ3YzEyYTcxMTc5NGY0ZWNiY2M0NjJlYjNkMTVkZmZm
11
- MzZiNjMzODllMjVhOTNhOWUyZTNmZTU2MmY2Zjk2ZjM3MDE1YWU=
9
+ Y2E4Zjk0MTQzNjliM2RjOWZiZWUwZTA1ZGI1NTUyMTk5YTcwY2Y2ZTFiMzZi
10
+ OWZlZGYzYjE2NTA3MjIzN2UyOWM2MDhhNGY0NzFiZGU5NTViYzhiYWMyYWM5
11
+ NzFkNTg5NTc0ODE0ZGNmNmE2Nzk0NmRmZjBkOTJjODkyNjQ4NWU=
12
12
  data.tar.gz: !binary |-
13
- MWVmZGY0ZjA2Y2ZmMjNhYTJlNzEwMDdiYzI3NjE4YWMzZDAzOGJlMjFhYjBl
14
- ZDBhOWQ1Mjk5MDBjNmU4YTYwMzRjMmZjOGRkMzQxZjk1YTFkY2FmMDhiNDhh
15
- YmViMTEwMTcwN2NhODc4MjAzN2IyNTE0NGUwYTZhMGFlMmQ3MTQ=
13
+ ZTU4MTg4NWY0MzU3NWY2NzE3ZTQ0YjM0NWNlMDZiMjM1ODg5NWMxNDk4NTc0
14
+ ZTI1NGFhOTAwMWE4NDdhMTRhOGM4NjNlZWQ4NDI4MjNhMzc1ZmNlYTYzNmZl
15
+ ZWFhY2I5ZDE3MzkxMTI4MzcyNjAzOTY4ODY3NGI2MDEyZDZmNGQ=
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ar-ondemand (1.1.2)
4
+ ar-ondemand (1.1.3)
5
5
  activerecord (>= 3.2)
6
6
  activesupport (>= 3.2)
7
7
 
@@ -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
@@ -28,7 +28,7 @@ module ActiveRecord
28
28
  h[row[@key_index]] = row
29
29
  h
30
30
  end
31
-
31
+
32
32
  self
33
33
  end
34
34
 
@@ -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 do |row|
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
- row = @results.rows.first
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
- row = @results.rows.last
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
@@ -1,3 +1,3 @@
1
1
  module ArOnDemand
2
- VERSION = '1.1.2'
2
+ VERSION = '1.1.3'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar-ondemand
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Frank