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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36cf15c4c8069cda4565d54b6f37d8cebf615f7e
4
- data.tar.gz: d6f390f58ec8155f6a2cba7a226a77b8f3f47cc8
3
+ metadata.gz: ff02d9d70a9c571bba011ff3f651e559498cd6cd
4
+ data.tar.gz: 92bba9fd29b6adfd9035a16e512729bc54b6b092
5
5
  SHA512:
6
- metadata.gz: 00ebf0eb142deace0e9fd90fb7d543f808704b7674950e3e5cebb199758d32096bee2991225c0978f877b20e46a7b7385f4313f76d4951afc4530c9397f35391
7
- data.tar.gz: 127d4e190ed786378d95a5f87f97b4b86ffd8804fc11e8c9e42f9c17d143887a3f1c9fe40883d8e55a682eec748ca3975eadac80e119c508b1a5c982904bb2a3
6
+ metadata.gz: 75b7af23c3c6cf5cfb46ebd4533c9bb9bec6705e7b480b059071125bb2993fc3e7b017f1a25e5d2446ce52dd74b5916fe4bbabd3444b7f5e0eb2d2e9b17473ad
7
+ data.tar.gz: bea440ef0ff39602c6f265b8046ef359323f876b6b6b12a76beaabe5b8ae055953ca6b7306eca6cb27aa795845a60d6eae404de339bab05979a65e7f70fe8422
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- to_wa (0.1.0)
4
+ to_wa (0.2.0)
5
5
  activerecord
6
6
 
7
7
  GEM
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
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/to_wa`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Delete this and the text above, and describe your gem
13
+ ```console
14
+ bundle install
15
+ ```
6
16
 
7
- ## Installation
17
+ ```ruby
18
+ class TestRecord < ActiveRecord::Base
19
+ extend ToWa
20
+ end
21
+ ```
8
22
 
9
- Add this line to your application's Gemfile:
23
+ ## Simple usage
10
24
 
11
25
  ```ruby
12
- gem 'to_wa'
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
- And then execute:
43
+ ## Comparison operators
44
+
45
+ They receive `[left, right]`.
16
46
 
17
- $ bundle
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
- Or install it yourself as:
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
- ## Usage
100
+ # Usage
24
101
 
25
- TODO: Write usage instructions here
102
+ (Ofcourse, `ActiveRecord::Relation` will be provided after `to_wa` without `to_sql`.)
26
103
 
27
- ## Development
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
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
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
- ## Contributing
138
+ ## Working with other query
34
139
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/to_wa.
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
- ## License
145
+ ## Providing Arel::Nodes without ActiveRecord based class
38
146
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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(decide(ar, ex.is_a?(String) ? JSON.parse(ex) : ex))
8
+ ar.where(to_wa_raw(ar.arel_table, ex))
9
9
  end
10
10
 
11
- def decide(ar, ex)
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(ar, values.first).not
21
+ decide(arel_table, values.first).not
18
22
  when 'and', 'or'
19
- add_logic(ar, op, values)
23
+ add_logic(arel_table, op, values)
20
24
  else
21
- add_comparison(ar, op, *values)
25
+ add_comparison(arel_table, op, *values)
22
26
  end
23
27
  end
24
28
 
25
- def add_comparison(ar, op, table, value)
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
- ar.arel_table[table].send(op, normalized)
40
+ arel_table[table].send(op, normalized)
37
41
  end
38
42
 
39
- def add_logic(ar, op, exes)
43
+ def add_logic(arel_table, op, exes)
40
44
  logics =
41
45
  exes.inject(nil) do |a, ex|
42
- a.nil? ? decide(ar, ex) : a.send(op, decide(ar, ex))
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
@@ -1,3 +1,3 @@
1
1
  module ToWa
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
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.to_wa(arel_table, o)
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.1.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-19 00:00:00.000000000 Z
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: mysql2
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: onkcop
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: database_cleaner
70
+ name: onkcop
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="