gum 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
+ SHA1:
3
+ metadata.gz: af0f69b2b0a93befee806dd4f19132462587d70c
4
+ data.tar.gz: 2082ef791cbfc27fb14c7e5d3f0499196b7c444c
5
+ SHA512:
6
+ metadata.gz: 5083d26be62d07e05692d58ecd7f7a856e3a26aa095278537714df1668c10d39641e2d63130b8c9ee1b5fa067efa062a5753238323e13a048519394f7c2e62a5
7
+ data.tar.gz: 311fe5200a41c92e50c32420cb102243c85618e0a4a6a82ae9faba979081b859c204f8206b9df22fad883b8b8c5b24ab8253dbedf299298e9c68101d3d013ec8
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gum.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Anton Sozontov
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,41 @@
1
+ # Gum
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/gum`. 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 'gum'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install gum
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]/gum.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -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 'gum'
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
@@ -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,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gum/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'gum'
8
+ spec.version = Gum::VERSION
9
+ spec.authors = ['Anton Sozontov']
10
+ spec.email = ['a.sozontov@gmail.com']
11
+
12
+ spec.summary = 'Search DSL on top of chewy gem'
13
+ spec.description = %q{Gum provides simple DSL for searching in your Elasticsearch}
14
+ spec.homepage = 'https://github.com/qwiqer/gum'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_development_dependency 'bundler', '~> 1.13'
33
+ spec.add_development_dependency 'rake', '~> 10.0'
34
+ spec.add_development_dependency 'rspec', '~> 3.0'
35
+ spec.add_dependency 'activesupport', '>= 3.2'
36
+ spec.add_dependency 'chewy', '~> 0.8.4'
37
+ end
@@ -0,0 +1,12 @@
1
+ require 'gum/version'
2
+
3
+ require 'active_support/core_ext/class/attribute'
4
+ require 'active_support/core_ext/object/blank'
5
+ require 'active_support/core_ext/hash/compact'
6
+
7
+ require 'gum/coerce'
8
+ require 'gum/filters'
9
+ require 'gum/search'
10
+
11
+ module Gum
12
+ end
@@ -0,0 +1,51 @@
1
+ module Gum
2
+ module Coerce
3
+ FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set
4
+
5
+ module_function
6
+
7
+ def range(attr, params)
8
+ key_from = :"#{attr}_from"
9
+ key_to = :"#{attr}_to"
10
+
11
+ return unless params.values_at(key_from, key_to).all?
12
+
13
+ yield(gte: params[key_from], lte: params[key_to])
14
+ end
15
+
16
+ def term(attr, params)
17
+ return if params[attr].blank?
18
+
19
+ yield params[attr]
20
+ end
21
+
22
+ def terms(attr, params)
23
+ return if params[attr].blank?
24
+
25
+ if params[attr].is_a?(Array)
26
+ yield params[attr]
27
+ elsif params[attr].is_a?(String)
28
+ yield params[attr].split(',')
29
+ else
30
+ raise ArgumentError, 'Terms should be either Array or String'
31
+ end
32
+ end
33
+
34
+ def exists(attr, params)
35
+ return if params[attr].eql?('')
36
+
37
+ yield !FALSE_VALUES.include?(params[attr])
38
+ end
39
+
40
+ def split(attr, params, separator: '-', default: nil)
41
+ return if params[attr].blank?
42
+
43
+ value, option = params[attr].split(separator, 2)
44
+ option ||= default.presence
45
+
46
+ return if value.blank? || option.blank?
47
+
48
+ yield value, option
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,91 @@
1
+ module Gum
2
+ module Filters
3
+ module_function
4
+
5
+ def range(attr, flags = {})
6
+ lambda do |params|
7
+ attr_from = params["#{attr}_from"]
8
+ attr_to = params["#{attr}_to"]
9
+ return if attr_from.blank? && attr_to.blank?
10
+ Coerce.range(attr, params) do |value|
11
+ { range: { attr => value.update(flags) } }
12
+ end
13
+ end
14
+ end
15
+
16
+ def term(attr, flags = {})
17
+ lambda do |params|
18
+ Coerce.term(attr, params) do |value|
19
+ if flags[:boost]
20
+ { term: { attr => { value: value, boost: flags[:boost] } } }
21
+ else
22
+ { term: { attr => value }.update(flags) }
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def terms(attr, _flags = {})
29
+ lambda do |params|
30
+ Coerce.terms(attr, params) do |value|
31
+ { terms: { attr => value } }
32
+ end
33
+ end
34
+ end
35
+
36
+ def exists(attr, _flags = {})
37
+ lambda do |params|
38
+ Coerce.exists(attr, params) do |value|
39
+ if value
40
+ { exists: { field: attr } }
41
+ else
42
+ { bool: { must_not: { exists: { field: attr } } } }
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ def prefix(attr, flags = {})
49
+ lambda do |params|
50
+ Coerce.term(attr, params) do |value|
51
+ { prefix: { attr => value }.update(flags) }
52
+ end
53
+ end
54
+ end
55
+
56
+ def wildcard(attr, flags = {})
57
+ lambda do |params|
58
+ Coerce.term(attr, params) do |value|
59
+ { wildcard: { attr => value }.update(flags) }
60
+ end
61
+ end
62
+ end
63
+
64
+ def regexp(attr, flags = {})
65
+ lambda do |params|
66
+ Coerce.term(attr, params) do |value|
67
+ { regexp: flags.update(attr => value) }
68
+ end
69
+ end
70
+ end
71
+
72
+ def fuzzy(attr, separator: '_', **flags)
73
+ lambda do |params|
74
+ Coerce.split(attr, params, separator: separator, default: flags[:fuzziness]) do |value, fuzziness|
75
+ { fuzzy: { attr => { value: value, fuzziness: fuzziness }.update(flags) } }
76
+ end
77
+ end
78
+ end
79
+
80
+ def default_range_result(attr_from, attr_to)
81
+ {
82
+ range: {
83
+ attr => {
84
+ gte: attr_from,
85
+ lte: attr_to
86
+ }
87
+ }
88
+ }
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,93 @@
1
+ module Gum
2
+ class Search
3
+ class_attribute :type
4
+ class_attribute :scope
5
+ class_attribute :fields
6
+ class_attribute :filters
7
+ class_attribute :orders
8
+ class_attribute :query_param
9
+
10
+ self.fields = Set.new
11
+ self.filters = {}
12
+ self.orders = {}
13
+
14
+ # @param [Chewy::Type] type
15
+ def self.use(type)
16
+ self.type = type
17
+ end
18
+
19
+ def self.scope(&block)
20
+ self.scope = block
21
+ end
22
+
23
+ def self.searchable(*attrs)
24
+ self.fields += attrs
25
+ end
26
+
27
+ def self.query(attr)
28
+ self.query_param = attr
29
+ end
30
+
31
+ Filters.singleton_methods.each do |method|
32
+ define_singleton_method method do |*attrs|
33
+ options = attrs.extract_options!
34
+ attrs.each do |attr|
35
+ filters[attr] = Filters.public_send(method, attr, options)
36
+ end
37
+ end
38
+ end
39
+
40
+ def self.order_by(*attrs)
41
+ attrs.each do |attr|
42
+ orders[attr] =
43
+ lambda do |params|
44
+ Coerce.split(attr, params) do |field, direction|
45
+ { field => direction }
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ def initialize(params)
52
+ @params = params
53
+ @sort_params = @params[:sort].split(',') if @params[:sort]
54
+ @q = params[query_param]
55
+ end
56
+
57
+ def compile_filters
58
+ filters.map do |_, proc|
59
+ proc.call(@params)
60
+ end.compact
61
+ end
62
+
63
+ def compile_orders
64
+ if @sort_params
65
+ self.orders = {}
66
+ @sort_params.each do |attr|
67
+ self.class.order_by(attr)
68
+ end
69
+ end
70
+ orders.map do |attr, proc|
71
+ proc.call(attr)
72
+ end.compact
73
+ end
74
+
75
+ def search
76
+ type.query(query).filter(compile_filters).order(compile_orders).load(scope: scope)
77
+ rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e
78
+ @error = e.message.match(/QueryParsingException\[([^;]+)\]/).try(:[], 1)
79
+ type.none
80
+ end
81
+
82
+ def query
83
+ return {} unless @q.present?
84
+ {
85
+ query_string: {
86
+ fields: fields.to_a,
87
+ query: @q,
88
+ default_operator: 'and'
89
+ }
90
+ }
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,3 @@
1
+ module Gum
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gum
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anton Sozontov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-20 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: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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.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.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: chewy
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.8.4
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.8.4
83
+ description: Gum provides simple DSL for searching in your Elasticsearch
84
+ email:
85
+ - a.sozontov@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - gum.gemspec
100
+ - lib/gum.rb
101
+ - lib/gum/coerce.rb
102
+ - lib/gum/filters.rb
103
+ - lib/gum/search.rb
104
+ - lib/gum/version.rb
105
+ homepage: https://github.com/qwiqer/gum
106
+ licenses:
107
+ - MIT
108
+ metadata:
109
+ allowed_push_host: https://rubygems.org
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.5.1
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Search DSL on top of chewy gem
130
+ test_files: []