to_wa 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +135 -18
- data/lib/to_wa/builder.rb +13 -9
- data/lib/to_wa/version.rb +1 -1
- data/lib/to_wa.rb +6 -2
- data/to_wa.gemspec +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff02d9d70a9c571bba011ff3f651e559498cd6cd
|
4
|
+
data.tar.gz: 92bba9fd29b6adfd9035a16e512729bc54b6b092
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 75b7af23c3c6cf5cfb46ebd4533c9bb9bec6705e7b480b059071125bb2993fc3e7b017f1a25e5d2446ce52dd74b5916fe4bbabd3444b7f5e0eb2d2e9b17473ad
|
7
|
+
data.tar.gz: bea440ef0ff39602c6f265b8046ef359323f876b6b6b12a76beaabe5b8ae055953ca6b7306eca6cb27aa795845a60d6eae404de339bab05979a65e7f70fe8422
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,39 +1,156 @@
|
|
1
|
+
[![CircleCI](https://circleci.com/gh/mmmpa/to_wa.svg?style=svg)](https://circleci.com/gh/mmmpa/to_wa)
|
2
|
+
|
1
3
|
# ToWa
|
2
4
|
|
3
|
-
|
5
|
+
"ToWa" adds `to_wa` method to ActiveRecord based class. `to_wa` method can receive `Hash` or `JSON` argument and add them as `Where` to ActiveRecord query.
|
6
|
+
|
7
|
+
# Installation
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ToWa'
|
11
|
+
```
|
4
12
|
|
5
|
-
|
13
|
+
```console
|
14
|
+
bundle install
|
15
|
+
```
|
6
16
|
|
7
|
-
|
17
|
+
```ruby
|
18
|
+
class TestRecord < ActiveRecord::Base
|
19
|
+
extend ToWa
|
20
|
+
end
|
21
|
+
```
|
8
22
|
|
9
|
-
|
23
|
+
## Simple usage
|
10
24
|
|
11
25
|
```ruby
|
12
|
-
|
26
|
+
TestRecord.to_wa(
|
27
|
+
{
|
28
|
+
'and': [
|
29
|
+
{ '=': ['name', 'ToWa'] },
|
30
|
+
{ '=': ['gender', 'male'] }
|
31
|
+
]
|
32
|
+
}
|
33
|
+
).to_sql
|
34
|
+
#=> "SELECT `test_records`.* FROM `test_records` WHERE (`test_records`.`name` = 'ToWa' AND `test_records`.`genderb` = 'malebbb')"
|
35
|
+
```
|
36
|
+
|
37
|
+
# Basic syntax
|
38
|
+
|
39
|
+
```js
|
40
|
+
{ "operator": valuesArray }
|
13
41
|
```
|
14
42
|
|
15
|
-
|
43
|
+
## Comparison operators
|
44
|
+
|
45
|
+
They receive `[left, right]`.
|
16
46
|
|
17
|
-
|
47
|
+
```js
|
48
|
+
{ "=": ["name", "ToWa"] } // means "name eq ToWa"
|
49
|
+
```
|
50
|
+
|
51
|
+
|alias|operator||
|
52
|
+
|:---|:---|:---|
|
53
|
+
==|eq|
|
54
|
+
=|eq|
|
55
|
+
eq|eq|
|
56
|
+
!=|not_eq|
|
57
|
+
<>|not_eq|
|
58
|
+
ne|not_eq|
|
59
|
+
\>|gt|
|
60
|
+
gt|gt|
|
61
|
+
\>=|gteq|
|
62
|
+
gteq|gteq|
|
63
|
+
<|lt|
|
64
|
+
lt|lt|
|
65
|
+
<=|lteq|
|
66
|
+
lteq|lteq|
|
67
|
+
matches|matches|`right like "%right%"` (% in right will be escaped.)
|
68
|
+
like|matches|
|
69
|
+
in|in|`right` must be Array. `left in (right)`
|
70
|
+
between|between|`right` must be Array. `left between right[0] and right[1]`
|
71
|
+
|
72
|
+
## Logical Operators
|
73
|
+
|
74
|
+
They receive data list.
|
75
|
+
|
76
|
+
```js
|
77
|
+
{
|
78
|
+
"and": [
|
79
|
+
{ "=": ["name", "ToWa"] },
|
80
|
+
{ "=": ["gender", "male"] },
|
81
|
+
{
|
82
|
+
"or": [
|
83
|
+
{ "<": ["age", 12] },
|
84
|
+
{ ">": ["age", 16] }
|
85
|
+
]
|
86
|
+
}
|
87
|
+
]
|
88
|
+
} // means "(name EQ ToWa AND name EQ ToWa AND (age < 12 OR age > 16))"
|
89
|
+
```
|
18
90
|
|
19
|
-
|
91
|
+
|alias|operator||
|
92
|
+
|:---|:---|:---|
|
93
|
+
&&|and|
|
94
|
+
and|and|
|
95
|
+
\|\||or|
|
96
|
+
or|or|
|
97
|
+
not|not|It must receive Array that includes only one data. `{ "not": [{ "name": "ToWa" }] }`
|
20
98
|
|
21
|
-
$ gem install to_wa
|
22
99
|
|
23
|
-
|
100
|
+
# Usage
|
24
101
|
|
25
|
-
|
102
|
+
(Ofcourse, `ActiveRecord::Relation` will be provided after `to_wa` without `to_sql`.)
|
26
103
|
|
27
|
-
|
104
|
+
```ruby
|
105
|
+
TestRecord.to_wa({ '=': ['name', 'ToWa'] }).to_sql
|
106
|
+
#=> SELECT `test_records`.* FROM `test_records` WHERE `test_records`.`name` = 'ToWa'
|
107
|
+
```
|
28
108
|
|
29
|
-
|
109
|
+
```ruby
|
110
|
+
TestRecord.to_wa(
|
111
|
+
{
|
112
|
+
'and': [
|
113
|
+
{ '=': ['name', 'ToWa'] },
|
114
|
+
{ '=': ['gender', 'male'] }
|
115
|
+
]
|
116
|
+
}
|
117
|
+
).to_sql
|
118
|
+
#=> "SELECT `test_records`.* FROM `test_records` WHERE (`test_records`.`name` = 'ToWa' AND `test_records`.`genderb` = 'male')"
|
119
|
+
```
|
30
120
|
|
31
|
-
|
121
|
+
```ruby
|
122
|
+
TestRecord.to_wa(
|
123
|
+
{
|
124
|
+
'and': [
|
125
|
+
{
|
126
|
+
'or': [
|
127
|
+
{ '=': ['name', 'ToWa'] },
|
128
|
+
{ '=': ['name', 'to_wa'] }
|
129
|
+
]
|
130
|
+
},
|
131
|
+
{ '=': ['gender', 'male'] }
|
132
|
+
]
|
133
|
+
}
|
134
|
+
).to_sql
|
135
|
+
#=> "SELECT `test_records`.* FROM `test_records` WHERE ((`test_records`.`name` = 'ToWa' OR `test_records`.`name` = 'to_wa') AND `test_records`.`gender` = 'male')"
|
136
|
+
```
|
32
137
|
|
33
|
-
##
|
138
|
+
## Working with other query
|
34
139
|
|
35
|
-
|
140
|
+
```ruby
|
141
|
+
TestRecord.select(:id).to_wa({ '=': ['name', 'ToWa'] }).order(id: :desc).to_sql
|
142
|
+
#=> "SELECT `test_records`.`id` FROM `test_records` WHERE `test_records`.`name` = 'ToWa' ORDER BY `test_records`.`id` DESC"
|
143
|
+
```
|
36
144
|
|
37
|
-
##
|
145
|
+
## Providing Arel::Nodes without ActiveRecord based class
|
38
146
|
|
39
|
-
|
147
|
+
```ruby
|
148
|
+
a = ToWa(Arel::Table.new('test_records'), { '=': ['name', 'ToWa'] })
|
149
|
+
|
150
|
+
a.class
|
151
|
+
#=> Arel::Nodes::Equality
|
152
|
+
a.to_sql
|
153
|
+
#=> "`test_records`.`name` = 'ToWa'"
|
154
|
+
TestRecord.where(a).to_sql
|
155
|
+
#=> "SELECT `test_records`.* FROM `test_records` WHERE `test_records`.`name` = 'ToWa'"
|
156
|
+
```
|
data/lib/to_wa/builder.rb
CHANGED
@@ -5,24 +5,28 @@ module ToWa
|
|
5
5
|
include ActiveRecord::Sanitization
|
6
6
|
|
7
7
|
def to_wa(ar, ex)
|
8
|
-
ar.where(
|
8
|
+
ar.where(to_wa_raw(ar.arel_table, ex))
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
11
|
+
def to_wa_raw(arel_table = nil, ex)
|
12
|
+
decide(arel_table, ex.is_a?(String) ? JSON.parse(ex) : ex)
|
13
|
+
end
|
14
|
+
|
15
|
+
def decide(arel_table, ex)
|
12
16
|
op = ::ToWa::Core::OPERATORS[ex.keys.first.to_s]
|
13
17
|
values = ex.values.first
|
14
18
|
|
15
19
|
case op
|
16
20
|
when 'not'
|
17
|
-
decide(
|
21
|
+
decide(arel_table, values.first).not
|
18
22
|
when 'and', 'or'
|
19
|
-
add_logic(
|
23
|
+
add_logic(arel_table, op, values)
|
20
24
|
else
|
21
|
-
add_comparison(
|
25
|
+
add_comparison(arel_table, op, *values)
|
22
26
|
end
|
23
27
|
end
|
24
28
|
|
25
|
-
def add_comparison(
|
29
|
+
def add_comparison(arel_table, op, table, value)
|
26
30
|
normalized =
|
27
31
|
case op
|
28
32
|
when 'between'
|
@@ -33,13 +37,13 @@ module ToWa
|
|
33
37
|
value
|
34
38
|
end
|
35
39
|
|
36
|
-
|
40
|
+
arel_table[table].send(op, normalized)
|
37
41
|
end
|
38
42
|
|
39
|
-
def add_logic(
|
43
|
+
def add_logic(arel_table, op, exes)
|
40
44
|
logics =
|
41
45
|
exes.inject(nil) do |a, ex|
|
42
|
-
a.nil? ? decide(
|
46
|
+
a.nil? ? decide(arel_table, ex) : a.send(op, decide(arel_table, ex))
|
43
47
|
end
|
44
48
|
|
45
49
|
(exes.size == 1) ? logics : Arel::Nodes::Grouping.new(logics)
|
data/lib/to_wa/version.rb
CHANGED
data/lib/to_wa.rb
CHANGED
@@ -8,10 +8,14 @@ module ToWa
|
|
8
8
|
def to_wa(o)
|
9
9
|
::ToWa::Core.to_wa(self, o)
|
10
10
|
end
|
11
|
+
|
12
|
+
def to_wa_raw(arel_table, o)
|
13
|
+
ToWa(arel_table, o)
|
14
|
+
end
|
11
15
|
end
|
12
16
|
|
13
17
|
module Kernel
|
14
|
-
def ToWa(arel_table, o)
|
15
|
-
::ToWa::Core.
|
18
|
+
def ToWa(arel_table, o) # rubocop:disable Naming/MethodName
|
19
|
+
::ToWa::Core.to_wa_raw(arel_table, o)
|
16
20
|
end
|
17
21
|
end
|
data/to_wa.gemspec
CHANGED
@@ -24,9 +24,9 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_dependency 'activerecord'
|
25
25
|
|
26
26
|
spec.add_development_dependency 'bundler', '~> 1.16'
|
27
|
+
spec.add_development_dependency 'database_cleaner'
|
27
28
|
spec.add_development_dependency 'mysql2'
|
28
29
|
spec.add_development_dependency 'onkcop'
|
29
|
-
spec.add_development_dependency 'database_cleaner'
|
30
30
|
spec.add_development_dependency 'rake', '~> 10.0'
|
31
31
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
32
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: to_wa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mmmpa
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-01-
|
11
|
+
date: 2018-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.16'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: database_cleaner
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
@@ -53,7 +53,7 @@ dependencies:
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: mysql2
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
@@ -67,7 +67,7 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: onkcop
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|