tire 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ module Tire
4
4
  class Query
5
5
  def initialize(&block)
6
6
  @value = {}
7
- self.instance_eval(&block) if block_given?
7
+ block.arity < 1 ? self.instance_eval(&block) : block.call(self) if block_given?
8
8
  end
9
9
 
10
10
  def term(field, value)
@@ -27,7 +27,11 @@ module Tire
27
27
  def boolean(options={}, &block)
28
28
  # TODO: Try to get rid of the `boolean` method
29
29
  raise ArgumentError, "Please pass a block to boolean query" unless block_given?
30
- @value = BooleanQuery.new(options, &block).to_hash
30
+
31
+ @boolean ||= BooleanQuery.new(options)
32
+ block.arity < 1 ? @boolean.instance_eval(&block) : block.call(@boolean) if block_given?
33
+ @value[:bool] = @boolean.to_hash
34
+ @value
31
35
  end
32
36
 
33
37
  def all
@@ -66,7 +70,7 @@ module Tire
66
70
  def initialize(options={}, &block)
67
71
  @options = options
68
72
  @value = {}
69
- self.instance_eval(&block)
73
+ block.arity < 1 ? self.instance_eval(&block) : block.call(self) if block_given?
70
74
  end
71
75
 
72
76
  def must(&block)
@@ -85,7 +89,7 @@ module Tire
85
89
  end
86
90
 
87
91
  def to_hash
88
- { :bool => @value.update(@options) }
92
+ @value.update(@options)
89
93
  end
90
94
  end
91
95
 
data/lib/tire/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tire
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
@@ -113,6 +113,19 @@ module Tire::Search
113
113
  assert_equal( { :query_string => {:query => 'fuu'} }, query[:bool][:must_not].first )
114
114
  end
115
115
 
116
+ should "allow passing variables from outer scope" do
117
+ q1 = 'foo'
118
+ q2 = 'bar'
119
+ query = Query.new.boolean do |boolean|
120
+ boolean.must { |query| query.string q1 }
121
+ boolean.must { |query| query.string q2 }
122
+ end
123
+
124
+ assert_equal( 2, query[:bool][:must].size, query[:bool][:must].inspect )
125
+ assert_equal( { :query_string => {:query => 'foo'} }, query[:bool][:must].first )
126
+ assert_equal( { :query_string => {:query => 'bar'} }, query[:bool][:must].last )
127
+ end
128
+
116
129
  end
117
130
 
118
131
  end
@@ -253,6 +253,27 @@ module Tire
253
253
  assert_equal( { 'terms' => { 'tags' => ['baz'] } }, query['must'].last)
254
254
  end
255
255
 
256
+ should "allow passing variables from outer scope" do
257
+ q1 = 'foo'
258
+ q2 = 'bar'
259
+
260
+ assert_nothing_raised do
261
+ @search = Search::Search.new('index') do
262
+ query do
263
+ boolean do
264
+ must { string q1 }
265
+ must { string q2 }
266
+ end
267
+ end
268
+ end
269
+ end
270
+
271
+ hash = MultiJson.decode(@search.to_json)
272
+ query = hash['query']['bool']
273
+
274
+ assert_equal 2, query['must'].size
275
+ end
276
+
256
277
  end
257
278
 
258
279
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tire
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 6
10
- version: 0.1.6
9
+ - 7
10
+ version: 0.1.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Karel Minarik
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-06 00:00:00 +02:00
18
+ date: 2011-06-07 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency