whereable 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4fb9a55c5cc4baff3d60aaaa56e5ce74fde2a8a2b20ff4abec5ca7268cca927c
4
+ data.tar.gz: dd20820bcb8b5a46bdd30458e9c135f41892db53062caeb088590e98e33851c3
5
+ SHA512:
6
+ metadata.gz: '055580e74876c9f5e1746ce1eb16d2fba7a20efc9fb32f87b1a0fe6faf5648d84b35f9435d11c1749c856d7ab8ee8f0f6f87bcf9f21b7ce7dbaf5a707765fa2b'
7
+ data.tar.gz: 05e88aef960b3a18d6dcc72d119a8e172adb3c83256215cb1555198dbd9b330b5dfd79b7203e7f3ad9258a40af5956efc5bcdbe9d62882fe783d9f3f9e48762d
File without changes
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Mack Earnhardt
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,40 @@
1
+ # Whereable
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/whereable`. 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 'whereable'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install whereable
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]/whereable.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,147 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020 Mack Earnhardt
4
+
5
+ require 'whereable/version'
6
+ require 'active_record'
7
+ require 'active_support'
8
+ require 'treetop'
9
+ require 'whereable.treetop'
10
+
11
+ # Whereable module
12
+ module Whereable
13
+ extend ActiveSupport::Concern
14
+
15
+ class FilterInvalid < StandardError; end
16
+
17
+ module Disjunction
18
+ # Hash for OR
19
+ def to_h
20
+ return conjunction.to_h if opt.empty?
21
+
22
+ {
23
+ or: [conjunction.to_h] + opt.elements.map { |o| o.conjunction.to_h },
24
+ }
25
+ end
26
+ end
27
+
28
+ module Conjunction
29
+ # Hash for AND
30
+ def to_h
31
+ return term.to_h if opt.empty?
32
+
33
+ {
34
+ and: [term.to_h] + opt.elements.map { |o| o.term.to_h },
35
+ }
36
+ end
37
+ end
38
+
39
+ module NestedExpression
40
+ # Hash for nested expression
41
+ delegate :to_h, to: :expression
42
+ end
43
+
44
+ module Condition
45
+ # Hash for comparison
46
+ def to_h
47
+ {
48
+ operator.to_sym => {
49
+ column: column.to_s,
50
+ literal: literal.to_s,
51
+ },
52
+ }
53
+ end
54
+ end
55
+
56
+ module Column
57
+ # String column name
58
+ def to_s
59
+ text_value
60
+ end
61
+ end
62
+
63
+ # Operators such as 'eq' that match their Arel equivalent aren't needed here
64
+ OP_SYMS = {
65
+ 'ne' => :not_eq,
66
+ 'lte' => :lteq,
67
+ 'gte' => :gteq,
68
+ '=' => :eq,
69
+ '!=' => :not_eq,
70
+ '<>' => :not_eq,
71
+ '<' => :lt,
72
+ '<=' => :lteq,
73
+ '>' => :gt,
74
+ '>=' => :gteq,
75
+ }.freeze
76
+ private_constant :OP_SYMS
77
+
78
+ module Operator
79
+ # Arel comparion method
80
+ def to_sym
81
+ OP_SYMS[text_value] || text_value.downcase.to_sym
82
+ end
83
+ end
84
+
85
+ module Literal
86
+ # Strip enclosing quotes and tranlate embedded quote patterns
87
+ def to_s
88
+ text_value
89
+ .gsub(/\A['"]|['"]\z/, '')
90
+ .gsub("''", "'")
91
+ .gsub("\\'", "'")
92
+ .gsub('\"', '"')
93
+ end
94
+ end
95
+
96
+ included do
97
+ scope(
98
+ :whereable,
99
+ lambda do |filter|
100
+ return all if filter.blank?
101
+
102
+ hash_tree = whereable_hash_tree(filter)
103
+
104
+ where(whereable_deparse(hash_tree))
105
+ end,
106
+ )
107
+ end
108
+
109
+ class_methods do
110
+ # Parse filter to hash tree using Treetop PEG
111
+ def whereable_hash_tree(filter)
112
+ parser = WhereableParser.new
113
+ hash = parser.parse(filter)&.to_h
114
+
115
+ raise FilterInvalid, "Invalid filter at #{filter[parser.max_terminal_failure_index..-1]}" if hash.nil?
116
+
117
+ hash
118
+ end
119
+
120
+ # deparse hash tree to Arel
121
+ def whereable_deparse(hash)
122
+ raise FilterInvalid, "Invalid hash #{hash}" if hash.size > 1
123
+
124
+ key, value = hash.first
125
+
126
+ case key
127
+ when :and, :or
128
+ arel = whereable_deparse(value.first)
129
+ value[1..-1].each do |o|
130
+ arel = arel.public_send(key, whereable_deparse(o))
131
+ end
132
+ arel
133
+ when :eq, :not_eq, :gt, :gteq, :lt, :lteq
134
+ column = value[:column]
135
+ literal = value[:literal]
136
+ raise FilterInvalid, "Invalid column #{column}" unless column_names.include?(column)
137
+
138
+ if defined_enums.key?(column)
139
+ literal = defined_enums[column][literal] || raise(FilterInvalid, "Invalid value #{literal} for #{column}")
140
+ end
141
+ arel_table[column].public_send(key, literal)
142
+ else
143
+ raise FilterInvalid, "Invalid hash #{hash}"
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,37 @@
1
+ grammar Whereable
2
+ rule expression
3
+ disjunction
4
+ end
5
+
6
+ rule disjunction
7
+ conjunction opt:( _? 'or'i _? conjunction )* <Disjunction>
8
+ end
9
+
10
+ rule conjunction
11
+ term opt:( _? 'and'i _? term )* <Conjunction>
12
+ end
13
+
14
+ rule term
15
+ condition / '(' _? expression _? ')' <NestedExpression>
16
+ end
17
+
18
+ rule condition
19
+ column _? operator _? literal <Condition>
20
+ end
21
+
22
+ rule _
23
+ [\s]+
24
+ end
25
+
26
+ rule column
27
+ [a-zA-Z0-9_]+ <Column>
28
+ end
29
+
30
+ rule operator
31
+ ( 'eq' / 'ne' / 'gte' / 'gt' / 'lte' / 'lt' / '=' / '!=' / '<>' / '>=' / '>' / '<=' / '<' ) <Operator>
32
+ end
33
+
34
+ rule literal
35
+ ( '"' ( '\"' / [^"] )+ '"' / "'" ( "''" / "\\'" / [^'] )+ "'" / [^\s)]+ ) <Literal>
36
+ end
37
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020 Mack Earnhardt
4
+
5
+ module Whereable
6
+ VERSION = '0.1.0'
7
+ public_constant :VERSION
8
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: whereable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mack Earnhardt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-11-12 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: treetop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: 'Translate where-like filter syntax into Arel-based ActiveRecord scope.
42
+
43
+ '
44
+ email:
45
+ - mack@agilereasoning.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - "./CHANGELOG.md"
51
+ - "./LICENSE.txt"
52
+ - "./README.md"
53
+ - "./lib/whereable.treetop"
54
+ - lib/whereable.rb
55
+ - lib/whereable/version.rb
56
+ homepage: https://github.com/MacksMind/whereable
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ homepage_uri: https://github.com/MacksMind/whereable
61
+ source_code_uri: https://github.com/MacksMind/whereable
62
+ changelog_uri: https://github.com/MacksMind/whereable/blob/master/CHANGELOG.md
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 2.4.0
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.1.4
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Translate where-like filter syntax into Arel-based ActiveRecord scope.
82
+ test_files: []