to_wa 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 36cf15c4c8069cda4565d54b6f37d8cebf615f7e
4
+ data.tar.gz: d6f390f58ec8155f6a2cba7a226a77b8f3f47cc8
5
+ SHA512:
6
+ metadata.gz: 00ebf0eb142deace0e9fd90fb7d543f808704b7674950e3e5cebb199758d32096bee2991225c0978f877b20e46a7b7385f4313f76d4951afc4530c9397f35391
7
+ data.tar.gz: 127d4e190ed786378d95a5f87f97b4b86ffd8804fc11e8c9e42f9c17d143887a3f1c9fe40883d8e55a682eec748ca3975eadac80e119c508b1a5c982904bb2a3
@@ -0,0 +1,51 @@
1
+ version: 2
2
+ jobs:
3
+ build:
4
+ parallelism: 1
5
+ working_directory: ~/circleci-demo-ruby-rails
6
+ docker:
7
+ - image: circleci/ruby:2.4-node
8
+ environment:
9
+ BUNDLE_JOBS: 3
10
+ BUNDLE_RETRY: 3
11
+ BUNDLE_PATH: vendor/bundle
12
+ - image: circleci/mysql:5.7
13
+ environment:
14
+ MYSQL_DATABASE: to_wa_test_database
15
+ steps:
16
+ - checkout
17
+
18
+ # Which version of bundler?
19
+ - run:
20
+ name: Which bundler?
21
+ command: bundle -v
22
+
23
+ # Restore bundle cache
24
+ - restore_cache:
25
+ keys:
26
+ - rails-demo-bundle-v2-{{ checksum "Gemfile.lock" }}
27
+ - rails-demo-bundle-v2-
28
+
29
+ - run:
30
+ name: Bundle Install
31
+ command: bundle check || bundle install
32
+
33
+ # Store bundle cache
34
+ - save_cache:
35
+ key: rails-demo-bundle-v2-{{ checksum "Gemfile.lock" }}
36
+ paths:
37
+ - vendor/bundle
38
+
39
+ - run:
40
+ name: Wait for DB
41
+ command: dockerize -wait tcp://localhost:3306 -timeout 1m
42
+
43
+ - run:
44
+ name: Database setup
45
+ command: bundle exec ruby ./db/preparation.rb
46
+
47
+ # Run rspec in parallel
48
+ - type: shell
49
+ command: |
50
+ bundle exec rspec -cfd \
51
+ $(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
@@ -0,0 +1,17 @@
1
+ inherit_gem:
2
+ onkcop:
3
+ - "config/rubocop.yml"
4
+ # uncomment if use rails cops
5
+ # - "config/rails.yml"
6
+ # uncomment if use rspec cops
7
+ # - "config/rspec.yml"
8
+
9
+ AllCops:
10
+ TargetRubyVersion: 2.4
11
+ # uncomment if use rails cops
12
+ # TargetRailsVersion: 5.1
13
+ StringLiterals:
14
+ EnforcedStyle: single_quotes
15
+
16
+ Layout/IndentationConsistency:
17
+ EnforcedStyle: normal
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in to_wa.gemspec
6
+ gemspec
@@ -0,0 +1,80 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ to_wa (0.1.0)
5
+ activerecord
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.1.4)
11
+ activesupport (= 5.1.4)
12
+ activerecord (5.1.4)
13
+ activemodel (= 5.1.4)
14
+ activesupport (= 5.1.4)
15
+ arel (~> 8.0)
16
+ activesupport (5.1.4)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (~> 0.7)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (8.0.0)
22
+ ast (2.3.0)
23
+ concurrent-ruby (1.0.5)
24
+ database_cleaner (1.6.2)
25
+ diff-lcs (1.3)
26
+ i18n (0.9.1)
27
+ concurrent-ruby (~> 1.0)
28
+ minitest (5.11.1)
29
+ mysql2 (0.4.10)
30
+ onkcop (0.52.1.1)
31
+ rubocop (~> 0.52.1)
32
+ rubocop-rspec (>= 1.22.0)
33
+ parallel (1.12.1)
34
+ parser (2.4.0.2)
35
+ ast (~> 2.3)
36
+ powerpack (0.1.1)
37
+ rainbow (3.0.0)
38
+ rake (10.5.0)
39
+ rspec (3.7.0)
40
+ rspec-core (~> 3.7.0)
41
+ rspec-expectations (~> 3.7.0)
42
+ rspec-mocks (~> 3.7.0)
43
+ rspec-core (3.7.1)
44
+ rspec-support (~> 3.7.0)
45
+ rspec-expectations (3.7.0)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.7.0)
48
+ rspec-mocks (3.7.0)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.7.0)
51
+ rspec-support (3.7.0)
52
+ rubocop (0.52.1)
53
+ parallel (~> 1.10)
54
+ parser (>= 2.4.0.2, < 3.0)
55
+ powerpack (~> 0.1)
56
+ rainbow (>= 2.2.2, < 4.0)
57
+ ruby-progressbar (~> 1.7)
58
+ unicode-display_width (~> 1.0, >= 1.0.1)
59
+ rubocop-rspec (1.22.0)
60
+ rubocop (>= 0.52.1)
61
+ ruby-progressbar (1.9.0)
62
+ thread_safe (0.3.6)
63
+ tzinfo (1.2.4)
64
+ thread_safe (~> 0.1)
65
+ unicode-display_width (1.3.0)
66
+
67
+ PLATFORMS
68
+ ruby
69
+
70
+ DEPENDENCIES
71
+ bundler (~> 1.16)
72
+ database_cleaner
73
+ mysql2
74
+ onkcop
75
+ rake (~> 10.0)
76
+ rspec (~> 3.0)
77
+ to_wa!
78
+
79
+ BUNDLED WITH
80
+ 1.16.0
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 mmmpa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # ToWa
2
+
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.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'to_wa'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install to_wa
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
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.
30
+
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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/to_wa.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'to_wa'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,15 @@
1
+ class ToWaTestConfiguration
2
+ BASE = {
3
+ adapter: :mysql2,
4
+ host: ENV['DB_HOST'] || '127.0.0.1',
5
+ port: ENV['DB_PORT'] || '3306',
6
+ username: ENV['DB_USER'] || 'root',
7
+ password: ENV['DB_PASSWORD'] || '',
8
+ }.freeze
9
+
10
+ DB_NAME = 'to_wa_test_database'.freeze
11
+
12
+ FULL_SET = BASE.merge(
13
+ database: DB_NAME,
14
+ )
15
+ end
@@ -0,0 +1,22 @@
1
+ class ToWaTestMigration < ActiveRecord::Migration[5.1]
2
+ class << self
3
+ def up
4
+ create_table(:test_records) do |t|
5
+ t.string :a
6
+ t.string :b
7
+ t.string :c
8
+ t.integer :x
9
+ t.integer :y
10
+ t.integer :z
11
+ end
12
+ rescue
13
+ nil
14
+ end
15
+
16
+ def down
17
+ drop_table(:test_records)
18
+ rescue
19
+ nil
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ require 'active_record'
2
+
3
+ require './db/configuration'
4
+ require './db/migration'
5
+
6
+ ActiveRecord::Base.establish_connection(ToWaTestConfiguration::BASE)
7
+ begin
8
+ ActiveRecord::Base.connection.create_database(ToWaTestConfiguration::DB_NAME)
9
+ rescue ActiveRecord::StatementInvalid => e
10
+ puts e
11
+ end
12
+
13
+ ActiveRecord::Base.establish_connection(ToWaTestConfiguration::FULL_SET)
14
+ ToWaTestMigration.up
@@ -0,0 +1,17 @@
1
+ require 'to_wa/version'
2
+ require 'active_record'
3
+
4
+ module ToWa
5
+ require 'to_wa/builder'
6
+ require 'to_wa/core'
7
+
8
+ def to_wa(o)
9
+ ::ToWa::Core.to_wa(self, o)
10
+ end
11
+ end
12
+
13
+ module Kernel
14
+ def ToWa(arel_table, o)
15
+ ::ToWa::Core.to_wa(arel_table, o)
16
+ end
17
+ end
@@ -0,0 +1,52 @@
1
+ require 'json'
2
+
3
+ module ToWa
4
+ module Builder
5
+ include ActiveRecord::Sanitization
6
+
7
+ def to_wa(ar, ex)
8
+ ar.where(decide(ar, ex.is_a?(String) ? JSON.parse(ex) : ex))
9
+ end
10
+
11
+ def decide(ar, ex)
12
+ op = ::ToWa::Core::OPERATORS[ex.keys.first.to_s]
13
+ values = ex.values.first
14
+
15
+ case op
16
+ when 'not'
17
+ decide(ar, values.first).not
18
+ when 'and', 'or'
19
+ add_logic(ar, op, values)
20
+ else
21
+ add_comparison(ar, op, *values)
22
+ end
23
+ end
24
+
25
+ def add_comparison(ar, op, table, value)
26
+ normalized =
27
+ case op
28
+ when 'between'
29
+ value.first..value.second
30
+ when 'matches'
31
+ "%#{ToWa::Builder.like(value)}%"
32
+ else
33
+ value
34
+ end
35
+
36
+ ar.arel_table[table].send(op, normalized)
37
+ end
38
+
39
+ def add_logic(ar, op, exes)
40
+ logics =
41
+ exes.inject(nil) do |a, ex|
42
+ a.nil? ? decide(ar, ex) : a.send(op, decide(ar, ex))
43
+ end
44
+
45
+ (exes.size == 1) ? logics : Arel::Nodes::Grouping.new(logics)
46
+ end
47
+
48
+ def self.like(v)
49
+ sanitize_sql_like(v)
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,38 @@
1
+ module ToWa
2
+ module Core
3
+ extend ::ToWa::Builder
4
+
5
+ COMPARISON = {
6
+ '==' => 'eq',
7
+ '=' => 'eq',
8
+ 'eq' => 'eq',
9
+ '!=' => 'not_eq',
10
+ '<>' => 'not_eq',
11
+ 'ne' => 'not_eq',
12
+ '>' => 'gt',
13
+ 'gt' => 'gt',
14
+ '>=' => 'gteq',
15
+ 'gteq' => 'gteq',
16
+ '<' => 'lt',
17
+ 'lt' => 'lt',
18
+ '<=' => 'lteq',
19
+ 'lteq' => 'lteq',
20
+ 'matches' => 'matches',
21
+ 'like' => 'matches',
22
+ 'in' => 'in',
23
+ 'between' => 'between',
24
+ }.freeze
25
+
26
+ LOGICAL = {
27
+ '&&' => 'and',
28
+ 'and' => 'and',
29
+ '||' => 'or',
30
+ 'or' => 'or',
31
+ 'not' => 'not',
32
+ }.freeze
33
+
34
+ OPERATORS = COMPARISON.merge(LOGICAL)
35
+
36
+ ALLOW = Set.new(OPERATORS.keys)
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module ToWa
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'to_wa/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'to_wa'
8
+ spec.version = ToWa::VERSION
9
+ spec.authors = ['mmmpa']
10
+ spec.email = ['mmmpa.mmmpa@gmail.com']
11
+
12
+ spec.summary = 'to where arel'
13
+ spec.description = 'to where arel'
14
+ spec.homepage = 'http://mmmpa.net'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'activerecord'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.16'
27
+ spec.add_development_dependency 'mysql2'
28
+ spec.add_development_dependency 'onkcop'
29
+ spec.add_development_dependency 'database_cleaner'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'rspec', '~> 3.0'
32
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: to_wa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mmmpa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mysql2
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: onkcop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: database_cleaner
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ description: to where arel
112
+ email:
113
+ - mmmpa.mmmpa@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".circleci/config.yml"
119
+ - ".gitignore"
120
+ - ".rubocop.yml"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - Gemfile.lock
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - db/configuration.rb
130
+ - db/migration.rb
131
+ - db/preparation.rb
132
+ - lib/to_wa.rb
133
+ - lib/to_wa/builder.rb
134
+ - lib/to_wa/core.rb
135
+ - lib/to_wa/version.rb
136
+ - to_wa.gemspec
137
+ homepage: http://mmmpa.net
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.6.13
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: to where arel
161
+ test_files: []