json-arel 0.3 → 0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/json_arel.rb +31 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2faecd7790dc6f744b86c2bff6e0d8e1cba20441
|
4
|
+
data.tar.gz: ac1ed46f5eb4c9b5b28f3ec5fcf925a9e55c0b83
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b05b3d1624063e414870d4d16338944dbe2c32d349ebcd4fa16506ac51a9671a5bb1cad1577f1a6493b11e8eeeb1a39d849c58d4a6a7fe367a42165eee7af2f
|
7
|
+
data.tar.gz: 22061f2ad3856c6062e26d7a2edbf528e0308b4449ce68924fa9defe4257edf9e18597604f782048a420bd5f5891cd0f92351e3ecf4d3c51d5bc599c78ea6b91
|
data/lib/json_arel.rb
CHANGED
@@ -1,19 +1,21 @@
|
|
1
1
|
require 'arel'
|
2
|
-
|
3
2
|
require 'active_record'
|
4
3
|
|
5
|
-
ActiveRecord::Base.establish_connection(
|
6
|
-
:adapter => 'postgresql',
|
7
|
-
:database => 'explorer_development'
|
8
|
-
)
|
9
|
-
|
10
4
|
module JSONArel
|
11
5
|
class Resolver
|
12
6
|
|
13
7
|
VALID_OPERATORS = %w[$eq $not_eq $gt $gteq $lt $lteq $in]
|
14
8
|
|
15
|
-
def
|
9
|
+
def default_active_record_opts
|
10
|
+
{
|
11
|
+
:adapter => 'postgresql',
|
12
|
+
:database => 'explorer_development'
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(tree, active_record_opts=default_active_record_opts)
|
16
17
|
@tree = tree
|
18
|
+
ActiveRecord::Base.establish_connection(active_record_opts)
|
17
19
|
end
|
18
20
|
|
19
21
|
def resolve
|
@@ -38,7 +40,7 @@ module JSONArel
|
|
38
40
|
node['fields'] = node['fields'].map {|x| Arel.sql(x) }
|
39
41
|
elsif node['fields'].class == Hash
|
40
42
|
# Is it a hash?
|
41
|
-
node['fields'] = node['fields'].map do |k,v|
|
43
|
+
node['fields'] = node['fields'].invert.map do |k,v|
|
42
44
|
Arel.sql(k).as(v)
|
43
45
|
end
|
44
46
|
end
|
@@ -67,6 +69,26 @@ module JSONArel
|
|
67
69
|
node['group'] = node['group'].map {|x| Arel.sql(x) }
|
68
70
|
expr = expr.group(node['group']) if node['group'].length > 0
|
69
71
|
|
72
|
+
# Resolve ORDER BY
|
73
|
+
node['order'] ||= []
|
74
|
+
if node['order'].class == String
|
75
|
+
node['order'] = [node['order']]
|
76
|
+
end
|
77
|
+
if node['order'].length > 0
|
78
|
+
node['order'].each do |condition|
|
79
|
+
if condition.kind_of?(Array)
|
80
|
+
condition[0] = Arel.sql(condition[0])
|
81
|
+
if condition[1] == "ASC"
|
82
|
+
expr = expr.order condition[0].asc
|
83
|
+
else
|
84
|
+
expr = expr.order condition[0].desc
|
85
|
+
end
|
86
|
+
else
|
87
|
+
expr = expr.order(condition)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
70
92
|
# Evaluate CTEs
|
71
93
|
expr = expr.with(ctes) if ctes.length > 0
|
72
94
|
|
@@ -96,4 +118,4 @@ module JSONArel
|
|
96
118
|
|
97
119
|
class ResolverError < StandardError
|
98
120
|
end
|
99
|
-
end
|
121
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-arel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.5'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Omar Bohsali
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-01-
|
12
|
+
date: 2015-01-27 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email: omar.bohsali@gmail.com
|