simple-sql 0.4.12 → 0.4.13

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 219be87ab80103dce585c2c71a8be92dc8274448
4
- data.tar.gz: aacb51c639d27d3348695c075a9e1e12c27fe915
3
+ metadata.gz: f577e360eaff0f4796c8bbb52026e6ad135bf0bf
4
+ data.tar.gz: 91bb7c60f202bdc03021f8baccf060f116f78cf3
5
5
  SHA512:
6
- metadata.gz: 1f8250eac83d27aa869fdc01814f1c565e71470d8805b876eac12c875466f780341259ee9e6f10f84e27a0f3dada384a2044c109267c5c8bcf34d4eb2d14ffcd
7
- data.tar.gz: 4260387271f5c4ab8e25d6bdc4d457a94412453598f0e4ce876800a89db0862871950efe9849ed1f5823c1bce8727f47d81df81f4c28ed4c0851c8cefe6c8405
6
+ metadata.gz: 3b7844945f0fe1f7befd22015813693dd908744a8c97d8a8a1e9351c91a85148a844df8c8bffd97cf5099cb81461d941fa214145dc8310fbfed186c14654d64a
7
+ data.tar.gz: 5d5fe7b5ec335edc2d0e95abe0a13e1e14e48c0bec0c64ae78fd7ad967289e470863191ae00854426a4300db92bdd036a39f281e37c27779ad8f6499672db754
@@ -1,75 +1,85 @@
1
- module Simple::SQL::Helpers::RowConverter # :private:
1
+ module Simple::SQL::Helpers::RowConverter
2
2
  SELF = self
3
3
 
4
4
  # returns an array of converted records
5
- def self.convert_ary(records, into:)
5
+ def self.convert_row(records, into:, associations: nil)
6
6
  hsh = records.first
7
7
  return records unless hsh
8
8
 
9
9
  converter = if into == :struct
10
- StructConverter.for(attributes: hsh.keys)
10
+ StructConverter.for(attributes: hsh.keys, associations: associations)
11
11
  else
12
- TypeConverter.for(type: into)
12
+ TypeConverter.for(type: into, associations: associations)
13
13
  end
14
14
 
15
- records.map { |record| converter.convert_ary(record) }
15
+ records.map { |record| converter.convert_row(record) }
16
16
  end
17
17
 
18
18
  def self.convert(record, into:) # :nodoc:
19
- ary = convert_ary([record], into: into)
19
+ ary = convert_row([record], into: into)
20
20
  ary.first
21
21
  end
22
22
 
23
23
  class TypeConverter #:nodoc:
24
- def self.for(type:)
25
- new(type: type)
24
+ def self.for(type:, associations:)
25
+ new(type: type, associations: associations)
26
26
  end
27
27
 
28
- def initialize(type:)
29
- @type = type
28
+ def initialize(type:, associations:)
29
+ @type = type
30
+ @associations = associations
30
31
  end
31
32
 
32
- def convert_ary(hsh)
33
+ def convert_row(hsh)
34
+ hsh = convert_associations(hsh) if @associations
35
+ @type.new hsh
36
+ end
37
+
38
+ def convert_associations(hsh)
33
39
  updates = {}
34
- hsh.each do |key, value|
40
+
41
+ @associations.each do |key|
42
+ value = hsh.fetch(key)
35
43
  case value
36
44
  when Hash then updates[key] = SELF.convert(value, into: @type)
37
- when Array then updates[key] = SELF.convert_ary(value, into: @type)
45
+ when Array then updates[key] = SELF.convert_row(value, into: @type)
38
46
  end
39
47
  end
40
48
 
41
- hsh = hsh.merge(updates)
42
-
43
- @type.new hsh
49
+ hsh.merge(updates)
44
50
  end
45
51
  end
46
52
 
47
53
  class StructConverter # :nodoc:
48
- def self.for(attributes:)
54
+ def self.for(attributes:, associations:)
49
55
  @cache ||= {}
50
- @cache[attributes] ||= new(attributes)
56
+ @cache[[attributes, associations]] ||= new(attributes: attributes, associations: associations)
51
57
  end
52
58
 
53
- def initialize(attributes)
59
+ def initialize(attributes:, associations:)
60
+ @attributes = attributes
61
+ @associations = associations
62
+ @association_indices = associations.map { |association| attributes.index(association) } if associations
63
+
54
64
  @klass = Struct.new(*attributes)
55
65
  end
56
66
 
57
- def convert_ary(hsh)
58
- values = hsh.values_at(*@klass.members)
59
- updates = {}
67
+ def convert_row(hsh)
68
+ values = hsh.values_at(*@attributes)
60
69
 
61
- values.each_with_index do |value, idx|
70
+ convert_associations(values) if @associations
71
+ @klass.new(*values)
72
+ end
73
+
74
+ # convert values at the <tt>@association_indices</tt>.
75
+ def convert_associations(values)
76
+ @association_indices.each do |idx|
77
+ value = values[idx]
62
78
  case value
63
- when Hash then updates[idx] = SELF.convert(value, into: :struct)
64
- when Array then updates[idx] = SELF.convert_ary(value, into: :struct)
79
+ when Hash then values[idx] = SELF.convert(value, into: :struct)
80
+ when Array then values[idx] = SELF.convert_row(value, into: :struct)
65
81
  end
66
82
  end
67
-
68
- updates.each do |idx, updated_value|
69
- values[idx] = updated_value
70
- end
71
-
72
- @klass.new(*values)
73
83
  end
74
84
  end
75
85
  end
@@ -11,6 +11,7 @@ class ::Simple::SQL::Result::Records < ::Simple::SQL::Result
11
11
  @hash_records = records
12
12
  @target_type = target_type
13
13
  @pg_source_oid = pg_source_oid
14
+ @associations = []
14
15
 
15
16
  materialize
16
17
  end
@@ -54,6 +55,9 @@ class ::Simple::SQL::Result::Records < ::Simple::SQL::Result
54
55
  AssociationLoader.preload @hash_records, association,
55
56
  host_table: host_table, schema: schema, as: as,
56
57
  order_by: order_by, limit: limit
58
+
59
+ @associations << association
60
+
57
61
  materialize
58
62
  end
59
63
 
@@ -64,7 +68,7 @@ class ::Simple::SQL::Result::Records < ::Simple::SQL::Result
64
68
 
65
69
  def materialize
66
70
  records = @hash_records
67
- records = RowConverter.convert_ary(records, into: @target_type) if @target_type != Hash
71
+ records = RowConverter.convert_row(records, associations: @associations, into: @target_type) if @target_type != Hash
68
72
  replace(records)
69
73
  end
70
74
  end
@@ -1,5 +1,5 @@
1
1
  module Simple
2
2
  module SQL
3
- VERSION = "0.4.12"
3
+ VERSION = "0.4.13"
4
4
  end
5
5
  end
@@ -134,6 +134,16 @@ describe "Simple::SQL::Result#preload" do
134
134
 
135
135
  expect(organizations.first.users.first).to be_a(OpenStruct)
136
136
  end
137
+
138
+ it "only touches associations, leaving non-associated hashes and arrays alone" do
139
+ organizations = SQL.all "SELECT id, ARRAY[10000] AS ary FROM organizations", into: OpenStruct
140
+ expect(organizations.first.ary).to eq([10000])
141
+ end
142
+
143
+ it "only touches associations, leaving non-associated hashes and arrays alone" do
144
+ organizations = SQL.all "SELECT id, ARRAY[10000] AS ary FROM organizations", into: :struct
145
+ expect(organizations.first.ary).to eq([10000])
146
+ end
137
147
  end
138
148
 
139
149
  describe ":order_by" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.12
4
+ version: 0.4.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - radiospiel