arel_extensions 2.0.10 → 2.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/arel_extensions/nodes/json.rb +40 -25
- data/lib/arel_extensions/version.rb +1 -1
- data/lib/arel_extensions/visitors/to_sql.rb +25 -17
- data/version_v1.rb +1 -1
- data/version_v2.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: b1ecd7512b41f7db6706dd6affb2e57327cf7331233e39027906743f824f82a8
|
4
|
+
data.tar.gz: 1c7baed7dedb0fb616116fd9888dff8e3a9b0c15ff9b2ae107b982adb522e67c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a81f1010f6cf3d992830b1596eb5b8eaf61fd725cbe97f4cea23aea8172d2f6f6377b58684cf00b16da947ab9a1b65f23efe6f9335db274764931772cb7a219e
|
7
|
+
data.tar.gz: d5140b1fcbff6e4a9549d0d0418ed95215405f8f78ae8e82d6a7a86792130f4e31272cfcd6733c511cc2828599f466a2d66c0b8ffcc461a6d2c7ffe81307c369
|
@@ -33,36 +33,51 @@ module ArelExtensions
|
|
33
33
|
def initialize *expr
|
34
34
|
@dict =
|
35
35
|
if expr.length == 1
|
36
|
-
|
37
|
-
when JsonNode
|
38
|
-
exp.dict
|
39
|
-
when Array
|
40
|
-
exp.map{|e|
|
41
|
-
(e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_node(e)
|
42
|
-
}
|
43
|
-
when Hash
|
44
|
-
exp.reduce({}){|acc,v|
|
45
|
-
acc[convert_to_node(v[0])] = (v[1].is_a?(Array) || v[1].is_a?(Hash)) ? Json.new(v[1]) : convert_to_node(v[1])
|
46
|
-
acc
|
47
|
-
}
|
48
|
-
when String, Numeric, TrueClass, FalseClass
|
49
|
-
convert_to_node(exp)
|
50
|
-
when NilClass
|
51
|
-
Arel.sql('null')
|
52
|
-
else
|
53
|
-
if (exp.is_a?(Arel::Attributes::Attribute) && type_of_attribute(exp) == :string) \
|
54
|
-
|| (exp.return_type == :string)
|
55
|
-
convert_to_node(exp)
|
56
|
-
else
|
57
|
-
[convert_to_node(exp)]
|
58
|
-
end
|
59
|
-
end
|
36
|
+
convert_to_json_node(expr.first)
|
60
37
|
else
|
61
|
-
expr.map{|e| (e
|
38
|
+
expr.map{|e| convert_to_json_node(e) }
|
62
39
|
end
|
63
40
|
super
|
64
41
|
end
|
65
42
|
|
43
|
+
def convert_to_json_node(n)
|
44
|
+
case n
|
45
|
+
when JsonNode
|
46
|
+
n.dict
|
47
|
+
when Array
|
48
|
+
n.map{|e|
|
49
|
+
(e.is_a?(Array) || e.is_a?(Hash)) ? Json.new(e) : convert_to_json_node(e)
|
50
|
+
}
|
51
|
+
when Hash
|
52
|
+
n.reduce({}){|acc,v|
|
53
|
+
acc[convert_to_json_node(v[0])] = (v[1].is_a?(Array) || v[1].is_a?(Hash)) ? Json.new(v[1]) : convert_to_json_node(v[1])
|
54
|
+
acc
|
55
|
+
}
|
56
|
+
when String, Numeric, TrueClass, FalseClass
|
57
|
+
convert_to_node(n)
|
58
|
+
when Date
|
59
|
+
convert_to_node(n.strftime("%Y-%m-%d"))
|
60
|
+
when DateTime, Time
|
61
|
+
convert_to_node(n.strftime("%Y-%m-%dT%H:%M:%S.%L%:z"))
|
62
|
+
when NilClass
|
63
|
+
Arel.sql('null')
|
64
|
+
else
|
65
|
+
convert_to_node(n)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def type_of_node(v)
|
70
|
+
if v.is_a?(Arel::Attributes::Attribute)
|
71
|
+
self.type_of_attribute(v)
|
72
|
+
elsif v.respond_to?(:return_type)
|
73
|
+
v.return_type
|
74
|
+
elsif v.nil?
|
75
|
+
:nil
|
76
|
+
else
|
77
|
+
:string
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
66
81
|
end
|
67
82
|
|
68
83
|
class JsonMerge < JsonNode
|
@@ -585,6 +585,26 @@ module ArelExtensions
|
|
585
585
|
collector
|
586
586
|
end
|
587
587
|
|
588
|
+
def json_value(o,v)
|
589
|
+
case o.type_of_node(v)
|
590
|
+
when :string
|
591
|
+
Arel.when(v.is_null).then(Arel::Nodes.build_quoted("null")).else(Arel::Nodes.build_quoted('"') + v.replace('\\','\\\\').replace('"','\"') + '"')
|
592
|
+
when :date
|
593
|
+
s = v.format('%Y-%m-%d')
|
594
|
+
Arel.when(s.is_null).then(Arel::Nodes.build_quoted("null")).else(Arel::Nodes.build_quoted('"') + s + '"')
|
595
|
+
when :datetime
|
596
|
+
s = v.format('%Y-%m-%dT%H:%M:%S')
|
597
|
+
Arel.when(s.is_null).then(Arel::Nodes.build_quoted("null")).else(Arel::Nodes.build_quoted('"') + s + '"')
|
598
|
+
when :time
|
599
|
+
s = v.format('%H:%M:%S')
|
600
|
+
Arel.when(s.is_null).then(Arel::Nodes.build_quoted("null")).else(Arel::Nodes.build_quoted('"') + s + '"')
|
601
|
+
when :nil
|
602
|
+
Arel::Nodes.build_quoted("null")
|
603
|
+
else
|
604
|
+
ArelExtensions::Nodes::Cast.new([v, :string]).coalesce("null")
|
605
|
+
end
|
606
|
+
end
|
607
|
+
|
588
608
|
def visit_ArelExtensions_Nodes_Json o,collector
|
589
609
|
case o.dict
|
590
610
|
when Array
|
@@ -593,11 +613,7 @@ module ArelExtensions
|
|
593
613
|
if i != 0
|
594
614
|
res += ', '
|
595
615
|
end
|
596
|
-
|
597
|
-
res = res + '"' + v + '"'
|
598
|
-
else
|
599
|
-
res += v
|
600
|
-
end
|
616
|
+
res += json_value(o,v)
|
601
617
|
end
|
602
618
|
res += ']'
|
603
619
|
collector = visit res, collector
|
@@ -607,12 +623,8 @@ module ArelExtensions
|
|
607
623
|
if i != 0
|
608
624
|
res += ', '
|
609
625
|
end
|
610
|
-
res += Arel::Nodes.build_quoted('"')+k + '": '
|
611
|
-
|
612
|
-
res = res + '"' + v + '"'
|
613
|
-
else
|
614
|
-
res += v
|
615
|
-
end
|
626
|
+
res += Arel::Nodes.build_quoted('"') + ArelExtensions::Nodes::Cast.new([k, :string]).coalesce("").replace('\\','\\\\').replace('"','\"') + '": '
|
627
|
+
res += json_value(o,v)
|
616
628
|
end
|
617
629
|
res += '}'
|
618
630
|
collector = visit res, collector
|
@@ -633,12 +645,8 @@ module ArelExtensions
|
|
633
645
|
if i != 0
|
634
646
|
res = res + ', '
|
635
647
|
end
|
636
|
-
kv = Arel::Nodes.build_quoted('"')+k + '": '
|
637
|
-
|
638
|
-
kv = kv + '"' + v + '"'
|
639
|
-
else
|
640
|
-
kv += v
|
641
|
-
end
|
648
|
+
kv = Arel::Nodes.build_quoted('"') + ArelExtensions::Nodes::Cast.new([k, :string]).coalesce("").replace('\\','\\\\').replace('"','\"') + '": '
|
649
|
+
kv += json_value(o,v)
|
642
650
|
res = res + kv.group_concat(', ', order: Array(orders)).coalesce('')
|
643
651
|
end
|
644
652
|
res = res + '}'
|
data/version_v1.rb
CHANGED
data/version_v2.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arel_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yann Azoury
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2020-
|
13
|
+
date: 2020-09-17 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|