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 +4 -4
- data/README.md +0 -1
- data/lib/occams-record/pluck.rb +45 -4
- data/lib/occams-record/query.rb +1 -1
- data/lib/occams-record/raw_query.rb +1 -1
- data/lib/occams-record/results/results.rb +27 -2
- data/lib/occams-record/results/row.rb +1 -1
- data/lib/occams-record/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 846af974c8bc267fe8e901995d0aa3341b018d135eeb267c11d9add447cd0155
|
4
|
+
data.tar.gz: 763f8a038908cb9a990bcf305644661736dd9133e51a9d8fb8ece2b8ff018fd2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
---
|
data/lib/occams-record/pluck.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
11
|
-
results.map { |
|
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
|
data/lib/occams-record/query.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
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.
|
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-
|
11
|
+
date: 2023-07-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|