boolminot 0.0.1

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: eab2ecb6a19e19003609919b33bc3dfe899dfecf
4
+ data.tar.gz: 01356fb40ceb98133720d7992bf4963c2111af99
5
+ SHA512:
6
+ metadata.gz: c99a90a02d91cc8d4e9730a5549b022c9ed3b7ffef74c70ff7b133b089beceb238a97ec487e1f8d65ab1ef85d3b9e6cec700e9b98d5ab17ac30ddb8818fc65ae
7
+ data.tar.gz: 2b234ef56d661876a68c827f075840760d551d0229bc7d7dea581da735d1cc829c1e3aa7030e51a16b5dd7286a4e96d768f1fbf34a2326c736241edb0dba62f5
@@ -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
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.2.0
7
+ - 2.3.0
8
+ - 2.4.0
9
+ before_install: gem install bundler -v 1.15.1
@@ -0,0 +1,14 @@
1
+ # Boolminot Changelog
2
+
3
+ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning][Semver].
4
+
5
+ ## [Unreleased]
6
+
7
+ * Your contribution here!
8
+
9
+ ## 0.0.1 (2017-07-02)
10
+
11
+ * Initial release
12
+
13
+ [Semver]: http://semver.org
14
+ [Unreleased]: https://github.com/eugeniobruno/boolminot/compare/v0.0.1...HEAD
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at eugeniobruno@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in boolminot.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Eugenio Bruno
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,74 @@
1
+ # Boolminot
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/boolminot.svg)](https://badge.fury.io/rb/boolminot)
4
+ [![Build Status](https://travis-ci.org/eugeniobruno/boolminot.svg?branch=master)](https://travis-ci.org/eugeniobruno/boolminot)
5
+ [![Coverage Status](https://coveralls.io/repos/github/eugeniobruno/boolminot/badge.svg?branch=master)](https://coveralls.io/github/eugeniobruno/boolminot?branch=master)
6
+ [![Code Climate](https://codeclimate.com/github/eugeniobruno/boolminot.svg)](https://codeclimate.com/github/eugeniobruno/boolminot)
7
+ [![Dependency Status](https://gemnasium.com/eugeniobruno/boolminot.svg)](https://gemnasium.com/eugeniobruno/boolminot)
8
+
9
+ Boolminot provides composable models of [Elasticsearch](https://www.elastic.co/products/elasticsearch) clauses, built on top of the [Logica](https://github.com/eugeniobruno/logica) predicate framework.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'boolminot'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install boolminot
26
+
27
+ ## Usage
28
+
29
+ Here is a simple example:
30
+
31
+ ```ruby
32
+ has_any_color = Boolminot.clause_factory.exists(:colors)
33
+ has_color_white = Boolminot.clause_factory.term(:colors, 'white')
34
+ has_color_red = Boolminot.clause_factory.term(:colors, 'red')
35
+ has_color_green = Boolminot.clause_factory.term(:colors, 'green')
36
+ is_machine_washable = Boolminot.clause_factory.term(:machine, true)
37
+
38
+ has_any_color.to_elasticsearch
39
+ { exists: { field: :colors } }
40
+
41
+ has_color_red.to_elasticsearch
42
+ { term: { colors: 'red'} }
43
+
44
+ has_color_white.and(has_color_red).to_elasticsearch
45
+ { bool: { must: [ { term: { colors: 'white' } }, { term: { colors: 'red' } } ] } }
46
+
47
+ has_color_white.or(has_color_red).to_elasticsearch
48
+ { terms: { colors: ['white', 'red'] } }
49
+
50
+ has_color_white.or(is_machine_washable).to_elasticsearch
51
+ { bool: { should: [ { term: { colors: 'white' } }, { term: { machine: true } } ] } }
52
+
53
+ has_color_green.negated.to_elasticsearch
54
+ { bool: { must_not: [ { term: { colors: 'green' } } ] } }
55
+ ```
56
+
57
+ More examples with other clause types can be found in the test files.
58
+
59
+
60
+ ## Development
61
+
62
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
63
+
64
+ To install this gem onto your local machine, run `bundle exec rake install`.
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/eugeniobruno/boolminot. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
69
+
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
74
+
@@ -0,0 +1,17 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/**/test_*.rb']
7
+ end
8
+
9
+ task default: :test
10
+
11
+ desc 'Start a REPL session'
12
+ task :console do
13
+ require 'boolminot'
14
+ require 'pry'
15
+ ARGV.clear
16
+ Pry.start
17
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'boolminot'
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,38 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'boolminot/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'boolminot'
9
+ spec.version = Boolminot::VERSION
10
+ spec.date = '2017-07-02'
11
+ spec.authors = ['Eugenio Bruno']
12
+ spec.email = ['eugeniobruno@gmail.com']
13
+
14
+ spec.summary = 'Boolminot represents Elasticsearch clauses as predicates.'
15
+ spec.homepage = 'https://github.com/eugeniobruno/boolminot'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'bin'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
24
+ spec.require_paths = ['lib']
25
+ spec.extra_rdoc_files = %w[LICENSE.txt README.md CODE_OF_CONDUCT.md CHANGELOG.md]
26
+
27
+ spec.required_ruby_version = '>= 2.0.0'
28
+
29
+ spec.add_runtime_dependency 'logica', '~> 0.0'
30
+
31
+ spec.add_development_dependency 'bundler', '~> 1.15'
32
+ spec.add_development_dependency 'rake', '~> 10.0'
33
+ spec.add_development_dependency 'minitest', '~> 5.0'
34
+ spec.add_development_dependency 'minitest-bender', '~> 0.0'
35
+ spec.add_development_dependency 'simplecov', '~> 0.14'
36
+ spec.add_development_dependency 'coveralls', '~> 0.8'
37
+ spec.add_development_dependency 'pry-byebug', '~> 3.4'
38
+ end
@@ -0,0 +1,28 @@
1
+ require 'boolminot/version'
2
+
3
+ require 'logica'
4
+
5
+ require 'boolminot/helpers/range_bounds_validator'
6
+
7
+ require 'boolminot/clauses/base'
8
+ require 'boolminot/clauses/generic'
9
+ require 'boolminot/clauses/terms'
10
+ require 'boolminot/clauses/range'
11
+ require 'boolminot/clauses/nested'
12
+
13
+ require 'boolminot/clauses/negation'
14
+ require 'boolminot/clauses/match_all'
15
+ require 'boolminot/clauses/match_none'
16
+
17
+ require 'boolminot/clauses/compounds/base'
18
+ require 'boolminot/clauses/compounds/conjunction'
19
+ require 'boolminot/clauses/compounds/disjunction'
20
+ require 'boolminot/clauses/compounds/at_least'
21
+
22
+ require 'boolminot/clause_factory'
23
+
24
+ module Boolminot
25
+ def self.clause_factory
26
+ @clause_factory ||= ClauseFactory.new
27
+ end
28
+ end
@@ -0,0 +1,81 @@
1
+ module Boolminot
2
+ class ClauseFactory < Logica::PredicateFactory
3
+ def generic(type, body, opts = {})
4
+ Clauses::Generic.new(type.to_sym, body, opts)
5
+ end
6
+
7
+ def exists(field, opts = {})
8
+ generic(:exists, { field: field.to_sym }, opts)
9
+ end
10
+
11
+ def missing(field, opts = {})
12
+ exists(field, opts).negated
13
+ end
14
+
15
+ def term(field, value, opts = {})
16
+ Clauses::Terms.new(field, [value], opts)
17
+ end
18
+
19
+ def terms(field, values, opts = {})
20
+ Clauses::Terms.new(field, values, opts)
21
+ end
22
+
23
+ def range(field, bounds, opts = {})
24
+ Clauses::Range.new(field, bounds, opts)
25
+ end
26
+
27
+ def geo_bounding_box(field, bounding_box, opts = {})
28
+ generic(:geo_bounding_box, { field => bounding_box }, opts)
29
+ end
30
+
31
+ def nested(path, clause, opts = {})
32
+ Clauses::Nested.new(path, clause, opts)
33
+ end
34
+
35
+ def script(script, opts = {})
36
+ generic(:script, { script: script }, opts)
37
+ end
38
+
39
+ def query_string(parameters, opts = {})
40
+ generic(:query_string, parameters, opts)
41
+ end
42
+
43
+ def wildcard(field, expression, opts = {})
44
+ generic(:wildcard, { field => expression }, opts)
45
+ end
46
+
47
+ def match_all
48
+ tautology
49
+ end
50
+
51
+ def match_none
52
+ contradiction
53
+ end
54
+
55
+ private
56
+
57
+ def negation_class
58
+ Clauses::Negation
59
+ end
60
+
61
+ def conjunction_class
62
+ Clauses::Compounds::Conjunction
63
+ end
64
+
65
+ def disjunction_class
66
+ Clauses::Compounds::Disjunction
67
+ end
68
+
69
+ def at_least_class
70
+ Clauses::Compounds::AtLeast
71
+ end
72
+
73
+ def tautology_class
74
+ Clauses::MatchAll
75
+ end
76
+
77
+ def contradiction_class
78
+ Clauses::MatchNone
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,65 @@
1
+ module Boolminot
2
+ module Clauses
3
+ class Base < Logica::Predicates::Base
4
+ module ClassMethods
5
+ def predicate_factory
6
+ Boolminot.clause_factory
7
+ end
8
+ end
9
+
10
+ module InstanceMethods
11
+ def satisfied_by?(document)
12
+ raise NoMethodError, 'Sorry; Boolminot clauses do not act as percolators'
13
+ end
14
+
15
+ def to_elasticsearch(options = {})
16
+ default_options = {
17
+ context: :filter
18
+ }
19
+ options = default_options.merge(options)
20
+
21
+ send "to_elasticsearch_in_#{options[:context]}_context"
22
+ end
23
+
24
+ def to_elasticsearch_negated(options = {})
25
+ { bool: { must_not: [to_elasticsearch(options)] } }
26
+ end
27
+
28
+ def to_raw_bool(options = {})
29
+ { bool: { must: [to_elasticsearch(options)] } }
30
+ end
31
+
32
+ protected
33
+
34
+ def terms_with_field?(field)
35
+ false
36
+ end
37
+
38
+ private
39
+
40
+ def type
41
+ OpenHouse.simple_inflector.underscored_demodulized(self.class.name).to_sym
42
+ end
43
+
44
+ def to_elasticsearch_in_query_context
45
+ to_elasticsearch_in_filter_context
46
+ end
47
+
48
+ def to_elasticsearch_in_filter_context
49
+ { type => body.merge(opts) }
50
+ end
51
+
52
+ def opts
53
+ {}
54
+ end
55
+
56
+ def clause_factory
57
+ predicate_factory
58
+ end
59
+ end
60
+
61
+ extend ClassMethods
62
+ include InstanceMethods
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,28 @@
1
+ module Boolminot
2
+ module Clauses
3
+ module Compounds
4
+ class AtLeast < Logica::Predicates::Compounds::AtLeast
5
+ extend Clauses::Base::ClassMethods
6
+ include Clauses::Base::InstanceMethods
7
+
8
+ extend Base::ClassMethods
9
+ include Base::InstanceMethods
10
+
11
+ private
12
+
13
+ def to_elasticsearch_in_query_context
14
+ {
15
+ bool: {
16
+ should: clauses.map { |c| c.to_elasticsearch(context: :query) },
17
+ minimum_should_match: amount
18
+ }
19
+ }
20
+ end
21
+
22
+ def to_elasticsearch_in_filter_context
23
+ to_disjunction.to_elasticsearch(context: :filter)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ module Boolminot
2
+ module Clauses
3
+ module Compounds
4
+ class Base < Clauses::Base
5
+ module ClassMethods
6
+ end
7
+
8
+ module InstanceMethods
9
+ def clauses
10
+ predicates
11
+ end
12
+
13
+ def to_raw_bool(options = {})
14
+ if options.fetch(:added_to, {}).fetch(:should, []).empty?
15
+ to_elasticsearch(options)
16
+ else
17
+ super
18
+ end
19
+ end
20
+ end
21
+
22
+ extend ClassMethods
23
+ include InstanceMethods
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ module Boolminot
2
+ module Clauses
3
+ module Compounds
4
+ class Conjunction < Logica::Predicates::Compounds::Conjunction
5
+ extend Clauses::Base::ClassMethods
6
+ include Clauses::Base::InstanceMethods
7
+
8
+ extend Base::ClassMethods
9
+ include Base::InstanceMethods
10
+
11
+ def to_elasticsearch(options = {})
12
+ bool = clauses.reduce({}) do |sections, clause|
13
+ more_sections = clause.to_raw_bool(options.merge(added_to: sections)).fetch(:bool)
14
+ sections.merge(more_sections) do |_, old_val, new_val|
15
+ old_val + new_val
16
+ end
17
+ end
18
+
19
+ { bool: bool }
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,56 @@
1
+ module Boolminot
2
+ module Clauses
3
+ module Compounds
4
+ class Disjunction < Logica::Predicates::Compounds::Disjunction
5
+ extend Clauses::Base::ClassMethods
6
+ include Clauses::Base::InstanceMethods
7
+
8
+ extend Base::ClassMethods
9
+ include Base::InstanceMethods
10
+
11
+ def to_elasticsearch_negated(options = {})
12
+ { bool: { must_not: clauses.map { |c| c.to_elasticsearch(options) } } }
13
+ end
14
+
15
+ def or_with_terms(terms_clause, options = {})
16
+ default_options = {
17
+ argument_first: true
18
+ }
19
+ options = default_options.merge(options)
20
+
21
+ return super(terms_clause) unless terms_clause.opts.empty?
22
+
23
+ terms_clauses_with_same_field, others = clauses.partition { |q| q.terms_with_field?(terms_clause.field) }
24
+
25
+ return super(terms_clause) if terms_clauses_with_same_field.empty?
26
+
27
+ with_same_field = terms_clauses_with_same_field.first # no disjunction would have two terms with the same field
28
+ same_field = with_same_field.field
29
+ values = with_same_field.values
30
+ more_values = terms_clause.values
31
+
32
+ all_values = options[:argument_first] ? (more_values + values) : (values + more_values)
33
+ with_more_values = clause_factory.terms(same_field, all_values)
34
+
35
+ updated_clauses = options[:argument_first] ? ([with_more_values] + others) : (others + [with_more_values])
36
+ clause_factory.disjunction(updated_clauses)
37
+ end
38
+
39
+ private
40
+
41
+ def to_elasticsearch_in_query_context
42
+ {
43
+ bool: {
44
+ should: clauses.map { |c| c.to_elasticsearch(context: :query) },
45
+ minimum_should_match: 1
46
+ }
47
+ }
48
+ end
49
+
50
+ def to_elasticsearch_in_filter_context
51
+ { bool: { should: clauses.map { |c| c.to_elasticsearch(context: :filter) } } }
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,13 @@
1
+ module Boolminot
2
+ module Clauses
3
+ class Generic < Base
4
+ attr_reader :type, :body, :opts
5
+
6
+ def initialize(type, body, opts = {})
7
+ @type = type.to_sym
8
+ @body = body
9
+ @opts = opts
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ module Boolminot
2
+ module Clauses
3
+ class MatchAll < Logica::Predicates::Tautology
4
+ extend Base::ClassMethods
5
+ include Base::InstanceMethods
6
+
7
+ def initialize(opts = {})
8
+ @opts = opts
9
+ end
10
+
11
+ def body
12
+ opts
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ module Boolminot
2
+ module Clauses
3
+ class MatchNone < Logica::Predicates::Contradiction
4
+ extend Base::ClassMethods
5
+ include Base::InstanceMethods
6
+
7
+ def initialize(opts = {})
8
+ @opts = opts
9
+ end
10
+
11
+ # only for compatibility with Elasticsearch 1.x and 2.x
12
+ # otherwise, defining body as opts would suffice
13
+ def to_elasticsearch(options = {})
14
+ to_raw_bool(options)
15
+ end
16
+
17
+ def to_raw_bool(options = {})
18
+ { bool: { must_not: [negated.to_elasticsearch(options)] } }
19
+ end
20
+ # ---
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ module Boolminot
2
+ module Clauses
3
+ class Negation < Logica::Predicates::Negation
4
+ extend Base::ClassMethods
5
+ include Base::InstanceMethods
6
+
7
+ def to_elasticsearch(options = {})
8
+ clause.to_elasticsearch_negated(options)
9
+ end
10
+
11
+ def to_raw_bool(options = {})
12
+ to_elasticsearch(options)
13
+ end
14
+
15
+ def clause
16
+ predicate
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ module Boolminot
2
+ module Clauses
3
+ class Nested < Base
4
+ attr_reader :path, :clause, :opts
5
+
6
+ def initialize(path, clause, opts)
7
+ @path = path
8
+ @clause = clause
9
+ @opts = opts
10
+ end
11
+
12
+ def to_elasticsearch(options = {})
13
+ default_options = {
14
+ context: :filter
15
+ }
16
+ options = default_options.merge(options)
17
+
18
+ context = options.fetch(:context)
19
+ inner = clause.to_elasticsearch(options)
20
+
21
+ { type => { path: path }.merge(context => inner).merge(opts) }
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,59 @@
1
+ module Boolminot
2
+ module Clauses
3
+ class Range < Base
4
+ attr_reader :field, :bounds, :opts
5
+
6
+ def initialize(field, bounds, opts = {})
7
+ @field = field.to_sym
8
+ @bounds = normalized(bounds)
9
+ @opts = opts
10
+ validate_consistency
11
+ end
12
+
13
+ def lower_bound
14
+ bounds[:gt] || bounds[:gte]
15
+ end
16
+
17
+ def upper_bound
18
+ bounds[:lt] || bounds[:lte]
19
+ end
20
+
21
+ def and(other)
22
+ other.and_with_range(self)
23
+ end
24
+
25
+ def and_with_range(range_clause)
26
+ return super unless opts.empty? && range_clause.opts.empty?
27
+ return super unless field == range_clause.field
28
+
29
+ new_bounds = bounds.merge(range_clause.bounds) { return super }
30
+
31
+ valid_bounds = Helpers::RangeBoundsValidator.new(new_bounds).valid?
32
+ return super unless valid_bounds
33
+
34
+ clause_factory.range(field, new_bounds)
35
+ end
36
+
37
+ private
38
+
39
+ def to_elasticsearch_in_filter_context
40
+ { type => { field => body } }
41
+ end
42
+
43
+ def body
44
+ bounds.merge(opts)
45
+ end
46
+
47
+ def normalized(hash)
48
+ hash.each_with_object(hash.class.new) do |(k, v), h|
49
+ h[k.to_sym] = v
50
+ end
51
+ end
52
+
53
+ def validate_consistency
54
+ valid = Helpers::RangeBoundsValidator.new(bounds).valid?
55
+ raise ArgumentError, "invalid range bounds: #{bounds}" unless valid
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,59 @@
1
+ module Boolminot
2
+ module Clauses
3
+ class Terms < Base
4
+ attr_reader :field, :values, :opts
5
+
6
+ def initialize(field, values, opts = {})
7
+ @field = field.to_sym
8
+ @values = values.uniq
9
+ @opts = opts
10
+ end
11
+
12
+ def or(other)
13
+ other.or_with_terms(self)
14
+ end
15
+
16
+ def or_with_terms(terms_clause)
17
+ return super unless opts.empty? && terms_clause.opts.empty?
18
+ return super unless field == terms_clause.field
19
+
20
+ clause_factory.terms(field, terms_clause.values + values)
21
+ end
22
+
23
+ def or_with_disjunction(disjunction)
24
+ disjunction.or_with_terms(self, argument_first: false)
25
+ end
26
+
27
+ def specialization_of?(other)
28
+ other.generalization_of_terms?(self)
29
+ end
30
+
31
+ def generalization_of_terms?(terms_clause)
32
+ return super unless opts.empty? && terms_clause.opts.empty?
33
+ field == terms_clause.field && terms_clause.values.all? { |v| values.include?(v) }
34
+ end
35
+
36
+ def terms_with_field?(some_field)
37
+ some_field == field
38
+ end
39
+
40
+ private
41
+
42
+ def type
43
+ term? ? :term : super
44
+ end
45
+
46
+ def body
47
+ { field => value_or_values }
48
+ end
49
+
50
+ def value_or_values
51
+ term? ? values.first : values
52
+ end
53
+
54
+ def term?
55
+ values.size == 1
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,33 @@
1
+ module Boolminot
2
+ module Helpers
3
+ class RangeBoundsValidator
4
+ attr_reader :bounds
5
+
6
+ def initialize(bounds)
7
+ @bounds = bounds
8
+ end
9
+
10
+ def valid?
11
+ good_keys = bounds.keys.all? { |key| valid_keys.include?(key) }
12
+ any_bound = !(lower_bounds + upper_bounds).empty?
13
+ well_defined = [lower_bounds, upper_bounds].all? { |bounds| bounds.size <= 1 }
14
+
15
+ good_keys && any_bound && well_defined
16
+ end
17
+
18
+ private
19
+
20
+ def valid_keys
21
+ %i[gt gte lt lte]
22
+ end
23
+
24
+ def lower_bounds
25
+ [bounds[:gt], bounds[:gte]].compact
26
+ end
27
+
28
+ def upper_bounds
29
+ [bounds[:lt], bounds[:lte]].compact
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Boolminot
2
+ VERSION = '0.0.1'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: boolminot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eugenio Bruno
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logica
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.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.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.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
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
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-bender
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.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.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.14'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.14'
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.8'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.8'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.4'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.4'
125
+ description:
126
+ email:
127
+ - eugeniobruno@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files:
131
+ - LICENSE.txt
132
+ - README.md
133
+ - CODE_OF_CONDUCT.md
134
+ - CHANGELOG.md
135
+ files:
136
+ - ".gitignore"
137
+ - ".travis.yml"
138
+ - CHANGELOG.md
139
+ - CODE_OF_CONDUCT.md
140
+ - Gemfile
141
+ - LICENSE.txt
142
+ - README.md
143
+ - Rakefile
144
+ - bin/console
145
+ - bin/setup
146
+ - boolminot.gemspec
147
+ - lib/boolminot.rb
148
+ - lib/boolminot/clause_factory.rb
149
+ - lib/boolminot/clauses/base.rb
150
+ - lib/boolminot/clauses/compounds/at_least.rb
151
+ - lib/boolminot/clauses/compounds/base.rb
152
+ - lib/boolminot/clauses/compounds/conjunction.rb
153
+ - lib/boolminot/clauses/compounds/disjunction.rb
154
+ - lib/boolminot/clauses/generic.rb
155
+ - lib/boolminot/clauses/match_all.rb
156
+ - lib/boolminot/clauses/match_none.rb
157
+ - lib/boolminot/clauses/negation.rb
158
+ - lib/boolminot/clauses/nested.rb
159
+ - lib/boolminot/clauses/range.rb
160
+ - lib/boolminot/clauses/terms.rb
161
+ - lib/boolminot/helpers/range_bounds_validator.rb
162
+ - lib/boolminot/version.rb
163
+ homepage: https://github.com/eugeniobruno/boolminot
164
+ licenses:
165
+ - MIT
166
+ metadata: {}
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: 2.0.0
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubyforge_project:
183
+ rubygems_version: 2.5.1
184
+ signing_key:
185
+ specification_version: 4
186
+ summary: Boolminot represents Elasticsearch clauses as predicates.
187
+ test_files: []