activerecord-hash_options 0.1.0
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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +1 -0
- data/.travis.yml +26 -0
- data/Appraisals +25 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +88 -0
- data/Rakefile +8 -0
- data/activerecord-hash_options.gemspec +29 -0
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/gemfiles/gemfile_50.gemfile +10 -0
- data/gemfiles/gemfile_51.gemfile +10 -0
- data/gemfiles/gemfile_52.gemfile +10 -0
- data/gemfiles/gemfile_60.gemfile +10 -0
- data/init.rb +1 -0
- data/lib/active_record-hash_options.rb +1 -0
- data/lib/active_record/hash_options.rb +83 -0
- data/lib/active_record/hash_options/enumerable.rb +26 -0
- data/lib/active_record/hash_options/helpers.rb +22 -0
- data/lib/active_record/hash_options/operators.rb +137 -0
- data/lib/active_record/hash_options/version.rb +6 -0
- data/lib/activerecord-hash_options.rb +1 -0
- data/lib/activerecord/hash_options.rb +1 -0
- metadata +125 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 8813e9aab70d04b26a259dc942ce9d72ff617ab08bce5110efff8913314f74dd
|
|
4
|
+
data.tar.gz: 5066d7c5a45c2d815dc6cb8baefc45321cecdbd098dff6cde8570c689703b80e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 25f350ebb9ad1a614cbe2d853a4bb6a104d90064b2a1e363a571a04c775440ba476ef1f892a1dc0706ee9b723662588e5b9f76d9e07bfd9b17e62240061a88e7
|
|
7
|
+
data.tar.gz: a1567cb1f71a1a71b5af8c2358ad80b5de6f440625e9c119069230bf21178c283d29b8051ae6233cc2473cb5f13d262e95413cb8ae23659a28c94ae99a392448
|
data/.gitignore
ADDED
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--require spec_helper
|
data/.travis.yml
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
---
|
|
2
|
+
sudo: false
|
|
3
|
+
language: ruby
|
|
4
|
+
cache: bundler
|
|
5
|
+
|
|
6
|
+
rvm:
|
|
7
|
+
- 2.5.3
|
|
8
|
+
|
|
9
|
+
services:
|
|
10
|
+
- mysql
|
|
11
|
+
- postgresql
|
|
12
|
+
|
|
13
|
+
env:
|
|
14
|
+
- DB=mysql2
|
|
15
|
+
- DB=pg
|
|
16
|
+
- DB=sqlite3
|
|
17
|
+
|
|
18
|
+
gemfile:
|
|
19
|
+
- gemfiles/gemfile_50.gemfile
|
|
20
|
+
- gemfiles/gemfile_51.gemfile
|
|
21
|
+
- gemfiles/gemfile_52.gemfile
|
|
22
|
+
- gemfiles/gemfile_60.gemfile
|
|
23
|
+
|
|
24
|
+
before_script:
|
|
25
|
+
- [ '$DB' = 'mysql2' ] && mysql -e 'create database hash_options_test;' || true
|
|
26
|
+
- [ '$DB' = 'pg' ] && psql -c 'create database hash_options_test;' -U postgres || true
|
data/Appraisals
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
%w(5.0.7 5.1.7 5.2.3 6.0.0).each do |ar_version|
|
|
2
|
+
appraise "gemfile-#{ar_version.split('.').first(2).join}" do
|
|
3
|
+
gem "activerecord", "~> #{ar_version}"
|
|
4
|
+
|
|
5
|
+
if ar_version >= "5.0"
|
|
6
|
+
gem "mysql2"
|
|
7
|
+
elsif ar_version >= "4.2"
|
|
8
|
+
gem "mysql2", "~> 0.4.0"
|
|
9
|
+
else
|
|
10
|
+
gem "mysql"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
if ar_version >= "5.0"
|
|
14
|
+
gem "pg"
|
|
15
|
+
else
|
|
16
|
+
gem "pg", "0.18.4"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
if ar_version >= "5.2"
|
|
20
|
+
gem "sqlite3"
|
|
21
|
+
else
|
|
22
|
+
gem "sqlite3", "~> 1.3.13"
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 Keenan Brock
|
|
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.
|
data/README.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# ActiveRecord::HashOptions
|
|
2
|
+
|
|
3
|
+
This extends the hash options passed into ActiveRecord `where`.
|
|
4
|
+
|
|
5
|
+
Thanks to the example from [codesnik](https://gist.github.com/codesnik/2ebba1940c05b08b17f9)
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'activerecord-hash_options'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install activerecord-hash_options
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
There are a number of ways to use active record hash options.
|
|
26
|
+
It all depends upon how much you want to monkey patch your environment
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
require 'active_record/hash_options'
|
|
30
|
+
|
|
31
|
+
Person.where(:name => ActiveRecord::HashOptions::LIKE('Smith%'))
|
|
32
|
+
Person.where(:age => ActiveRecord::HashOptions::GTE(21))
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
```ruby
|
|
38
|
+
require 'active_record/hash_options'
|
|
39
|
+
include ActiveRecord::HashOptions
|
|
40
|
+
|
|
41
|
+
Person.where(:name => LIKE('Smith%'))
|
|
42
|
+
Person.where.not(:age => GTE(21))
|
|
43
|
+
|
|
44
|
+
ActiveRecord::HashOptions.filter(Person.all, :name => LIKE('Smith%'))
|
|
45
|
+
ActiveRecord::HashOptions.filter(Person.all.to_a, :name => LIKE('Smith%'))
|
|
46
|
+
ActiveRecord::HashOptions.filter(Person.all.to_a, :age => GTE(21), true)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
require 'active_record/hash_options'
|
|
53
|
+
include ActiveRecord::HashOptions::Helpers
|
|
54
|
+
|
|
55
|
+
Person.where(:name => like('Smith%'))
|
|
56
|
+
Person.where.not(:age => gte(21))
|
|
57
|
+
|
|
58
|
+
ActiveRecord::HashOptions.filter(Person.all.to_a, :name => like('Smith%'))
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
require 'active_record/hash_options'
|
|
65
|
+
include ActiveRecord::HashOptions::Helpers
|
|
66
|
+
Array.send(:include, ActiveRecord::HashOptions::Enumerable)
|
|
67
|
+
|
|
68
|
+
Person.all.to_a.where(:name => like('Smith%'))
|
|
69
|
+
Person.all.to_a.where.not(:age => gte(21))
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Development
|
|
74
|
+
|
|
75
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
|
|
76
|
+
|
|
77
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment. Model `Table1` will be available
|
|
78
|
+
for your convenience
|
|
79
|
+
|
|
80
|
+
## Contributing
|
|
81
|
+
|
|
82
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kbrock/activerecord-hash_options.
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
## License
|
|
86
|
+
|
|
87
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
|
88
|
+
|
data/Rakefile
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'active_record/hash_options/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "activerecord-hash_options"
|
|
8
|
+
spec.version = ActiveRecord::HashOptions::VERSION
|
|
9
|
+
spec.authors = ["Keenan Brock"]
|
|
10
|
+
spec.email = ["keenan@thebrocks.net"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Give ActiveRecord where hashes more power }
|
|
13
|
+
spec.description = %q{Gives ActiveRecord where hashes more power like the ability to gt or like}
|
|
14
|
+
spec.homepage = "https://github.com/kbrock/activerecord-hash_options/"
|
|
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.require_paths = ["lib"]
|
|
21
|
+
|
|
22
|
+
# We use appraisal to generate the gemfiles
|
|
23
|
+
# but other than that, we do not use appraisal during the runtime/development time of gem
|
|
24
|
+
# spec.add_development_dependency "appraisal"
|
|
25
|
+
spec.add_development_dependency "rake", ">= 12.3.3"
|
|
26
|
+
spec.add_development_dependency "rspec-core", "~>3.8.0"
|
|
27
|
+
spec.add_development_dependency "rspec-expectations", "~>3.8.0"
|
|
28
|
+
spec.add_development_dependency "activerecord", ">= 5.0"
|
|
29
|
+
end
|
data/bin/console
ADDED
data/bin/setup
ADDED
data/init.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "activerecord-hash_options"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "active_record/hash_options"
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
require "active_record/hash_options/version"
|
|
3
|
+
require "active_record/hash_options/operators"
|
|
4
|
+
require "active_record/hash_options/helpers"
|
|
5
|
+
require "active_record/hash_options/enumerable"
|
|
6
|
+
|
|
7
|
+
module ActiveRecord
|
|
8
|
+
module HashOptions
|
|
9
|
+
def self.extended(mod)
|
|
10
|
+
ActiveRecord::HashOptions.register_my_handler(mod)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.inherited(mod)
|
|
14
|
+
ActiveRecord::HashOptions.register_my_handler(mod)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.register_my_handler(mod)
|
|
18
|
+
if mod < ActiveRecord::Base && mod.kind_of?(Class)
|
|
19
|
+
mod.predicate_builder.register_handler(GT, GT.arel_proc)
|
|
20
|
+
mod.predicate_builder.register_handler(LT, LT.arel_proc)
|
|
21
|
+
mod.predicate_builder.register_handler(GTE, GTE.arel_proc)
|
|
22
|
+
mod.predicate_builder.register_handler(LTE, LTE.arel_proc)
|
|
23
|
+
mod.predicate_builder.register_handler(INSENSITIVE, INSENSITIVE.arel_proc)
|
|
24
|
+
mod.predicate_builder.register_handler(LIKE, LIKE.arel_proc)
|
|
25
|
+
mod.predicate_builder.register_handler(NOT_LIKE, NOT_LIKE.arel_proc)
|
|
26
|
+
# for postgres:
|
|
27
|
+
mod.predicate_builder.register_handler(ILIKE, ILIKE.arel_proc)
|
|
28
|
+
|
|
29
|
+
# NOTE: Probably want Regexp over REGEXP (e.g.: where(:name => /value/i))
|
|
30
|
+
mod.predicate_builder.register_handler(Regexp, REGEXP.arel_proc)
|
|
31
|
+
mod.predicate_builder.register_handler(REGEXP, REGEXP.arel_proc)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.filter(scope_or_array, conditions, negate = false)
|
|
36
|
+
if scope_or_array.kind_of?(Array)
|
|
37
|
+
filter_array(scope_or_array, conditions, negate)
|
|
38
|
+
else
|
|
39
|
+
filter_scope(scope_or_array, conditions, negate)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.filter_scope(scope, conditions, negate)
|
|
44
|
+
if negate
|
|
45
|
+
scope.where.not(conditions)
|
|
46
|
+
else
|
|
47
|
+
scope.where(conditions)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.filter_array(array, conditions, negate)
|
|
52
|
+
array.select do |rec|
|
|
53
|
+
conditions.all? do |name, value|
|
|
54
|
+
actual_val = rec.send(name)
|
|
55
|
+
# not thrilled about special cases, but `x in [..., nil]` and `x == nil` are the only cases
|
|
56
|
+
# that handle negation for a `nil` / `null` value correctly
|
|
57
|
+
if actual_val.nil? && (value != nil || value.kind_of?(Array) && !value.include?(nil))
|
|
58
|
+
false
|
|
59
|
+
else
|
|
60
|
+
compare_array_column(actual_val, value) ? !negate : negate
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def self.compare_array_column(actual_val, value)
|
|
67
|
+
case value
|
|
68
|
+
when Regexp
|
|
69
|
+
actual_val =~ value
|
|
70
|
+
when Array
|
|
71
|
+
value.include?(actual_val)
|
|
72
|
+
when Range
|
|
73
|
+
value.cover?(actual_val)
|
|
74
|
+
when ActiveRecord::HashOptions::GenericOp
|
|
75
|
+
# NOTE: The `nil?` check in `filter_array` may skip this comparison and short circuit to a false
|
|
76
|
+
value.call(actual_val)
|
|
77
|
+
else # NilClass, String, Integer
|
|
78
|
+
# NOTE: this treats `x == nil` the same as `x IS NULL`
|
|
79
|
+
actual_val == value
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# include into any file to add class to make Array#where
|
|
4
|
+
module ActiveRecord
|
|
5
|
+
module HashOptions
|
|
6
|
+
module Enumerable
|
|
7
|
+
def where(conditions = :chain)
|
|
8
|
+
if :chain == conditions
|
|
9
|
+
ActiveRecord::HashOptions::Enumerable::NotChain.new(self)
|
|
10
|
+
else
|
|
11
|
+
ActiveRecord::HashOptions.filter(self, conditions, false)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class NotChain
|
|
16
|
+
def initialize(collection)
|
|
17
|
+
@collection = collection
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def not(conditions = {})
|
|
21
|
+
ActiveRecord::HashOptions.filter(@collection, conditions, true)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
module ActiveRecord
|
|
3
|
+
module HashOptions
|
|
4
|
+
module Helpers
|
|
5
|
+
# numeric
|
|
6
|
+
def gt(val); ActiveRecord::HashOptions::GT.new(val); end
|
|
7
|
+
def lt(val); ActiveRecord::HashOptions::LT.new(val); end
|
|
8
|
+
def gte(val); ActiveRecord::HashOptions::GTE.new(val); end
|
|
9
|
+
def lte(val); ActiveRecord::HashOptions::LTE.new(val); end
|
|
10
|
+
|
|
11
|
+
# string
|
|
12
|
+
def insensitive(val); ActiveRecord::HashOptions::INSENSITIVE.new(val); end
|
|
13
|
+
|
|
14
|
+
def starts_with(val) ; ActiveRecord::HashOptions::LIKE.new("#{val}%"); end
|
|
15
|
+
def ends_with(val) ; ActiveRecord::HashOptions::LIKE.new("%#{val}"); end
|
|
16
|
+
def contains(val) ; ActiveRecord::HashOptions::LIKE.new("%#{val}%"); end
|
|
17
|
+
def not_like(val) ; ActiveRecord::HashOptions::NOT_LIKE.new(val); end
|
|
18
|
+
def like(val); ActiveRecord::HashOptions::LIKE.new(val); end
|
|
19
|
+
def ilike(val); ActiveRecord::HashOptions::ILIKE.new(val); end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
module ActiveRecord
|
|
3
|
+
module HashOptions
|
|
4
|
+
# Operator contains logic for producing arel for the database and acts as a lambda for ruby
|
|
5
|
+
class GenericOp
|
|
6
|
+
attr_accessor :expression, :expression2
|
|
7
|
+
|
|
8
|
+
def initialize(expression, expression2 = nil)
|
|
9
|
+
@expression = expression
|
|
10
|
+
@expression2 = expression2
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.quote(op_expression, column)
|
|
14
|
+
if op_expression.kind_of?(String)
|
|
15
|
+
Arel::Nodes.build_quoted(op_expression, column)
|
|
16
|
+
else
|
|
17
|
+
op_expression
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class GT < GenericOp
|
|
23
|
+
def self.arel_proc
|
|
24
|
+
proc { |column, op| Arel::Nodes::GreaterThan.new(column, GenericOp.quote(op.expression, column)) }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def call(val)
|
|
28
|
+
val && val > expression
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
class LT < GenericOp
|
|
33
|
+
def self.arel_proc
|
|
34
|
+
proc { |column, op| Arel::Nodes::LessThan.new(column, GenericOp.quote(op.expression, column)) }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def call(val)
|
|
38
|
+
val && val < expression
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
class GTE < GenericOp
|
|
43
|
+
def self.arel_proc
|
|
44
|
+
proc { |column, op| Arel::Nodes::GreaterThanOrEqual.new(column, GenericOp.quote(op.expression, column)) }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def call(val)
|
|
48
|
+
val && val >= expression
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
class LTE < GenericOp
|
|
53
|
+
def self.arel_proc
|
|
54
|
+
proc { |column, op| Arel::Nodes::LessThanOrEqual.new(column, GenericOp.quote(op.expression, column)) }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def call(val)
|
|
58
|
+
val && val <= expression
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
class INSENSITIVE < GenericOp
|
|
63
|
+
def self.arel_proc
|
|
64
|
+
proc do |column, op|
|
|
65
|
+
lower_column = Arel::Nodes::NamedFunction.new("LOWER", [column])
|
|
66
|
+
Arel::Nodes::Equality.new(lower_column, GenericOp.quote(op.expression.downcase, column))
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def call(val)
|
|
71
|
+
val&.downcase == expression&.downcase
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Ruby doesn't have like, so I converted regex to like best I could
|
|
76
|
+
# We could also do the reverse for database operations
|
|
77
|
+
class LIKE < GenericOp
|
|
78
|
+
def self.arel_proc
|
|
79
|
+
proc { |column, op| Arel::Nodes::Matches.new(column, GenericOp.quote(op.expression, column), nil, true) }
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def call(val)
|
|
83
|
+
expression2 ||= like_to_regex(expression)
|
|
84
|
+
val && val =~ expression2
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# escape . * ^ $ (this.gif => this[.]gif - so it won't match this_gif)
|
|
88
|
+
# leave [] as [] (use by like and regular expressions)
|
|
89
|
+
# convert % => .*, _ => .
|
|
90
|
+
# convert ^.*abc$ => abc$
|
|
91
|
+
# convert ^abc.*$ => ^abc
|
|
92
|
+
# @param extra ilike passes in Regexp::IGNORECASE
|
|
93
|
+
def like_to_regex(lk, extra = nil)
|
|
94
|
+
exp = lk.gsub(/([.*^$])/) {"[#{$1}]"} # escape special characters
|
|
95
|
+
exp = "^#{exp}$".gsub("%", '.*').gsub("_", ".").gsub(/^\.\*/, '').gsub(/\.\*$/, '')
|
|
96
|
+
Regexp.new(exp, extra)
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
class NOT_LIKE < LIKE
|
|
101
|
+
def self.arel_proc
|
|
102
|
+
proc { |column, op| Arel::Nodes::DoesNotMatch.new(column, GenericOp.quote(op.expression, column), nil, true) }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def call(val)
|
|
106
|
+
expression2 ||= like_to_regex(expression)
|
|
107
|
+
val && val !~ expression2
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
class ILIKE < LIKE
|
|
112
|
+
def self.arel_proc
|
|
113
|
+
proc { |column, op| Arel::Nodes::Matches.new(column, GenericOp.quote(op.expression, column), nil, false) }
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def like_to_regex(lk)
|
|
117
|
+
super(lk, Regexp::IGNORECASE)
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# typically, a Regexp will go through, so this should not becalled
|
|
122
|
+
# this is here to group proc for regexp with others
|
|
123
|
+
class REGEXP < GenericOp
|
|
124
|
+
def self.arel_proc
|
|
125
|
+
proc do |column, op|
|
|
126
|
+
regexp_text = Arel::Nodes.build_quoted(op.source, column)
|
|
127
|
+
case_sensitive = (op.options & Regexp::IGNORECASE == 0)
|
|
128
|
+
Arel::Nodes::Regexp.new(column, regexp_text, case_sensitive)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def call(val)
|
|
133
|
+
val && val =~ expression
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "active_record/hash_options"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "active_record/hash_options"
|
metadata
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: activerecord-hash_options
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Keenan Brock
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2020-05-21 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rake
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 12.3.3
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 12.3.3
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rspec-core
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 3.8.0
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 3.8.0
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec-expectations
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 3.8.0
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 3.8.0
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: activerecord
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '5.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '5.0'
|
|
69
|
+
description: Gives ActiveRecord where hashes more power like the ability to gt or
|
|
70
|
+
like
|
|
71
|
+
email:
|
|
72
|
+
- keenan@thebrocks.net
|
|
73
|
+
executables: []
|
|
74
|
+
extensions: []
|
|
75
|
+
extra_rdoc_files: []
|
|
76
|
+
files:
|
|
77
|
+
- ".gitignore"
|
|
78
|
+
- ".rspec"
|
|
79
|
+
- ".travis.yml"
|
|
80
|
+
- Appraisals
|
|
81
|
+
- Gemfile
|
|
82
|
+
- LICENSE.txt
|
|
83
|
+
- README.md
|
|
84
|
+
- Rakefile
|
|
85
|
+
- activerecord-hash_options.gemspec
|
|
86
|
+
- bin/console
|
|
87
|
+
- bin/setup
|
|
88
|
+
- gemfiles/gemfile_50.gemfile
|
|
89
|
+
- gemfiles/gemfile_51.gemfile
|
|
90
|
+
- gemfiles/gemfile_52.gemfile
|
|
91
|
+
- gemfiles/gemfile_60.gemfile
|
|
92
|
+
- init.rb
|
|
93
|
+
- lib/active_record-hash_options.rb
|
|
94
|
+
- lib/active_record/hash_options.rb
|
|
95
|
+
- lib/active_record/hash_options/enumerable.rb
|
|
96
|
+
- lib/active_record/hash_options/helpers.rb
|
|
97
|
+
- lib/active_record/hash_options/operators.rb
|
|
98
|
+
- lib/active_record/hash_options/version.rb
|
|
99
|
+
- lib/activerecord-hash_options.rb
|
|
100
|
+
- lib/activerecord/hash_options.rb
|
|
101
|
+
homepage: https://github.com/kbrock/activerecord-hash_options/
|
|
102
|
+
licenses:
|
|
103
|
+
- MIT
|
|
104
|
+
metadata: {}
|
|
105
|
+
post_install_message:
|
|
106
|
+
rdoc_options: []
|
|
107
|
+
require_paths:
|
|
108
|
+
- lib
|
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
110
|
+
requirements:
|
|
111
|
+
- - ">="
|
|
112
|
+
- !ruby/object:Gem::Version
|
|
113
|
+
version: '0'
|
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
115
|
+
requirements:
|
|
116
|
+
- - ">="
|
|
117
|
+
- !ruby/object:Gem::Version
|
|
118
|
+
version: '0'
|
|
119
|
+
requirements: []
|
|
120
|
+
rubyforge_project:
|
|
121
|
+
rubygems_version: 2.7.6.2
|
|
122
|
+
signing_key:
|
|
123
|
+
specification_version: 4
|
|
124
|
+
summary: Give ActiveRecord where hashes more power
|
|
125
|
+
test_files: []
|