stretchy 0.4.0 → 0.4.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.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.ruby-version +1 -0
  4. data/lib/stretchy/boosts/base.rb +2 -3
  5. data/lib/stretchy/boosts/field_decay_boost.rb +23 -19
  6. data/lib/stretchy/boosts/filter_boost.rb +6 -3
  7. data/lib/stretchy/boosts/random_boost.rb +7 -4
  8. data/lib/stretchy/builders/boost_builder.rb +1 -1
  9. data/lib/stretchy/builders/filter_builder.rb +55 -0
  10. data/lib/stretchy/builders/match_builder.rb +41 -43
  11. data/lib/stretchy/builders/query_builder.rb +37 -0
  12. data/lib/stretchy/builders/shell_builder.rb +53 -0
  13. data/lib/stretchy/builders/where_builder.rb +88 -159
  14. data/lib/stretchy/builders.rb +3 -0
  15. data/lib/stretchy/clauses/base.rb +33 -78
  16. data/lib/stretchy/clauses/boost_clause.rb +10 -10
  17. data/lib/stretchy/clauses/boost_match_clause.rb +3 -3
  18. data/lib/stretchy/clauses/boost_where_clause.rb +5 -5
  19. data/lib/stretchy/clauses/match_clause.rb +5 -24
  20. data/lib/stretchy/clauses/where_clause.rb +23 -42
  21. data/lib/stretchy/errors/validation_error.rb +17 -0
  22. data/lib/stretchy/errors.rb +1 -1
  23. data/lib/stretchy/filters/and_filter.rb +5 -1
  24. data/lib/stretchy/filters/base.rb +2 -2
  25. data/lib/stretchy/filters/bool_filter.rb +10 -4
  26. data/lib/stretchy/filters/exists_filter.rb +5 -1
  27. data/lib/stretchy/filters/geo_filter.rb +13 -7
  28. data/lib/stretchy/filters/not_filter.rb +5 -2
  29. data/lib/stretchy/filters/or_filter.rb +4 -1
  30. data/lib/stretchy/filters/query_filter.rb +5 -1
  31. data/lib/stretchy/filters/range_filter.rb +10 -5
  32. data/lib/stretchy/filters/terms_filter.rb +8 -2
  33. data/lib/stretchy/queries/base.rb +2 -2
  34. data/lib/stretchy/queries/bool_query.rb +12 -0
  35. data/lib/stretchy/queries/filtered_query.rb +8 -3
  36. data/lib/stretchy/queries/function_score_query.rb +25 -32
  37. data/lib/stretchy/queries/match_query.rb +10 -3
  38. data/lib/stretchy/results/base.rb +9 -23
  39. data/lib/stretchy/types/base.rb +2 -2
  40. data/lib/stretchy/types/geo_point.rb +15 -6
  41. data/lib/stretchy/types/range.rb +24 -6
  42. data/lib/stretchy/utils/logger.rb +10 -5
  43. data/lib/stretchy/utils/validation.rb +77 -0
  44. data/lib/stretchy/utils.rb +1 -1
  45. data/lib/stretchy/version.rb +1 -1
  46. data/lib/stretchy.rb +3 -0
  47. data/lib/stretchy_validations.rb +10 -0
  48. data/lib/validation/rule/decay.rb +20 -0
  49. data/lib/validation/rule/distance.rb +20 -0
  50. data/lib/validation/rule/field.rb +22 -0
  51. data/lib/validation/rule/inclusion.rb +33 -0
  52. data/lib/validation/rule/latitude.rb +21 -0
  53. data/lib/validation/rule/longitude.rb +22 -0
  54. data/lib/validation/rule/one_of.rb +22 -0
  55. data/lib/validation/rule/required.rb +26 -0
  56. data/lib/validation/rule/responds_to.rb +23 -0
  57. data/lib/validation/rule/type.rb +41 -0
  58. data/stretchy.gemspec +2 -0
  59. metadata +48 -5
  60. data/lib/stretchy/errors/contract_error.rb +0 -5
  61. data/lib/stretchy/utils/contract.rb +0 -120
@@ -2,12 +2,17 @@ module Stretchy
2
2
  module Types
3
3
  class Range < Base
4
4
 
5
- attr_reader :min, :max, :exclusive_min, :exclusive_max
5
+ attribute :min
6
+ attribute :max
7
+ attribute :exclusive_min
8
+ attribute :exclusive_max
6
9
 
7
- contract min: { type: [Numeric, Date, Time] },
8
- max: { type: [Numeric, Date, Time] },
9
- exclusive_min: { in: [true, false] },
10
- exclusive_max: { in: [true, false] }
10
+ validations do
11
+ rule :min, type: {classes: [Numeric, Date, Time] }
12
+ rule :max, type: {classes: [Numeric, Date, Time] }
13
+ rule :exclusive_min, inclusion: {in: [true, false]}
14
+ rule :exclusive_max, inclusion: {in: [true, false]}
15
+ end
11
16
 
12
17
  def initialize(opts_or_range = {}, options = {})
13
18
 
@@ -17,20 +22,33 @@ module Stretchy
17
22
  @max = opts_or_range.max
18
23
  @exclusive_min = !!(options[:exclusive_min] || options[:exclusive])
19
24
  @exclusive_max = !!(options[:exclusive_max] || options[:exclusive])
25
+ @inverse = !!options[:inverse]
26
+ @should = !!options[:should]
20
27
  when ::Hash
21
28
  opts = options.merge(opts_or_range)
22
29
  @min = opts[:min]
23
30
  @max = opts[:max]
24
31
  @exclusive_min = !!(opts[:exclusive_min] || opts[:exclusive])
25
32
  @exclusive_max = !!(opts[:exclusive_max] || opts[:exclusive])
33
+ @inverse = !!opts[:inverse]
34
+ @should = !!opts[:should]
35
+ when Range
36
+ @min = opts_or_range.min
37
+ @max = opts_or_range.max
38
+ @exclusive_min = opts_or_range.exclusive_min
39
+ @exclusive_max = opts_or_range.exclusive_max
26
40
  else
27
41
  raise Stretchy::Errors::ContractError.new("Ranges must be a range or a hash - found #{options.class.name}")
28
42
  end
29
43
 
30
- require_one min: @min, max: @max
44
+ require_one! :min, :max
31
45
  validate!
32
46
  end
33
47
 
48
+ def empty?
49
+ !(@min || @max)
50
+ end
51
+
34
52
  def to_search
35
53
  json = {}
36
54
  if @exclusive_min && @min
@@ -5,15 +5,19 @@ module Stretchy
5
5
  module Utils
6
6
  class Logger
7
7
 
8
- include Stretchy::Utils::Contract
8
+ include Validation
9
9
 
10
10
  LEVELS = [:silence, :debug, :info, :warn, :error, :fatal]
11
11
 
12
- attr_accessor :base, :level, :color
12
+ attribute :base, Symbol
13
+ attribute :level, Symbol
14
+ attribute :color, String
13
15
 
14
- contract base: { responds_to: LEVELS.last },
15
- level: { in: LEVELS },
16
- color: { in: Colorize::COLORS }
16
+ validations do
17
+ rule :base, responds_to: {method: LEVELS.last}
18
+ rule :level, inclusion: {in: LEVELS}
19
+ rule :color, inclusion: {in: Colorize::COLORS.keys}
20
+ end
17
21
 
18
22
  def self.log(msg_or_obj)
19
23
  self.new.log(msg_or_obj)
@@ -25,6 +29,7 @@ module Stretchy
25
29
  @color = color || :blue
26
30
 
27
31
  @color = @color.to_s if @color.is_a?(Symbol)
32
+
28
33
  validate!
29
34
  end
30
35
 
@@ -0,0 +1,77 @@
1
+ module Stretchy
2
+ module Utils
3
+ module Validation
4
+
5
+ def self.included(base)
6
+ base.send(:include, Virtus.model)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ def initialize(attributes = nil)
11
+ self.class.attribute_set.set(self, attributes) if attributes
12
+ set_default_attributes
13
+ validate!
14
+ after_initialize if respond_to?(:after_initialize)
15
+ end
16
+
17
+ def validator
18
+ @validator ||= self.class.validator.new(self)
19
+ end
20
+
21
+ def validate!
22
+ raise Errors::ValidationError.new(errors) unless valid?
23
+ end
24
+
25
+ def require_one!(*attrs)
26
+ rule = ::Validation::Rule::Required.new
27
+ errors = {}
28
+ unless attrs.any? {|a| rule.valid_value?( send(a) ) }
29
+ raise Errors::ValidationError.new(
30
+ attrs.join(', ') => {
31
+ rule: :require_one_of
32
+ }
33
+ )
34
+ end
35
+ true
36
+ end
37
+
38
+ def require_only_one!(*attrs)
39
+ rule = ::Validation::Rule::Required.new
40
+ errors = {}
41
+ if attrs.select {|a| rule.valid_value?( send(a) ) }.count > 1
42
+ raise Errors::ValidationError.new(
43
+ attrs.join(', ') => {
44
+ rule: :require_only_one
45
+ }
46
+ )
47
+ end
48
+ true
49
+ end
50
+
51
+ def valid?
52
+ validator.valid?
53
+ end
54
+
55
+ def errors
56
+ validator.errors
57
+ end
58
+
59
+ module ClassMethods
60
+
61
+ def validations(&block)
62
+ @validator = Class.new(::Validation::Validator) do
63
+ include ::Validation
64
+
65
+ class_exec(&block)
66
+ end
67
+ end
68
+
69
+ def validator
70
+ @validator
71
+ end
72
+
73
+ end
74
+
75
+ end
76
+ end
77
+ end
@@ -1,4 +1,4 @@
1
- require 'stretchy/utils/contract'
1
+ require 'stretchy/utils/validation'
2
2
  require 'stretchy/utils/colorize'
3
3
  require 'stretchy/utils/logger'
4
4
  require 'stretchy/utils/dot_handler'
@@ -1,3 +1,3 @@
1
1
  module Stretchy
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
data/lib/stretchy.rb CHANGED
@@ -3,7 +3,10 @@ require 'logger'
3
3
  require 'forwardable'
4
4
  require 'excon'
5
5
  require 'elasticsearch'
6
+ require 'virtus'
7
+ require 'validation'
6
8
 
9
+ require 'stretchy_validations'
7
10
  require 'stretchy/utils'
8
11
  require 'stretchy/errors'
9
12
  require 'stretchy/types'
@@ -0,0 +1,10 @@
1
+ require 'validation/rule/not_empty'
2
+ require 'validation/rule/required'
3
+ require 'validation/rule/responds_to'
4
+ require 'validation/rule/decay'
5
+ require 'validation/rule/distance'
6
+ require 'validation/rule/field'
7
+ require 'validation/rule/inclusion'
8
+ require 'validation/rule/latitude'
9
+ require 'validation/rule/longitude'
10
+ require 'validation/rule/type'
@@ -0,0 +1,20 @@
1
+ module Validation
2
+ module Rule
3
+ class Decay
4
+ DECAY_FUNCTIONS = [:gauss, :linear, :exp]
5
+
6
+ def error_key
7
+ :decay_function
8
+ end
9
+
10
+ def valid_value?(value)
11
+ DECAY_FUNCTIONS.any?{|f| f == value || f.to_s == value }
12
+ end
13
+
14
+ def params
15
+ {}
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ module Validation
2
+ module Rule
3
+ class Distance
4
+ DISTANCE_FORMAT = /^(\d+)(km|mi)$/
5
+
6
+ def error_key
7
+ :distance_value
8
+ end
9
+
10
+ def valid_value?(value)
11
+ !!(value.to_s =~ DISTANCE_FORMAT)
12
+ end
13
+
14
+ def params
15
+ {}
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ module Validation
2
+ module Rule
3
+ class Field
4
+
5
+ def error_key
6
+ :field
7
+ end
8
+
9
+ def valid_value?(value)
10
+ valid = true
11
+ valid = false unless value.is_a?(String) || value.is_a?(Symbol) || value.is_a?(Numeric)
12
+ valid = false if value.to_s.empty?
13
+ valid
14
+ end
15
+
16
+ def params
17
+ {}
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,33 @@
1
+ module Validation
2
+ module Rule
3
+ class Inclusion
4
+
5
+ def initialize(params = {})
6
+ @params = params
7
+ end
8
+
9
+ def error_key
10
+ :inclusion
11
+ end
12
+
13
+ def valid_value?(value)
14
+ return true if value.nil? && !params[:required]
15
+ within.any? do |allowed_value|
16
+ if value.respond_to?(:eql?)
17
+ value.eql?(allowed_value)
18
+ else
19
+ value == allowed_value
20
+ end
21
+ end
22
+ end
23
+
24
+ def within
25
+ @params[:in] || @params[:within]
26
+ end
27
+
28
+ def params
29
+ @params
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,21 @@
1
+ module Validation
2
+ module Rule
3
+ class Latitude
4
+
5
+ def error_key
6
+ :latitude
7
+ end
8
+
9
+ def valid_value?(value)
10
+ valid = true
11
+ value = Float(value) rescue nil
12
+ valid = false unless value && value.to_f <= 90 && value.to_f >= -90
13
+ valid
14
+ end
15
+
16
+ def params
17
+ {}
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ module Validation
2
+ module Rule
3
+ class Longitude
4
+
5
+ def error_key
6
+ :longitude
7
+ end
8
+
9
+ def valid_value?(value)
10
+ valid = true
11
+ value = Float(value) rescue nil
12
+ valid = false unless value && value <= 180 && value >= -180
13
+ valid
14
+ end
15
+
16
+ def params
17
+ {}
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ module Validation
2
+ module Rule
3
+ class OneOf
4
+
5
+ def initialize(params)
6
+ @params = params
7
+ end
8
+
9
+ def error_key
10
+ :one_of
11
+ end
12
+
13
+ def valid_value?(value)
14
+
15
+ end
16
+
17
+ def params
18
+ @params
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,26 @@
1
+ module Validation
2
+ module Rule
3
+ class Required
4
+
5
+ def error_key
6
+ :required
7
+ end
8
+
9
+ def valid_value?(value)
10
+ case value
11
+ when nil
12
+ false
13
+ when String, Array, Hash
14
+ !value.empty?
15
+ else
16
+ true
17
+ end
18
+ end
19
+
20
+ def params
21
+ {}
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ module Validation
2
+ module Rule
3
+ class RespondsTo
4
+
5
+ def initialize(params)
6
+ @params = params
7
+ end
8
+
9
+ def error_key
10
+ :responds_to
11
+ end
12
+
13
+ def valid_value?(value)
14
+ value.respond_to?(params[:method])
15
+ end
16
+
17
+ def params
18
+ @params
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,41 @@
1
+ module Validation
2
+ module Rule
3
+ class Type
4
+
5
+ def initialize(params = {})
6
+ @params = params
7
+ end
8
+
9
+ def error_key
10
+ :type_rule
11
+ end
12
+
13
+ def valid_value?(value)
14
+ return true if value.nil? && !params[:required]
15
+
16
+ valid = true
17
+ if params[:array]
18
+ valid = false unless value.all? {|v| validate_type(v) }
19
+ else
20
+ valid = false unless validate_type(value)
21
+ end
22
+ valid
23
+ end
24
+
25
+ def validate_type(value)
26
+ valid = true
27
+ case params[:classes]
28
+ when Array
29
+ valid = false unless params[:classes].any? {|type| value.is_a?(type) }
30
+ else
31
+ valid = false unless value.is_a?(params[:classes])
32
+ end
33
+ valid
34
+ end
35
+
36
+ def params
37
+ @params
38
+ end
39
+ end
40
+ end
41
+ end
data/stretchy.gemspec CHANGED
@@ -21,6 +21,8 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_dependency "elasticsearch", "~> 1.0"
23
23
  spec.add_dependency "excon", "~> 0.45"
24
+ spec.add_dependency "valid"
25
+ spec.add_dependency "virtus"
24
26
 
25
27
  spec.add_development_dependency "bundler", "~> 1.8"
26
28
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stretchy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - agius
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-01 00:00:00.000000000 Z
11
+ date: 2015-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: elasticsearch
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.45'
41
+ - !ruby/object:Gem::Dependency
42
+ name: valid
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: virtus
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: bundler
43
71
  requirement: !ruby/object:Gem::Requirement
@@ -144,6 +172,7 @@ extensions: []
144
172
  extra_rdoc_files: []
145
173
  files:
146
174
  - ".gitignore"
175
+ - ".ruby-version"
147
176
  - ".travis.yml"
148
177
  - ".yardopts"
149
178
  - Gemfile
@@ -159,7 +188,10 @@ files:
159
188
  - lib/stretchy/boosts/random_boost.rb
160
189
  - lib/stretchy/builders.rb
161
190
  - lib/stretchy/builders/boost_builder.rb
191
+ - lib/stretchy/builders/filter_builder.rb
162
192
  - lib/stretchy/builders/match_builder.rb
193
+ - lib/stretchy/builders/query_builder.rb
194
+ - lib/stretchy/builders/shell_builder.rb
163
195
  - lib/stretchy/builders/where_builder.rb
164
196
  - lib/stretchy/clauses.rb
165
197
  - lib/stretchy/clauses/base.rb
@@ -169,7 +201,7 @@ files:
169
201
  - lib/stretchy/clauses/match_clause.rb
170
202
  - lib/stretchy/clauses/where_clause.rb
171
203
  - lib/stretchy/errors.rb
172
- - lib/stretchy/errors/contract_error.rb
204
+ - lib/stretchy/errors/validation_error.rb
173
205
  - lib/stretchy/filters.rb
174
206
  - lib/stretchy/filters/and_filter.rb
175
207
  - lib/stretchy/filters/base.rb
@@ -199,10 +231,21 @@ files:
199
231
  - lib/stretchy/utils/client_actions.rb
200
232
  - lib/stretchy/utils/colorize.rb
201
233
  - lib/stretchy/utils/configuration.rb
202
- - lib/stretchy/utils/contract.rb
203
234
  - lib/stretchy/utils/dot_handler.rb
204
235
  - lib/stretchy/utils/logger.rb
236
+ - lib/stretchy/utils/validation.rb
205
237
  - lib/stretchy/version.rb
238
+ - lib/stretchy_validations.rb
239
+ - lib/validation/rule/decay.rb
240
+ - lib/validation/rule/distance.rb
241
+ - lib/validation/rule/field.rb
242
+ - lib/validation/rule/inclusion.rb
243
+ - lib/validation/rule/latitude.rb
244
+ - lib/validation/rule/longitude.rb
245
+ - lib/validation/rule/one_of.rb
246
+ - lib/validation/rule/required.rb
247
+ - lib/validation/rule/responds_to.rb
248
+ - lib/validation/rule/type.rb
206
249
  - stretchy.gemspec
207
250
  homepage: https://github.com/hired/stretchy
208
251
  licenses:
@@ -224,7 +267,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
224
267
  version: '0'
225
268
  requirements: []
226
269
  rubyforge_project:
227
- rubygems_version: 2.2.2
270
+ rubygems_version: 2.4.5
228
271
  signing_key:
229
272
  specification_version: 4
230
273
  summary: Query builder for Elasticsearch
@@ -1,5 +0,0 @@
1
- module Stretchy
2
- module Errors
3
- class ContractError < StandardError ; end
4
- end
5
- end