elasticsearch_distance_unit_validator 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: a0e2418367852d6d8a6e446ce84d4c7603ff4089
4
+ data.tar.gz: f16293ed9a7372e96cb558552b4b2e38c8496561
5
+ SHA512:
6
+ metadata.gz: ea1fbac8d2ef0d0b1f2aced2ee9d56498a629a19a241ec857ddbb38f1c07f3e81520f8705ba083207dcc5614d9e16bc67497ae5e5bcb9335a951011d1448ba7c
7
+ data.tar.gz: ecc7a3676bb1b25cebbbf95022de968946d4f10052b9b6c2cb75db3017c3e19d5f827ee31a9dc72f3f7198c5ec51833eb92e079ed5da7b7f98549ba25ae4b1a7
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "codeclimate-test-reporter", group: :test, require: nil
4
+
5
+ # Specify your gem's dependencies in elasticsearch_distance_units_validator.gemspec
6
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Sean Devine
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ # ElasticsearchDistanceUnitValidator
2
+
3
+ [Elasticsearch](http://www.elasticsearch.org) uses [distance units](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/common-options.html#distance-units) for features such as its [geo distance filter](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-filter.html).
4
+
5
+ Examples of Elasticsearch distance units include `10ft` and `44miles`.
6
+
7
+ Use this validator to validate that a string conforms to the distance unit format.
8
+
9
+ ```ruby
10
+ validates :distance, elasticsearch_distance_unit: true
11
+ ```
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'elasticsearch_distance_unit_validator'
19
+ ```
20
+
21
+ ## Quality
22
+ [ ![Codeship Status for barelyknown/elasticsearch_distance_unit_validator](https://codeship.com/projects/9bddf450-8efa-0132-e9e0-5ef1dd5f157a/status?branch=master)](https://codeship.com/projects/61122)
23
+ [![Code Climate](https://codeclimate.com/github/barelyknown/elasticsearch_distance_unit_validator/badges/gpa.svg)](https://codeclimate.com/github/barelyknown/elasticsearch_distance_unit_validator)
24
+ [![Test Coverage](https://codeclimate.com/github/barelyknown/elasticsearch_distance_unit_validator/badges/coverage.svg)](https://codeclimate.com/github/barelyknown/elasticsearch_distance_unit_validator)
25
+
26
+ ## Credits
27
+
28
+ Originally created by [@barelyknown](https://twitter.com/barelyknown).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "elasticsearch_distance_unit_validator"
7
+ spec.version = "0.1"
8
+ spec.authors = ["Sean Devine"]
9
+ spec.email = ["barelyknown@icloud.com"]
10
+ spec.summary = %q{Validator for Elasticsearch distance units.}
11
+ spec.description = spec.summary
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "activemodel"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.2"
25
+ end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rspec' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rspec-core', 'rspec')
@@ -0,0 +1,48 @@
1
+ require "active_model"
2
+
3
+ class ElasticsearchDistanceUnitValidator < ActiveModel::EachValidator
4
+ VALID_UNITS = %w(
5
+ mi miles
6
+ yd yards
7
+ ft feet
8
+ in inches
9
+ km kilometers
10
+ m meters
11
+ cm centimeters
12
+ mm millimeters
13
+ NM nmi nauticalmiles
14
+ )
15
+ INVALID_GENERAL_FORMAT_MESSAGE = "did not match the general format"
16
+ INVALID_NUMBER_MESSAGE = "is not greater than zero"
17
+ INVALID_UNIT_MESSAGE = "is not a valid distance unit"
18
+
19
+ def validate_each(record, attribute, value)
20
+ validate_format(record, attribute, value)
21
+ if record.errors[attribute].empty?
22
+ validate_number(record, attribute, value)
23
+ validate_units(record, attribute, value)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def validate_format(record, attribute, value)
30
+ unless value =~ /\A-?\d+[a-zA-Z]+\z/
31
+ record.errors.add(attribute, "#{value} #{INVALID_GENERAL_FORMAT_MESSAGE}")
32
+ end
33
+ end
34
+
35
+ def validate_number(record, attribute, value)
36
+ number = value[/\A[-,\d,.]+/]
37
+ unless number && BigDecimal(number) > 0
38
+ record.errors.add(attribute, "#{number} #{INVALID_NUMBER_MESSAGE}")
39
+ end
40
+ end
41
+
42
+ def validate_units(record, attribute, value)
43
+ units = value[/[a-zA-Z]+\z/]
44
+ unless VALID_UNITS.include?(units)
45
+ record.errors.add(attribute, "#{units} #{INVALID_UNIT_MESSAGE}")
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,78 @@
1
+ RSpec.describe TestModel do
2
+ let :valid_units do
3
+ ElasticsearchDistanceUnitValidator::VALID_UNITS
4
+ end
5
+
6
+ let :invalid_units do
7
+ %w(hands smidges paces)
8
+ end
9
+
10
+ let :valid_numbers do
11
+ (1..10).to_a
12
+ end
13
+
14
+ let :invalid_numbers do
15
+ (-1..0).to_a
16
+ end
17
+
18
+ let :invalid_values do
19
+ [
20
+ valid_numbers[0].to_s + " " + valid_units[0].to_s, # space in the middle
21
+ "a" + valid_numbers[0].to_s + valid_units[0].to_s, # starts with a non digit
22
+ valid_numbers[0].to_s + valid_units[0].to_s + " ", # ends with a space
23
+ valid_numbers[0].to_s + valid_units[0].to_s + 1.to_s, #ends with a number
24
+ ]
25
+ end
26
+
27
+ def validate_error_for(subject, number, unit, *messages)
28
+ subject.distance = number.to_s + unit.to_s
29
+ subject.valid?
30
+ messages.each do |message|
31
+ expect(subject.errors[:distance]).to include Regexp.new(message)
32
+ end
33
+ end
34
+
35
+ context "when the general format is invalid" do
36
+ it "distance is invalid and has the correct error message" do
37
+ invalid_values.each do |value|
38
+ subject.distance = value
39
+ subject.valid?
40
+ expect(subject.errors[:distance]).to include Regexp.new(ElasticsearchDistanceUnitValidator::INVALID_GENERAL_FORMAT_MESSAGE)
41
+ end
42
+ end
43
+ end
44
+
45
+ context "when the number is valid" do
46
+ context "when the units is valid" do
47
+ it "distance is valid" do
48
+ valid_numbers.product(valid_units).each do |number, unit|
49
+ subject.distance = number.to_s + unit.to_s
50
+ expect(subject).to be_valid
51
+ end
52
+ end
53
+ end
54
+ context "when the units are invalid" do
55
+ it "distance is invalid and has the correct error message" do
56
+ valid_numbers.product(invalid_units).each do |number, unit|
57
+ validate_error_for(subject, number, unit, ElasticsearchDistanceUnitValidator::INVALID_UNIT_MESSAGE)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ context "when the number is invalid" do
63
+ context "when the units is valid" do
64
+ it "distance is invalid and has the correct error message" do
65
+ invalid_numbers.product(valid_units).each do |number, unit|
66
+ validate_error_for(subject, number, unit, ElasticsearchDistanceUnitValidator::INVALID_NUMBER_MESSAGE)
67
+ end
68
+ end
69
+ end
70
+ context "when the units is invalid" do
71
+ it "distance is invalid and has the correct error message" do
72
+ invalid_numbers.product(invalid_units).each do |number, unit|
73
+ validate_error_for(subject, number, unit, ElasticsearchDistanceUnitValidator::INVALID_UNIT_MESSAGE, ElasticsearchDistanceUnitValidator::INVALID_NUMBER_MESSAGE)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,32 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
4
+ require "elasticsearch_distance_unit_validator"
5
+ require "support/test_model"
6
+
7
+ RSpec.configure do |config|
8
+ config.expect_with :rspec do |expectations|
9
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
10
+ end
11
+
12
+ config.mock_with :rspec do |mocks|
13
+ mocks.verify_partial_doubles = true
14
+ end
15
+
16
+ config.filter_run :focus
17
+ config.run_all_when_everything_filtered = true
18
+
19
+ config.disable_monkey_patching!
20
+
21
+ config.warnings = true
22
+
23
+ if config.files_to_run.one?
24
+ config.default_formatter = 'doc'
25
+ end
26
+
27
+ config.profile_examples = false
28
+
29
+ config.order = :random
30
+
31
+ Kernel.srand config.seed
32
+ end
@@ -0,0 +1,9 @@
1
+ require "active_model"
2
+
3
+ class TestModel
4
+ include ActiveModel::Validations
5
+
6
+ attr_accessor :distance
7
+
8
+ validates :distance, elasticsearch_distance_unit: true
9
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elasticsearch_distance_unit_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Sean Devine
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
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.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ description: Validator for Elasticsearch distance units.
70
+ email:
71
+ - barelyknown@icloud.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - elasticsearch_distance_unit_validator.gemspec
83
+ - exec/rspec
84
+ - lib/elasticsearch_distance_unit_validator.rb
85
+ - spec/lib/elasticsearch_distance_unit_validator_spec.rb
86
+ - spec/spec_helper.rb
87
+ - spec/support/test_model.rb
88
+ homepage: ''
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.5
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Validator for Elasticsearch distance units.
112
+ test_files:
113
+ - spec/lib/elasticsearch_distance_unit_validator_spec.rb
114
+ - spec/spec_helper.rb
115
+ - spec/support/test_model.rb