searrrch 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 967aaa00861d87150c2ea654666dc1c4d2b86d89
4
+ data.tar.gz: ae2fa7b24de901a973999b68f78998f3ca445887
5
+ SHA512:
6
+ metadata.gz: bc8e8d30719e4a4a951941cb24dbc4c6d2ca4de31babd11d2f00118c2eb97a844db5bbe86c374b9891eb1585b44e2d20e825b5ebc533001d4c19f0403732dba2
7
+ data.tar.gz: 504f1ee4143e98d2d98e67207c5930a8d2ce343bb1a3de463148a482201677b581c8cffae025aa5aad4716feb82fd4361a3821f5c5cf2a3387e2617bb1369f0f
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ searrrch
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in keyword_search.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,88 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ searrrch (0.0.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.3.0)
10
+ coderay (1.1.1)
11
+ diff-lcs (1.2.5)
12
+ ffi (1.9.14)
13
+ formatador (0.2.5)
14
+ guard (2.14.0)
15
+ formatador (>= 0.2.4)
16
+ listen (>= 2.7, < 4.0)
17
+ lumberjack (~> 1.0)
18
+ nenv (~> 0.1)
19
+ notiffany (~> 0.0)
20
+ pry (>= 0.9.12)
21
+ shellany (~> 0.0)
22
+ thor (>= 0.18.1)
23
+ guard-compat (1.2.1)
24
+ guard-rspec (4.7.3)
25
+ guard (~> 2.1)
26
+ guard-compat (~> 1.1)
27
+ rspec (>= 2.99.0, < 4.0)
28
+ listen (3.1.5)
29
+ rb-fsevent (~> 0.9, >= 0.9.4)
30
+ rb-inotify (~> 0.9, >= 0.9.7)
31
+ ruby_dep (~> 1.2)
32
+ lumberjack (1.0.10)
33
+ method_source (0.8.2)
34
+ nenv (0.3.0)
35
+ notiffany (0.1.1)
36
+ nenv (~> 0.1)
37
+ shellany (~> 0.0)
38
+ parser (2.3.1.4)
39
+ ast (~> 2.2)
40
+ powerpack (0.1.1)
41
+ pry (0.10.4)
42
+ coderay (~> 1.1.0)
43
+ method_source (~> 0.8.1)
44
+ slop (~> 3.4)
45
+ rainbow (2.1.0)
46
+ rake (11.3.0)
47
+ rb-fsevent (0.9.8)
48
+ rb-inotify (0.9.7)
49
+ ffi (>= 0.5.0)
50
+ rspec (3.5.0)
51
+ rspec-core (~> 3.5.0)
52
+ rspec-expectations (~> 3.5.0)
53
+ rspec-mocks (~> 3.5.0)
54
+ rspec-core (3.5.4)
55
+ rspec-support (~> 3.5.0)
56
+ rspec-expectations (3.5.0)
57
+ diff-lcs (>= 1.2.0, < 2.0)
58
+ rspec-support (~> 3.5.0)
59
+ rspec-mocks (3.5.0)
60
+ diff-lcs (>= 1.2.0, < 2.0)
61
+ rspec-support (~> 3.5.0)
62
+ rspec-support (3.5.0)
63
+ rubocop (0.44.1)
64
+ parser (>= 2.3.1.1, < 3.0)
65
+ powerpack (~> 0.1)
66
+ rainbow (>= 1.99.1, < 3.0)
67
+ ruby-progressbar (~> 1.7)
68
+ unicode-display_width (~> 1.0, >= 1.0.1)
69
+ ruby-progressbar (1.8.1)
70
+ ruby_dep (1.5.0)
71
+ shellany (0.0.1)
72
+ slop (3.6.0)
73
+ thor (0.19.1)
74
+ unicode-display_width (1.1.1)
75
+
76
+ PLATFORMS
77
+ ruby
78
+
79
+ DEPENDENCIES
80
+ bundler (~> 1.13)
81
+ guard-rspec
82
+ rake
83
+ rspec (~> 3.5)
84
+ rubocop
85
+ searrrch!
86
+
87
+ BUNDLED WITH
88
+ 1.13.6
data/Guardfile ADDED
@@ -0,0 +1,70 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # Note: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ guard :rspec, cmd: "bundle exec rspec", failed_mode: :focus do
28
+ require "guard/rspec/dsl"
29
+ dsl = Guard::RSpec::Dsl.new(self)
30
+
31
+ # Feel free to open issues for suggestions and improvements
32
+
33
+ # RSpec files
34
+ rspec = dsl.rspec
35
+ watch(rspec.spec_helper) { rspec.spec_dir }
36
+ watch(rspec.spec_support) { rspec.spec_dir }
37
+ watch(rspec.spec_files)
38
+
39
+ # Ruby files
40
+ ruby = dsl.ruby
41
+ dsl.watch_spec_files_for(ruby.lib_files)
42
+
43
+ # Rails files
44
+ rails = dsl.rails(view_extensions: %w(erb haml slim))
45
+ dsl.watch_spec_files_for(rails.app_files)
46
+ dsl.watch_spec_files_for(rails.views)
47
+
48
+ watch(rails.controllers) do |m|
49
+ [
50
+ rspec.spec.call("routing/#{m[1]}_routing"),
51
+ rspec.spec.call("controllers/#{m[1]}_controller"),
52
+ rspec.spec.call("acceptance/#{m[1]}")
53
+ ]
54
+ end
55
+
56
+ # Rails config changes
57
+ watch(rails.spec_helper) { rspec.spec_dir }
58
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
59
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
60
+
61
+ # Capybara features specs
62
+ watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
63
+ watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
64
+
65
+ # Turnip features and steps
66
+ watch(%r{^spec/acceptance/(.+)\.feature$})
67
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
68
+ Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
69
+ end
70
+ end
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # searrch
2
+ Allow you to receive a query string using the syntax similar to gmail's filters
3
+ and parse it without going crazy.
4
+
5
+ The following search string is supported:
6
+
7
+ ```
8
+ user_id: 123 user_name: omg user_id: 124 free text here
9
+ ```
10
+
11
+ The user can also put the value between quotes and escape spaces.
12
+
13
+ Usage:
14
+
15
+ ```ruby
16
+
17
+ search = Searrrch.new query
18
+ search.each_value('user_id', :integer) do |val|
19
+ # this block will be called twice with val being set to '123' and '124'
20
+ end
21
+
22
+ # this return all values from user_id in an array
23
+ search.to_array('user_id')
24
+
25
+ # this returns the remaining text
26
+ search.freetext
27
+ ```
28
+
29
+ This is particular useful for appending search criteria to your rails query ;)
30
+
31
+ You can see all the available methods by reading the tiny source code. :D
32
+
33
+ Each method supports a 2nd optional parameter indicating the expected format of the field. Valid options are:
34
+
35
+ * :string (default)
36
+ * :integer
37
+ * rails model (well, anything with `.find` method)
38
+
39
+ ## Installing
40
+
41
+ ```bash
42
+ gem install searrrch
43
+ ```
44
+
45
+ ## Contributing
46
+
47
+ Contributors are welcome! Do the standard pull request stuff.
48
+
49
+ This project is setup to support `rvm`, but any system with bundler should work:
50
+
51
+ ```#bash
52
+ gem install bundler
53
+ bundler install
54
+ rake # this run tests
55
+ ```
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+
5
+ # rspec:
6
+ begin
7
+ require 'rspec/core/rake_task'
8
+
9
+ RSpec::Core::RakeTask.new(:spec)
10
+
11
+ task :default => :spec
12
+ rescue LoadError
13
+ # no rspec available
14
+ end
data/lib/searrrch.rb ADDED
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+ # This defines a really simple API for interfacing with search operations.
3
+ #
4
+ # This is indeed simple... I wanted it to be simple... so I made it simple. So.. SIMPLE!
5
+ class Searrrch
6
+ OPERATOR_EXPRESSION = /(\w+):[\  ]?([\w\p{Han}\p{Katakana}\p{Hiragana}\p{Hangul}ー,]+|(["'])(\\?.)*?\3)/
7
+ # 1 2 3
8
+ # About this regexp:
9
+ # 1. looks for word character (english word basically, plus _ and numbers - might catch others)
10
+ # the : must be right after.. or else fails.... and after : might have one space (en or ja)
11
+ # 2. then look for word characters.. supporting Japanese, Korean, Chinese and latin alphabet plus numbers and such
12
+ # also support ',' for you cool kids that expect something like a "list of ids"
13
+ # 3. and also accept any char if quoted - in which case the same quotation should be quoted as well
14
+
15
+ VERSION = '0.0.2'
16
+
17
+ # iterates over the entire string identifying each of the elements
18
+ # this code only checks for:
19
+ # 1. simple spaces
20
+ # 2. japanese spaces
21
+ # 3. : char
22
+ #
23
+ # All other chars are treated as normal char.
24
+ #
25
+ # Both key and value must have only the other regular chars.
26
+ #
27
+ # Everything after the last option will be considered free text search
28
+ def initialize(query)
29
+ query = query.to_s
30
+ @operators = {}
31
+
32
+ offset = 0
33
+ while m = OPERATOR_EXPRESSION.match(query, offset)
34
+ key = m[1].downcase.to_sym
35
+ value = m[2]
36
+ value = value[1, value.length - 2] if ["'", '"'].include?(value[0])
37
+ offset = m.end(2)
38
+ @operators[key] ||= []
39
+ @operators[key] << value
40
+ end
41
+ @freetext = query[offset, query.length].strip
42
+ end
43
+
44
+ # yield the value of the given operator to each of the contained elements.
45
+ def each_value(key, expects = :string)
46
+ return if @operators[key.to_sym].nil?
47
+ @operators[key.to_sym].each do |value|
48
+ yield(convert(value, expects))
49
+ end
50
+ end
51
+
52
+ # yield the block if there is data in the given block
53
+ def to_array(key, expects = :string)
54
+ res = []
55
+ each_value(key, expects) { |v| res << v }
56
+ res
57
+ end
58
+
59
+ def freetext(expects = :string)
60
+ convert(@freetext, expects)
61
+ end
62
+
63
+ protected
64
+
65
+ def convert(value, expects)
66
+ value = value.gsub(/\\(.)/, '\1')
67
+ case expects
68
+ when :string
69
+ return value
70
+ when :integer
71
+ return value.to_i
72
+ else
73
+ return expects.find(value) if defined? expects.find
74
+ end
75
+ end
76
+ end
data/searrrch.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'searrrch'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "searrrch"
9
+ spec.version = Searrrch::VERSION
10
+ spec.authors = ["Marcelo Coraça de Freitas"]
11
+ spec.email = ["marcelo.freitas@finc.com"]
12
+ spec.summary = %q{Really simple library to parse and handle gmail like parameters - called here 'operators'}
13
+ spec.homepage = %q{https://github.com/marcelocf/searrrch}
14
+ spec.license = "Apache-2.0"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(spec)/})
19
+ spec.rdoc_options = ["--charset=UTF-8"]
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.13"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec", "~> 3.5"
25
+ spec.add_development_dependency "guard-rspec"
26
+ spec.add_development_dependency "rubocop"
27
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+ require 'searrrch'
3
+ # Tests on the freetext feature
4
+ RSpec.describe 'freetext' do
5
+ it 'only free text' do
6
+ query = 'I only contain free text'
7
+ search = Searrrch.new query
8
+ expect(search.freetext).to eq query
9
+ end
10
+
11
+ it 'after one option' do
12
+ freetext = 'this is an example freetext'
13
+ query = "option1: value #{freetext}"
14
+ search = Searrrch.new query
15
+ expect(search.freetext).to eq freetext
16
+ end
17
+
18
+ it 'after one option ending with "' do
19
+ freetext = 'this is an example freetext'
20
+ query = "option1: \"a bigger value\" #{freetext}"
21
+ search = Searrrch.new query
22
+ expect(search.freetext).to eq freetext
23
+ end
24
+
25
+ it 'after one option ending with \'' do
26
+ freetext = 'this is an example freetext'
27
+ query = "option1: \'a bigger value\' #{freetext}"
28
+ search = Searrrch.new query
29
+ expect(search.freetext).to eq freetext
30
+ end
31
+
32
+ it 'with lots of ending and beginning spaces' do
33
+ freetext = 'this is an example freetext'
34
+ query = "option1: \'a bigger value\' #{freetext} "
35
+ search = Searrrch.new query
36
+ expect(search.freetext).to eq freetext
37
+ end
38
+
39
+ it 'containing : but not as an operator' do
40
+ freetext = 'this is an example with : freetext'
41
+ query = "option1: \'a bigger value\' #{freetext}"
42
+ search = Searrrch.new query
43
+ expect(search.freetext).to eq freetext
44
+ end
45
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+ require 'searrrch'
3
+ require 'pp'
4
+ # Tests on the freetext feature
5
+ RSpec.describe 'operators' do
6
+ it 'only one as string' do
7
+ query = 'operator: value'
8
+ search = Searrrch.new query
9
+ search.each_value(:operator) do |value|
10
+ expect(value).to eq 'value'
11
+ end
12
+ end
13
+
14
+ it 'only one as integer' do
15
+ query = 'operator: 123'
16
+ search = Searrrch.new query
17
+ search.each_value(:operator, :integer) do |value|
18
+ expect(value).to eq 123
19
+ end
20
+ end
21
+
22
+ it 'only one as \"rails model\"' do
23
+ module FakeModel
24
+ def self.find(id)
25
+ "it worked for #{id}"
26
+ end
27
+ end
28
+ query = 'operator: 123'
29
+ search = Searrrch.new query
30
+ search.each_value(:operator, FakeModel) do |value|
31
+ expect(value).to eq 'it worked for 123'
32
+ end
33
+ end
34
+
35
+ it 'has one operator with \'' do
36
+ query = 'operator: \'123 is lol\''
37
+ search = Searrrch.new query
38
+ search.each_value(:operator) do |value|
39
+ expect(value).to eq '123 is lol'
40
+ end
41
+ end
42
+
43
+ it 'has one operator with "' do
44
+ query = 'operator: "123 is lol"'
45
+ search = Searrrch.new query
46
+ search.each_value(:operator) do |value|
47
+ expect(value).to eq '123 is lol'
48
+ end
49
+ end
50
+
51
+ it 'has multiple operators with "' do
52
+ query = 'operator: "123 is lol" otheroperator: "12" operator: "other value"'
53
+ expectedvalue = '123 is lol'
54
+ othervalue = 'other value'
55
+ search = Searrrch.new query
56
+ search.each_value(:operator) do |value|
57
+ expect(value).to eq expectedvalue
58
+ expectedvalue = othervalue
59
+ end
60
+ end
61
+
62
+ it 'has multiple operators with \'' do
63
+ query = 'operator: \'123 is lol\' otheroperator: \'12\' operator: \'other value\''
64
+ expectedvalue = '123 is lol'
65
+ othervalue = 'other value'
66
+ search = Searrrch.new query
67
+ search.each_value(:operator) do |value|
68
+ expect(value).to eq expectedvalue
69
+ expectedvalue = othervalue
70
+ end
71
+ end
72
+
73
+ it 'has multiple operators with \' and "' do
74
+ query = 'operator: \'123 is " lol\' otheroperator: \'12\' operator: "other \' value"'
75
+ expectedvalue = '123 is " lol'
76
+ othervalue = 'other \' value'
77
+ search = Searrrch.new query
78
+ search.each_value(:operator) do |value|
79
+ expect(value).to eq expectedvalue
80
+ expectedvalue = othervalue
81
+ end
82
+ end
83
+
84
+ it 'has multiple operators with \' and " return as array' do
85
+ query = 'operator: \'123 is " lol\' otheroperator: \'12\' operator: "other \' value"'
86
+ values = ['123 is " lol', 'other \' value']
87
+ search = Searrrch.new query
88
+ expect(search.to_array(:operator)).to eq values
89
+ end
90
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: searrrch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Marcelo Coraça de Freitas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
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: rubocop
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
+ description:
84
+ email:
85
+ - marcelo.freitas@finc.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".ruby-gemset"
92
+ - ".ruby-version"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - Guardfile
96
+ - LICENSE
97
+ - README.md
98
+ - Rakefile
99
+ - lib/searrrch.rb
100
+ - searrrch.gemspec
101
+ - spec/freetext_spec.rb
102
+ - spec/operators_spec.rb
103
+ homepage: https://github.com/marcelocf/searrrch
104
+ licenses:
105
+ - Apache-2.0
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options:
109
+ - "--charset=UTF-8"
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.5.1
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Really simple library to parse and handle gmail like parameters - called
128
+ here 'operators'
129
+ test_files:
130
+ - spec/freetext_spec.rb
131
+ - spec/operators_spec.rb