occams-record 1.7.1 → 1.8.0

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
  SHA256:
3
- metadata.gz: 0dc125a06b3fa732e1064483f13e5abbe2a7c64b4e6ac7f1b2d37337d8e233cb
4
- data.tar.gz: 5c47a9a644b757463d5dd1961c1b86ed1de50cf247a286e8d1d0c3dd5daa99b8
3
+ metadata.gz: 846af974c8bc267fe8e901995d0aa3341b018d135eeb267c11d9add447cd0155
4
+ data.tar.gz: 763f8a038908cb9a990bcf305644661736dd9133e51a9d8fb8ece2b8ff018fd2
5
5
  SHA512:
6
- metadata.gz: 7fb55ec18a98e4d2bb4b3fab8d3f609a378bee578d1b405d56a43b1f035f40d53149fc8dabc321bbaf17b180eb9e3b1dda282cc2fd8675a3e10c8bc59a9f7a59
7
- data.tar.gz: f9f28ad1537cc297a0b5b1fe8a5209d4f4dfa4529148a603f715da884fa9d9f8188836aca84c8e5dcc36f674bb6abe7211de57c014e90a4539d892da0192c28d
6
+ metadata.gz: 24e1c5dfb09cb35b2b7116cb40aafcb1eb215ac96e3c8bab7ff6558598b31e8abb77b49c2b711392567862b182e1aed705888e939f8b66f15285f2ca352dbc89
7
+ data.tar.gz: 04f78d14765b6ae4d9ccee5f01633132ed1e39662071dfc39f6e33583e9cd3e6214841225eb35ce2f678472ce34bf45e27e23660114f28cede6a27cdb38613b0
data/README.md CHANGED
@@ -284,7 +284,6 @@ The following ActiveRecord features are under consideration, but not high priori
284
284
  The following ActiveRecord features are not supported, and likely never will be. Pull requests are still welcome, though.
285
285
 
286
286
  * Eager loading `through` associations that involve a polymorphic association.
287
- * ActiveRecord enum types
288
287
  * ActiveRecord serialized types
289
288
 
290
289
  ---
@@ -2,13 +2,54 @@ module OccamsRecord
2
2
  module Pluck
3
3
  private
4
4
 
5
- def pluck_results(results, cols)
5
+ def pluck_results(results, cols, model: nil)
6
6
  if cols.size == 1
7
- col = cols[0].to_s
7
+ pluck_results_single(results, cols[0].to_s, model: model)
8
+ else
9
+ pluck_results_multi(results, cols.map(&:to_s), model: model)
10
+ end
11
+ end
12
+
13
+ # returns an array of values
14
+ def pluck_results_single(results, col, model: nil)
15
+ enum = model&.defined_enums&.[](col)
16
+ inv_enum = enum&.invert
17
+ if enum
18
+ results.map { |r|
19
+ val = r[col]
20
+ enum.has_key?(val) ? val : inv_enum[val]
21
+ }
22
+ else
23
+ # micro-optimization for when there are no enums
8
24
  results.map { |r| r[col] }
25
+ end
26
+ end
27
+
28
+ # returns an array of arrays
29
+ def pluck_results_multi(results, cols, model: nil)
30
+ any_enums = false
31
+ cols_with_enums = cols.map { |col|
32
+ enum = model&.defined_enums&.[](col)
33
+ any_enums ||= !!enum
34
+ [col, enum, enum&.invert]
35
+ }
36
+
37
+ if any_enums
38
+ results.map { |row|
39
+ cols_with_enums.map { |(col, enum, inv_enum)|
40
+ if enum
41
+ val = row[col]
42
+ enum.has_key?(val) ? val : inv_enum[val]
43
+ else
44
+ row[col]
45
+ end
46
+ }
47
+ }
9
48
  else
10
- cols = cols.map(&:to_s)
11
- results.map { |r| r.values_at(*cols) }
49
+ # micro-optimization for when there are no enums
50
+ results.map { |row|
51
+ cols.map { |col| row[col] }
52
+ }
12
53
  end
13
54
  end
14
55
  end
@@ -250,7 +250,7 @@ module OccamsRecord
250
250
  else
251
251
  model.connection.exec_query sql
252
252
  end
253
- pluck_results result, cols
253
+ pluck_results result, cols, model: @model
254
254
  end
255
255
  end
256
256
  end
@@ -229,7 +229,7 @@ module OccamsRecord
229
229
  else
230
230
  conn.exec_query _escaped_sql
231
231
  end
232
- pluck_results result, cols
232
+ pluck_results result, cols, model: @eager_loaders.model
233
233
  end
234
234
 
235
235
  private
@@ -54,16 +54,41 @@ module OccamsRecord
54
54
  # * MySQL ?
55
55
  #
56
56
  type = column_types[col] || model_column_types&.[](col)
57
+
58
+ #
59
+ # NOTE is also some variation in when enum values are mapped in different AR versions.
60
+ # In >=5.0, <=7.0, ActiveRecord::Result objects contain the human-readable values. In 4.2 and
61
+ # pre-release versions of 7.1, they instead have the RAW values (e.g. integers) which we must map ourselves.
62
+ #
63
+ enum = model&.defined_enums&.[](col)
64
+ inv_enum = enum&.invert
65
+
57
66
  case type&.type
58
67
  when nil
59
- define_method(col) { @raw_values[idx] }
68
+ if enum
69
+ define_method(col) {
70
+ val = @raw_values[idx]
71
+ enum.has_key?(val) ? val : inv_enum[val]
72
+ }
73
+ else
74
+ define_method(col) { @raw_values[idx] }
75
+ end
60
76
  when :datetime
61
77
  define_method(col) { @cast_values[idx] ||= type.send(CASTER, @raw_values[idx])&.in_time_zone }
62
78
  when :boolean
63
79
  define_method(col) { @cast_values[idx] ||= type.send(CASTER, @raw_values[idx]) }
64
80
  define_method("#{col}?") { !!send(col) }
65
81
  else
66
- define_method(col) { @cast_values[idx] ||= type.send(CASTER, @raw_values[idx]) }
82
+ if enum
83
+ define_method(col) {
84
+ @cast_values[idx] ||= (
85
+ val = type.send(CASTER, @raw_values[idx])
86
+ enum.has_key?(val) ? val : inv_enum[val]
87
+ )
88
+ }
89
+ else
90
+ define_method(col) { @cast_values[idx] ||= type.send(CASTER, @raw_values[idx]) }
91
+ end
67
92
  end
68
93
  end
69
94
  end
@@ -40,7 +40,7 @@ module OccamsRecord
40
40
  #
41
41
  # Hash-like accessor for attributes and associations.
42
42
  #
43
- # @param attr [String|Symbol\
43
+ # @param attr [String|Symbol]
44
44
  # @return [Object]
45
45
  #
46
46
  def [](attr)
@@ -3,5 +3,5 @@
3
3
  #
4
4
  module OccamsRecord
5
5
  # @private
6
- VERSION = "1.7.1".freeze
6
+ VERSION = "1.8.0".freeze
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: occams-record
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.1
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Hollinger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-30 00:00:00.000000000 Z
11
+ date: 2023-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord