stretchy 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 124aff086e4d044125b18f641bce3e440a3bec66
4
- data.tar.gz: ee1b4ddda1669c13c7c17a9189fd1cda072f77b6
3
+ metadata.gz: 0a7be2bbcc9690d42a0a9ca01601b4936b52dcf2
4
+ data.tar.gz: a53b3d3201f42f90f807324ec96b6d1049a4037c
5
5
  SHA512:
6
- metadata.gz: 7a4375aca6e56a4755a693b3d346f945fe6d7aebe819064cbbeeb7e80e66588645ab17be24f8ed88b1ca89738d8eb6f9755cf2f79ccd35c9a60c8a862fd160e3
7
- data.tar.gz: dac8575ff96c77ea601051da8fb08489c19c3b17245deed2ff6be48ed58fd2ad9054848025c52a43980a7112ac3277ad604c73ae0f1b1904a6d7eedab42d0715
6
+ metadata.gz: 1171e1dfe7ce1255a911bdd471d6c40aab58b1fa90ba88016d4dff49d0e463effcf9b1194d7e9470d3186e40905ffc7aca941c32a1c45458231aa2376c6993b1
7
+ data.tar.gz: 731cd339625a1a8ffbd398066d751a5424831f5f210eed1dd6ecd152cc27e93424042c8ad7da7d851858899c2b4cb05093382e1a41885dca4e3981949daee182
@@ -4,22 +4,25 @@ module Stretchy
4
4
  module Filters
5
5
  class NotFilter < Base
6
6
 
7
- contract :filter, type: Base
7
+ attr_reader :filters
8
8
 
9
- def initialize(filters)
10
- filters = Array(filters)
9
+ contract :filters, { type: Base, array: true, required: true }
11
10
 
12
- if filters.count == 1
13
- @filter = filters.first
11
+ def initialize(*filters)
12
+ @filters = Array(filters).flatten
13
+ validate!
14
+ end
15
+
16
+ def filter
17
+ if @filters.count > 1
18
+ AndFilter.new(@filters)
14
19
  else
15
- @filter = AndFilter.new(filters)
20
+ @filters.first
16
21
  end
17
-
18
- validate!
19
22
  end
20
23
 
21
24
  def to_search
22
- { not: @filter.to_search }
25
+ { not: filter.to_search }
23
26
  end
24
27
  end
25
28
  end
@@ -2,14 +2,31 @@ module Stretchy
2
2
  module Results
3
3
  class Base
4
4
 
5
- def initialize(request_json, options = {})
6
- @request = request_json
7
- @index = options[:index]
8
- @type = options[:type]
5
+ extend Forwardable
6
+
7
+ attr_reader :clause, :index_name
8
+
9
+ delegate [:type] => :clause
10
+
11
+ def initialize(clause)
12
+ @clause = clause
13
+ @index_name = clause.index_name || Stretchy.index_name
14
+ end
15
+
16
+ def limit
17
+ clause.get_limit
18
+ end
19
+
20
+ def offset
21
+ clause.get_offset
22
+ end
23
+
24
+ def request
25
+ @request ||= {query: clause.to_search}
9
26
  end
10
27
 
11
28
  def response
12
- @response ||= Stretchy.search(type: @type, body: request_json)
29
+ @response ||= Stretchy.search(type: type, body: request, from: offset, size: limit)
13
30
  end
14
31
 
15
32
  def ids
@@ -42,6 +59,4 @@ module Stretchy
42
59
 
43
60
  end
44
61
  end
45
- end
46
-
47
- require 'stretchy/results/null_results'
62
+ end
@@ -6,6 +6,24 @@ module Stretchy
6
6
  # use this class when you don't want to actually run
7
7
  # a search, or catch an exception or something
8
8
 
9
+ def initialize(clause = nil)
10
+ @clause = clause
11
+ @index_name = clause.index_name if clause.is_a?(Stretchy::Clauses::Base)
12
+ @index_name ||= Stretchy.index_name
13
+ end
14
+
15
+ def request
16
+ {}
17
+ end
18
+
19
+ def limit
20
+ 0
21
+ end
22
+
23
+ def offset
24
+ 0
25
+ end
26
+
9
27
  def response
10
28
  @response ||= {
11
29
  'took' => 0,
@@ -21,20 +21,28 @@ module Stretchy
21
21
  Stretchy::Clauses::Base.new(*args, &block)
22
22
  end
23
23
 
24
- def search(type:, body:, fields: nil)
25
- options = { index: index_name, type: type, body: body }
26
- options[:fields] = fields if fields.is_a?(Array)
24
+ def search(options = {})
25
+ params = {}
26
+ params[:index] = options[:index] || index_name
27
+ params[:type] = options[:type]
28
+ params[:fields] = Array(options[:fields]) if options[:fields]
29
+ params[:body] = options[:body]
27
30
 
28
- client.search(options)
31
+ client.search(params)
29
32
  end
30
33
 
31
- def index(type:, body:, id: nil)
32
- id ||= body['id'] || body['_id'] || body[:id] || body[:_id]
33
- client.index(index: index_name, type: type, id: id, body: body)
34
+ def index(options = {})
35
+ index = options[:index] || index_name
36
+ type = options[:type]
37
+ body = options[:body]
38
+ id = options[:id] || options['id'] || body['id'] || body['_id'] || body[:id] || body[:_id]
39
+ client.index(index: index, type: type, id: id, body: body)
34
40
  end
35
41
 
36
- def bulk(type:, documents:)
37
- requests = documents.flat_map do |document|
42
+ def bulk(options = {})
43
+ type = options[:type]
44
+ documents = options[:documents]
45
+ requests = documents.flat_map do |document|
38
46
  id = document['id'] || document['_id'] || document[:id] || document[:_id]
39
47
  [
40
48
  { index: { '_index' => index_name, '_type' => type, '_id' => id } },
@@ -17,6 +17,7 @@ module Stretchy
17
17
 
18
18
  if options[:array]
19
19
  self.class.assert_type(name, value, type: Array)
20
+ self.class.assert_required(name, value, {}) if options[:required]
20
21
  end
21
22
 
22
23
  ASSERTIONS.each do |assertion|
@@ -60,7 +61,7 @@ module Stretchy
60
61
  end
61
62
 
62
63
  def assert_required(name, value, options)
63
- msg = "Expected to have param #{name}, but got nil"
64
+ msg = "Expected to have param #{name}, but got an #{value.inspect}"
64
65
  fail_assertion(msg) if is_empty?(value)
65
66
  end
66
67
 
@@ -1,3 +1,3 @@
1
1
  module Stretchy
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
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.3.0
4
+ version: 0.3.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-05-07 00:00:00.000000000 Z
11
+ date: 2015-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: elasticsearch