dry-elastic_model 0.1.0.pre

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
+ SHA256:
3
+ metadata.gz: bbf4b536a1498cf5adabf60c456a7e09a55e3f82a80277edede9addd8f544fe6
4
+ data.tar.gz: 8359870d7e139074c09e317922cb60ec63712fb69e1efa639c05afde0fa6a2df
5
+ SHA512:
6
+ metadata.gz: f28efa4742d61e263befbbfd0a49e0cc0931c44113c00c26d9f5ab9a82be22018f9f2edc4097593c33d5713a696739d58f7591a59f210ff3bd22218f9b100efd
7
+ data.tar.gz: 74e49fad9e28ed237efd3dd338fb94022b8016cf55f465433b14b31c834985c026238f0838475b9a6f5e282893e735d0c75220dde3d51a7b34b6dc597b431985
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rubocop.yml ADDED
@@ -0,0 +1,63 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.5
5
+
6
+ Metrics/ClassLength:
7
+ Max: 300
8
+
9
+ Metrics/MethodLength:
10
+ Enabled: false
11
+
12
+ Metrics/ParameterLists:
13
+ Enabled: false
14
+
15
+ Metrics/BlockLength:
16
+ Enabled: false
17
+
18
+ Layout/AccessModifierIndentation:
19
+ Enabled: false
20
+
21
+ Style/ClassAndModuleChildren:
22
+ Enabled: false
23
+
24
+ Style/Documentation:
25
+ Enabled: false
26
+
27
+ Layout/DotPosition:
28
+ Enabled: true
29
+ EnforcedStyle: trailing
30
+
31
+ Style/StringLiterals:
32
+ Enabled: true
33
+ EnforcedStyle: double_quotes
34
+
35
+ Naming/VariableNumber:
36
+ Enabled: false
37
+
38
+ Style/FormatString:
39
+ Enabled: false
40
+
41
+ RSpec/MultipleExpectations:
42
+ Enabled: false
43
+
44
+ RSpec/ExampleLength:
45
+ Enabled: false
46
+
47
+ RSpec/FilePath:
48
+ Enabled: false
49
+
50
+ RSpec/LetSetup:
51
+ Enabled: false
52
+
53
+ RSpec/NestedGroups:
54
+ Max: 6
55
+
56
+ Style/PercentLiteralDelimiters:
57
+ PreferredDelimiters:
58
+ default: ()
59
+ '%i': '()'
60
+ '%I': '()'
61
+ '%r': '{}'
62
+ '%w': '()'
63
+ '%W': '()'
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.2
7
+ - 2.3.8
8
+ - 2.4.4
9
+ - 2.5.5
10
+
11
+ before_install: gem install bundler -v 2.0.1
12
+ before_script:
13
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
14
+ - chmod +x ./cc-test-reporter
15
+ - ./cc-test-reporter before-build
16
+ after_script:
17
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
@@ -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 konole@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 dry-elastic_model.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,80 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dry-elastic_model (0.1.0)
5
+ dry-struct (~> 0.6.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ coderay (1.1.2)
11
+ concurrent-ruby (1.1.4)
12
+ diff-lcs (1.3)
13
+ docile (1.3.1)
14
+ dry-configurable (0.8.2)
15
+ concurrent-ruby (~> 1.0)
16
+ dry-core (~> 0.4, >= 0.4.7)
17
+ dry-container (0.7.0)
18
+ concurrent-ruby (~> 1.0)
19
+ dry-configurable (~> 0.1, >= 0.1.3)
20
+ dry-core (0.4.7)
21
+ concurrent-ruby (~> 1.0)
22
+ dry-equalizer (0.2.1)
23
+ dry-inflector (0.1.2)
24
+ dry-logic (0.5.0)
25
+ dry-container (~> 0.2, >= 0.2.6)
26
+ dry-core (~> 0.2)
27
+ dry-equalizer (~> 0.2)
28
+ dry-struct (0.6.0)
29
+ dry-core (~> 0.4, >= 0.4.3)
30
+ dry-equalizer (~> 0.2)
31
+ dry-types (~> 0.13)
32
+ ice_nine (~> 0.11)
33
+ dry-types (0.14.0)
34
+ concurrent-ruby (~> 1.0)
35
+ dry-container (~> 0.3)
36
+ dry-core (~> 0.4, >= 0.4.4)
37
+ dry-equalizer (~> 0.2)
38
+ dry-inflector (~> 0.1, >= 0.1.2)
39
+ dry-logic (~> 0.5, >= 0.5)
40
+ ice_nine (0.11.2)
41
+ json (2.2.0)
42
+ method_source (0.9.2)
43
+ pry (0.12.1)
44
+ coderay (~> 1.1.0)
45
+ method_source (~> 0.9.0)
46
+ rake (10.5.0)
47
+ rspec (3.8.0)
48
+ rspec-core (~> 3.8.0)
49
+ rspec-expectations (~> 3.8.0)
50
+ rspec-mocks (~> 3.8.0)
51
+ rspec-core (3.8.0)
52
+ rspec-support (~> 3.8.0)
53
+ rspec-expectations (3.8.2)
54
+ diff-lcs (>= 1.2.0, < 2.0)
55
+ rspec-support (~> 3.8.0)
56
+ rspec-mocks (3.8.0)
57
+ diff-lcs (>= 1.2.0, < 2.0)
58
+ rspec-support (~> 3.8.0)
59
+ rspec-support (3.8.0)
60
+ simplecov (0.16.1)
61
+ docile (~> 1.1)
62
+ json (>= 1.8, < 3)
63
+ simplecov-html (~> 0.10.0)
64
+ simplecov-html (0.10.2)
65
+ simplecov-lcov (0.7.0)
66
+
67
+ PLATFORMS
68
+ ruby
69
+
70
+ DEPENDENCIES
71
+ bundler (~> 2.0)
72
+ dry-elastic_model!
73
+ pry
74
+ rake (~> 10.0)
75
+ rspec (~> 3.0)
76
+ simplecov
77
+ simplecov-lcov
78
+
79
+ BUNDLED WITH
80
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Konrad Oleksiuk
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,96 @@
1
+ # Dry::ElasticModel [![Build Status](https://travis-ci.org/koleksiuk/dry-elastic_model.svg?branch=master)](https://travis-ci.org/koleksiuk/dry-elastic_model) [![Test Coverage](https://api.codeclimate.com/v1/badges/feba233f76f9fd76a6ad/test_coverage)](https://codeclimate.com/github/koleksiuk/dry-elastic_model/test_coverage) [![Maintainability](https://api.codeclimate.com/v1/badges/feba233f76f9fd76a6ad/maintainability)](https://codeclimate.com/github/koleksiuk/dry-elastic_model/maintainability)
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'dry-elastic_model'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install dry-elastic_model
18
+
19
+ ## Usage
20
+
21
+ This gem was created to provide a thin layer between Elasticsearch models mapping and Ruby objects. Right now, if you want to build your own Elasticsearch wrapper, you're on your own when it comes to creating document mappings and mapping ES documents to Ruby code, which can be painful especially if you want to also validate them before pushing.
22
+
23
+ Responsibilities of this library:
24
+ * models that represent Elasticsearch documents
25
+ * converting and validating Ruby objects to Elasticsearch documents
26
+ * converting & validating Elasticsearch documents to Ruby objects
27
+ * providing mapping out-of-box based on created models
28
+
29
+ Sample model:
30
+
31
+ ```ruby
32
+ class Foo < Dry::ElasticModel::Base
33
+ field :text_field, :text
34
+ field :keyword_field, :keyword, index: false
35
+ field :date_field, :date
36
+ field :long_field, :long
37
+ field :double_field, :double
38
+ field :boolean_field, :boolean
39
+ field :ip_field, :ip
40
+ list :list_text_field, :text
41
+ range :range_long_field, :long
42
+ end
43
+ ```
44
+
45
+ This corresponds to following Elasticsearch mapping (calling `Foo.mapping.to_json`):
46
+
47
+ ```json
48
+ {
49
+ "foo":{
50
+ "properties":{
51
+ "text_field":{
52
+ "type":"text",
53
+ "index":false
54
+ },
55
+ "keyword_field":{
56
+ "type":"keyword"
57
+ },
58
+ "date_field":{
59
+ "type":"date",
60
+ "format":"strict_date_optional_time||epoch_millis"
61
+ },
62
+ "long_field":{
63
+ "type":"long"
64
+ },
65
+ "double_field":{
66
+ "type":"double"
67
+ },
68
+ "boolean_field":{
69
+ "type":"boolean"
70
+ },
71
+ "ip_field":{
72
+ "type":"ip"
73
+ }
74
+ }
75
+ }
76
+ }
77
+ ```
78
+
79
+
80
+ ## Development
81
+
82
+ 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.
83
+
84
+ 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).
85
+
86
+ ## Contributing
87
+
88
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dry-elastic_model. 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.
89
+
90
+ ## License
91
+
92
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
93
+
94
+ ## Code of Conduct
95
+
96
+ Everyone interacting in the Dry::ElasticModel project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/dry-elastic_model/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dry/elastic_model"
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
+
11
+ class Foo < Dry::ElasticModel::Base
12
+ field :text_field, :text, index: false
13
+ field :keyword_field, :keyword
14
+ field :date_field, :date
15
+ field :long_field, :long
16
+ field :double_field, :double
17
+ field :boolean_field, :boolean
18
+ field :ip_field, :ip
19
+ end
20
+
21
+ class Bar < Dry::ElasticModel::Base
22
+ field :field, :object
23
+ end
24
+
25
+ class FooList < Dry::ElasticModel::Base
26
+ list :list_text_field, :text
27
+ end
28
+
29
+ class FooRange < Dry::ElasticModel::Base
30
+ range :range_long_field, :long
31
+ end
32
+
33
+ require "pry"
34
+ Pry.start
data/bin/setup ADDED
@@ -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,44 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "dry/elastic_model/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dry-elastic_model"
8
+ spec.version = Dry::ElasticModel::VERSION
9
+ spec.authors = ["Konrad Oleksiuk"]
10
+ spec.email = ["konole@gmail.com"]
11
+
12
+ spec.summary = %q{Create Elasticsearch models and mappings from code.}
13
+ spec.description = spec.summary
14
+ spec.homepage = "http://github.com/koleksiuk/dry-elastic_model"
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["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "http://github.com/koleksiuk/dry-elastic_model"
22
+ spec.metadata["changelog_uri"] = "http://github.com/koleksiuk/dry-elastic_model"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_dependency "dry-struct", "~> 0.6.0"
38
+ spec.add_development_dependency "bundler", "~> 2.0"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "rspec", "~> 3.0"
41
+ spec.add_development_dependency "pry", "~> 0"
42
+ spec.add_development_dependency "simplecov", "~> 0"
43
+ spec.add_development_dependency "simplecov-lcov", "~> 0"
44
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/elastic_model/version"
4
+ require "dry-struct"
5
+
6
+ module Dry
7
+ module ElasticModel
8
+ class Error < StandardError; end
9
+
10
+ autoload :Attributes, "dry/elastic_model/attributes"
11
+ autoload :Base, "dry/elastic_model/base"
12
+ autoload :Types, "dry/elastic_model/types"
13
+ autoload :Schema, "dry/elastic_model/schema"
14
+
15
+ module TypeOptions
16
+ autoload :Base, "dry/elastic_model/type_options/base"
17
+ autoload :Binary, "dry/elastic_model/type_options/binary"
18
+ autoload :Boolean, "dry/elastic_model/type_options/boolean"
19
+ autoload :Date, "dry/elastic_model/type_options/date"
20
+ autoload :IP, "dry/elastic_model/type_options/ip"
21
+ autoload :Keyword, "dry/elastic_model/type_options/keyword"
22
+ autoload :Numeric, "dry/elastic_model/type_options/numeric"
23
+ autoload :Object, "dry/elastic_model/type_options/object"
24
+ autoload :Range, "dry/elastic_model/type_options/range"
25
+ autoload :ScaledFloat, "dry/elastic_model/type_options/scaled_float"
26
+ autoload :StringType, "dry/elastic_model/type_options/string_type"
27
+ autoload :Text, "dry/elastic_model/type_options/text"
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module ElasticModel
5
+ module Attributes
6
+ def field(name, type, opts = {})
7
+ type_definition = Types::TYPES.fetch(type.to_sym)
8
+
9
+ type_options_klass = type_definition.meta[:type_options]
10
+
11
+ options = if type_options_klass
12
+ type_options_klass.new(opts).to_h
13
+ else
14
+ opts
15
+ end
16
+
17
+ default_opts = type_definition.meta[:opts] || {}
18
+ attribute(name,
19
+ type_definition.meta(opts: default_opts.merge(options.to_h)))
20
+ end
21
+
22
+ def range(name, type, opts = {})
23
+ member = Types::RANGE_TYPES.fetch(type.to_sym)
24
+
25
+ type_definition = Types::Range.call(member)
26
+
27
+ default_opts = type_definition.meta[:opts] || {}
28
+ attribute(name, type_definition.meta(opts: default_opts.merge(opts)))
29
+ end
30
+
31
+ def list(name, type, opts = {})
32
+ member = Types::TYPES.fetch(type.to_sym)
33
+
34
+ type_definition = Types::Array.call(member)
35
+
36
+ default_opts = type_definition.meta[:opts] || {}
37
+ attribute(name, type_definition.meta(opts: default_opts.merge(opts)))
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry-struct"
4
+
5
+ module Dry
6
+ module ElasticModel
7
+ class Base < Dry::Struct
8
+ extend Schema
9
+ extend Attributes
10
+
11
+ def as_json(opts = {})
12
+ if attributes.respond_to? :as_json
13
+ attributes.as_json(opts)
14
+ else
15
+ attributes
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module ElasticModel
5
+ module Schema
6
+ include Types
7
+
8
+ def mapping
9
+ {
10
+ name.downcase.to_sym => {
11
+ properties: schema.inject({}) do |h, (attr, definition)|
12
+ opts = definition.meta.fetch(:opts, {})
13
+ h.merge(
14
+ attr => { type: definition.meta.fetch(:es_name) }.merge(opts)
15
+ )
16
+ end
17
+ }
18
+ }
19
+ end
20
+
21
+ # @param definition [Dry::Types::Definition]
22
+ def parse_definition(definition)
23
+ definition.meta[:es_name]
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module ElasticModel
5
+ module TypeOptions
6
+ class Base < Dry::Struct
7
+ input input.strict
8
+
9
+ def to_h
10
+ attributes
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module ElasticModel
5
+ module TypeOptions
6
+ class Binary < Base
7
+ attribute :doc_values, Types::Bool.meta(omittable: true)
8
+ attribute :store, Types::Bool.meta(omittable: true)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module ElasticModel
5
+ module TypeOptions
6
+ class Boolean < Base
7
+ attribute :boost, Types::Float.meta(omittable: true)
8
+ attribute :doc_values, Types::Bool.meta(omittable: true)
9
+ attribute :index, Types::Bool.meta(omittable: true)
10
+ attribute :null_value, Types::Bool.meta(omittable: true)
11
+ attribute :store, Types::Bool.meta(omittable: true)
12
+ # rubocop:enable Metrics/LineLength
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module ElasticModel
5
+ module TypeOptions
6
+ class Date < Base
7
+ # rubocop:disable Metrics/LineLength
8
+ attribute :boost, Types::Float.meta(omittable: true)
9
+ attribute :doc_values, Types::Bool.meta(omittable: true)
10
+ attribute :format, Types::String.meta(omittable: true).default("strict_date_optional_time||epoch_millis")
11
+ attribute :locale, Types::String.meta(omittable: true)
12
+ attribute :ignore_malformed, Types::Bool.meta(omittable: true)
13
+ attribute :include_in_all, Types::Bool.meta(omittable: true)
14
+ attribute :index, Types::Bool.meta(omittable: true)
15
+ attribute :null_value, Types::Bool.meta(omittable: true)
16
+ attribute :store, Types::Bool.meta(omittable: true)
17
+ # rubocop:enable Metrics/LineLength
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module ElasticModel
5
+ module TypeOptions
6
+ class IP < Base
7
+ attribute :boost, Types::Float.meta(omittable: true)
8
+ attribute :doc_values, Types::Bool.meta(omittable: true)
9
+ attribute :include_in_all, Types::Bool.meta(omittable: true)
10
+ attribute :index, Types::Bool.meta(omittable: true)
11
+ attribute :null_value, Types::Bool.meta(omittable: true)
12
+ attribute :store, Types::Bool.meta(omittable: true)
13
+ # rubocop:enable Metrics/LineLength
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module ElasticModel
5
+ module TypeOptions
6
+ class Keyword < StringType
7
+ attribute :doc_values, Types::Bool.meta(omittable: true)
8
+ attribute :ignore_above, Types::Bool.meta(omittable: true)
9
+ attribute :null_value, Types::Bool.meta(omittable: true)
10
+ attribute :normalizer, Types::String.meta(omittable: true)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module ElasticModel
5
+ module TypeOptions
6
+ class Numeric < Base
7
+ attribute :coerce, Types::Bool.meta(omittable: true)
8
+ attribute :boost, Types::Float.meta(omittable: true)
9
+ attribute :doc_values, Types::Bool.meta(omittable: true)
10
+ attribute :ignore_malformed, Types::Bool.meta(omittable: true)
11
+ attribute :include_in_all, Types::Bool.meta(omittable: true)
12
+ attribute :index, Types::Bool.meta(omittable: true)
13
+ attribute :null_value, Types::Bool.meta(omittable: true)
14
+ attribute :store, Types::Bool.meta(omittable: true)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module ElasticModel
5
+ module TypeOptions
6
+ class Object < Base
7
+ attribute :dynamic, Types::Bool.meta(omittable: true)
8
+ attribute :enabled, Types::Bool.meta(omittable: true)
9
+ attribute :include_in_all, Types::Bool.meta(omittable: true)
10
+ attribute :properties, Types::Hash.meta(omittable: true)
11
+ # rubocop:enable Metrics/LineLength
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module ElasticModel
5
+ module TypeOptions
6
+ class Range < Base
7
+ attribute :coerce, Types::Bool.meta(omittable: true)
8
+ attribute :boost, Types::Float.meta(omittable: true)
9
+ attribute :include_in_all, Types::Bool.meta(omittable: true)
10
+ attribute :index, Types::Bool.meta(omittable: true)
11
+ attribute :store, Types::Bool.meta(omittable: true)
12
+ # rubocop:enable Metrics/LineLength
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module ElasticModel
5
+ module TypeOptions
6
+ class ScaledFloat < Numeric
7
+ input input.strict
8
+
9
+ attribute :scaling_factor, Types::Integer
10
+
11
+ def to_h
12
+ attributes
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module ElasticModel
5
+ module TypeOptions
6
+ class StringType < Base
7
+ # rubocop:disable Metrics/LineLength
8
+ attribute :boost, Types::Float.meta(omittable: true)
9
+ attribute :eager_global_ordinals, Types::Bool.meta(omittable: true)
10
+ attribute :fields, Types::Hash.meta(omittable: true)
11
+ attribute :include_in_all, Types::Bool.meta(omittable: true)
12
+ attribute :index, Types::Bool.meta(omittable: true)
13
+ # TODO: Allow https://www.elastic.co/guide/en/elasticsearch/reference/5.6/index-options.html
14
+ attribute :index_options, Types::Hash.meta(omittable: true)
15
+
16
+ attribute :norms, Types::Bool.meta(omittable: true)
17
+ attribute :store, Types::Bool.meta(omittable: true)
18
+ attribute :similarity, (Types::Value("BM25") | Types::Value("classic") | Types::Value("boolean")).meta(omittable: true)
19
+ # rubocop:enable Metrics/LineLength
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module ElasticModel
5
+ module TypeOptions
6
+ class Text < StringType
7
+ # rubocop:disable Metrics/LineLength
8
+ attribute :analyzer, Types::String.meta(omittable: true)
9
+ attribute :fielddata, Types::Bool.meta(omittable: true)
10
+ attribute :fielddata_frequency_filter, Types::String.meta(omittable: true)
11
+ attribute :position_increment_gap, Types::Integer.meta(omittable: true)
12
+ attribute :search_analyzer, Types::String.meta(omittable: true)
13
+ attribute :search_quote_analyzer, Types::String.meta(omittable: true)
14
+ attribute :term_vector, Types::Bool.meta(omittable: true)
15
+ # rubocop:enable Metrics/LineLength
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry-struct"
4
+ require "resolv"
5
+
6
+ module Dry
7
+ module ElasticModel
8
+ module Types
9
+ include Dry::Types.module
10
+
11
+ # rubocop:disable Metrics/LineLength
12
+ # String datatypes
13
+ Text = Types::Strict::String.meta(es_name: "text",
14
+ type_options: TypeOptions::Text,
15
+ opts: { index: "not_analyzed" })
16
+ Keyword = Types::Strict::Symbol.meta(type_options: TypeOptions::Keyword,
17
+ es_name: "keyword")
18
+
19
+ # Binary datatype
20
+ # TODO: Verify if correct Base64
21
+ Binary = Types::Strict::String.meta(es_name: "binary",
22
+ type_options: TypeOptions::Binary)
23
+
24
+ # Date datatype
25
+ # TODO: Test strings
26
+ Date = (Types::Strict::Date | Types::Strict::Time | Types.Value("now")).
27
+ meta(es_name: "date", type_options: TypeOptions::Date)
28
+
29
+ # Numeric datatypes
30
+ LONG_LIMIT = 2**63
31
+ INTEGER_LIMIT = 2**31
32
+ SHORT_LIMIT = 2**15
33
+ BYTE_LIMIT = 2**8
34
+
35
+ Long = Types::Strict::Integer.
36
+ constrained(gteq: -LONG_LIMIT, lteq: LONG_LIMIT - 1).
37
+ meta(type_options: TypeOptions::Numeric, es_name: "long")
38
+
39
+ Integer = Types::Strict::Integer.
40
+ constrained(gteq: -INTEGER_LIMIT, lteq: INTEGER_LIMIT - 1).
41
+ meta(type_options: TypeOptions::Numeric, es_name: "integer")
42
+
43
+ Short = Types::Strict::Integer.
44
+ constrained(gteq: -SHORT_LIMIT, lteq: SHORT_LIMIT - 1).
45
+ meta(type_options: TypeOptions::Numeric, es_name: "short")
46
+
47
+ Byte = Types::Strict::Integer.
48
+ constrained(gteq: -BYTE_LIMIT, lteq: BYTE_LIMIT - 1).
49
+ meta(type_options: TypeOptions::Numeric, es_name: "byte")
50
+
51
+ Double = (Types::Strict::Integer | Types::Strict::Float).
52
+ meta(es_name: "double")
53
+ Float = (Types::Strict::Integer | Types::Strict::Float).
54
+ meta(type_options: TypeOptions::Numeric, es_name: "float")
55
+
56
+ HalfFloat = (Types::Strict::Integer | Types::Strict::Float).
57
+ meta(type_options: TypeOptions::Numeric, es_name: "half_float")
58
+
59
+ ScaledFloat = (Types::Strict::Integer | Types::Strict::Float).
60
+ meta(type_options: TypeOptions::ScaledFloat,
61
+ es_name: "scaled_float")
62
+
63
+ # Boolean datatype
64
+ Boolean = (Types::Strict::Bool | Types.Value("true") | Types.Value("false")).
65
+ meta(es_name: "boolean", type_options: TypeOptions::Boolean)
66
+
67
+ # IP datatype
68
+ IP = (
69
+ Types::Strict::String.constrained(format: Resolv::IPv4::Regex) |
70
+ Types::Strict::String.constrained(format: Resolv::IPv6::Regex)
71
+ ).meta(es_name: "ip", type_options: TypeOptions::IP)
72
+
73
+ Array = lambda do |type|
74
+ Types::Strict::Array.of(type).meta(es_name: type.meta[:es_name])
75
+ end
76
+
77
+ Range = lambda do |type|
78
+ Types::Strict::Hash.schema(
79
+ gte: type.optional.default(nil),
80
+ gt: type.optional.default(nil),
81
+ lte: type.optional.default(nil),
82
+ lt: type.optional.default(nil)
83
+ ).strict.meta(es_name: "#{type.meta[:es_name]}_range",
84
+ type_options: TypeOptions::Range)
85
+ end
86
+
87
+ ObjectType = Types::Strict::Hash.meta(es_name: "object",
88
+ type_options: TypeOptions::Object)
89
+
90
+ TYPES = {
91
+ text: Text,
92
+ binary: Binary,
93
+ keyword: Keyword,
94
+ date: Date,
95
+ long: Long,
96
+ integer: Integer,
97
+ short: Short,
98
+ byte: Byte,
99
+ double: Double,
100
+ float: Float,
101
+ half_float: HalfFloat,
102
+ scaled_float: ScaledFloat,
103
+ boolean: Boolean,
104
+ ip: IP,
105
+ object: ObjectType
106
+ }.freeze
107
+
108
+ RANGE_KEYS = %i(integer float long double date).freeze
109
+ RANGE_TYPES = TYPES.select { |k, _v| RANGE_KEYS.include?(k) }
110
+ # rubocop:enable Metrics/LineLength
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module ElasticModel
5
+ VERSION = "0.1.0.pre"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry-elastic_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre
5
+ platform: ruby
6
+ authors:
7
+ - Konrad Oleksiuk
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-struct
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.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.6.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: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
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
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov-lcov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Create Elasticsearch models and mappings from code.
112
+ email:
113
+ - konole@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rubocop.yml"
120
+ - ".travis.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - Gemfile.lock
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - dry-elastic_model.gemspec
130
+ - lib/dry/elastic_model.rb
131
+ - lib/dry/elastic_model/attributes.rb
132
+ - lib/dry/elastic_model/base.rb
133
+ - lib/dry/elastic_model/schema.rb
134
+ - lib/dry/elastic_model/type_options/base.rb
135
+ - lib/dry/elastic_model/type_options/binary.rb
136
+ - lib/dry/elastic_model/type_options/boolean.rb
137
+ - lib/dry/elastic_model/type_options/date.rb
138
+ - lib/dry/elastic_model/type_options/ip.rb
139
+ - lib/dry/elastic_model/type_options/keyword.rb
140
+ - lib/dry/elastic_model/type_options/numeric.rb
141
+ - lib/dry/elastic_model/type_options/object.rb
142
+ - lib/dry/elastic_model/type_options/range.rb
143
+ - lib/dry/elastic_model/type_options/scaled_float.rb
144
+ - lib/dry/elastic_model/type_options/string_type.rb
145
+ - lib/dry/elastic_model/type_options/text.rb
146
+ - lib/dry/elastic_model/types.rb
147
+ - lib/dry/elastic_model/version.rb
148
+ homepage: http://github.com/koleksiuk/dry-elastic_model
149
+ licenses:
150
+ - MIT
151
+ metadata:
152
+ homepage_uri: http://github.com/koleksiuk/dry-elastic_model
153
+ source_code_uri: http://github.com/koleksiuk/dry-elastic_model
154
+ changelog_uri: http://github.com/koleksiuk/dry-elastic_model
155
+ post_install_message:
156
+ rdoc_options: []
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ required_rubygems_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">"
167
+ - !ruby/object:Gem::Version
168
+ version: 1.3.1
169
+ requirements: []
170
+ rubyforge_project:
171
+ rubygems_version: 2.7.3
172
+ signing_key:
173
+ specification_version: 4
174
+ summary: Create Elasticsearch models and mappings from code.
175
+ test_files: []