mongoid-giza 0.4.0 → 0.5.0

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: 3e001380b635b1d6791949047b359df006939e31
4
- data.tar.gz: da994906999be72baff0a36730ef846bbfe3bcfb
3
+ metadata.gz: 190b245876f472b085d4f2a25712e82e48e4a23e
4
+ data.tar.gz: 868320c19bf5078dc42faf5a961e03bcd11030b4
5
5
  SHA512:
6
- metadata.gz: e7d42d8a4c29c6d664e5d450b2322ee3c21de92e01602f2924c9a6a94db2a433092d4224664888d7a4785a732403d636ec4abc37bff04801fa7e45e2dfcb2de2
7
- data.tar.gz: c6fc8765245d9261839cdbe189fea7efff0ed22b841e0a6eac357322d51196279074cc4b926580dd7190d03d83c69a5ea288bebd86ded7add89902fcab2f1013
6
+ metadata.gz: bc2e8850ef004ba24b8d0d5bd9ff3368b4b7ca2cf94c633cbbb47d3e36aa70b0150c1324c9538377b5e65477398ebc2726fd055c176fecdebef493fd2535a26a
7
+ data.tar.gz: fd62cd8173bcb5ca7323af64175fb2925bc11421372337916adbb131fc109578748337f3871fc0ff93e520034d868f6c004b8b8ca3291cedeae6518817772307
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.0
4
+
5
+ * Clear the generated indexes configuration when regenerating dynamic indexes
6
+ * Removed `Mongoid::Giza::clear_generated_sphinx_indexes_configuration` method
7
+ * Added `Mongoid::Giza::remove_generated_sphinx_indexes` method to remove selected generated indexes
8
+ * Changed the indexes names parameter of `Mongoid::Giza::Search#initialize` from splat to array
9
+ * Renamed `Mongoid::Giza#generate_dynamic_sphinx_indexes` to `Mongoid::Giza#generate_sphinx_indexes`
10
+ * Renamed `Mongoid::Giza::regenerate_dynamic_sphinx_indexes` to `Mongoid::Giza::regenerate_sphinx_indexes`
11
+ * Renamed `Mongoid::Giza::Index#generate_xmlpipe2` to `Mongoid::Giza::Index#xmlpipe2`
12
+
3
13
  ## 0.4.0
4
14
 
5
15
  * Do not render the configuration file on `Mongoid::Giza::Indexer#index!`
data/README.md CHANGED
@@ -46,7 +46,7 @@ development:
46
46
  index:
47
47
  path: "/tmp/sphinx"
48
48
  source:
49
- xmlpipe_command: "rails r '<%= index.klass %>.sphinx_indexes[:<%= index.name %>].generate_xmlpipe2(STDOUT)'"
49
+ xmlpipe_command: "rails r '<%= index.klass %>.sphinx_indexes[:<%= index.name %>].xmlpipe2(STDOUT)'"
50
50
  ```
51
51
 
52
52
  ### Setting up indexes on models
@@ -138,6 +138,18 @@ class Person
138
138
  end
139
139
  ```
140
140
 
141
+ ##### Manipulating Dynamic Indexes
142
+
143
+ If your objects changed and this changes must be reflected on your dynamic indexes, call `Mongoid::Giza::regenerate_sphinx_indexes` to clear the existing ones and regenerate all again.
144
+
145
+ If you need finer control, you may use `Mongoid::Giza#generate_sphinx_indexes` to generate all dynamic indexes for the object (which will replace any existing one with the same name), and `Mongoid::Giza::remove_generated_sphinx_indexes` with the name of the generated indexes which you want to remove.
146
+
147
+ ##### Difference Between Dynamic Index and Generated Index
148
+
149
+ A dynamic index is just the skeleton that creates indexes from every object of the class.
150
+
151
+ A generated index is the actual index that was dynamically created based on the object which the dynamic index used for evaluation.
152
+
141
153
  ### Indexing
142
154
 
143
155
  There are 3 ways to populate the Sphinx index: use the model class' `sphinx_indexer!` method, `Mongoid::Giza::Indexer.instance.index!` or `Mongoid::Giza::Indexer.instance.full_index!`
data/lib/mongoid/giza.rb CHANGED
@@ -60,7 +60,7 @@ module Mongoid
60
60
  end
61
61
 
62
62
  # Generates all the dynamic indexes defined on the class for the object
63
- def generate_dynamic_sphinx_indexes
63
+ def generate_sphinx_indexes
64
64
  self.class.dynamic_sphinx_indexes.each do |dynamic_index|
65
65
  index = dynamic_index.generate_index(self)
66
66
  self.class.generated_sphinx_indexes.merge!({index.name => index})
@@ -128,21 +128,25 @@ module Mongoid
128
128
  # @return [Array] an Array with Riddle result hashes containing an additional key with the name of the class.
129
129
  # The value of this aditional key is a Mongoid::Criteria that return the actual objects of the match
130
130
  def search(&block)
131
- search = Mongoid::Giza::Search.new(giza_configuration.searchd.address, giza_configuration.searchd.port, *sphinx_indexes_names)
131
+ search = Mongoid::Giza::Search.new(giza_configuration.searchd.address, giza_configuration.searchd.port, sphinx_indexes_names)
132
132
  Docile.dsl_eval(search, &block)
133
133
  results = search.run
134
134
  results.each { |result| result[name.to_sym] = self.in(giza_id: result[:matches].map { |match| match[:doc] }) }
135
135
  end
136
136
 
137
137
  # Regenerates all dynamic indexes of the class
138
- def regenerate_dynamic_sphinx_indexes
138
+ def regenerate_sphinx_indexes
139
+ giza_configuration.remove_generated_indexes(generated_sphinx_indexes.keys)
139
140
  generated_sphinx_indexes.clear
140
141
  dynamic_sphinx_indexes.each { |dynamic_index| process_dynamic_sphinx_index(dynamic_index) }
141
142
  end
142
143
 
143
- # Removes all generated indexes of the class from the configuration
144
- def clear_generated_sphinx_indexes_configuration
145
- giza_configuration.remove_generated_indexes(generated_sphinx_indexes.keys)
144
+ # Removes the generated indexes.
145
+ #
146
+ # @param names [Array] a list of generated index names that should be removed
147
+ def remove_generated_sphinx_indexes(*names)
148
+ names.each { |name| generated_sphinx_indexes.delete(name) }
149
+ giza_configuration.remove_generated_indexes(names)
146
150
  end
147
151
 
148
152
  # Execute the indexing routines of the indexes defined on the class.
@@ -119,10 +119,10 @@ module Mongoid
119
119
 
120
120
  # Removes Riddle::Index's specifieds as params
121
121
  #
122
- # @param indexes [Array<Symbol>] names of indexes that should be removed
123
- def remove_generated_indexes(indexes)
124
- indexes.each do |index|
125
- indices.delete(@generated_indexes.delete(index))
122
+ # @param names [Array<Symbol>] names of generated indexes that should be removed
123
+ def remove_generated_indexes(names)
124
+ names.each do |name|
125
+ indices.delete(@generated_indexes.delete(name))
126
126
  end
127
127
  end
128
128
 
@@ -99,7 +99,7 @@ module Mongoid
99
99
  # Generates a XML document according to the XMLPipe2 specification from Sphinx
100
100
  #
101
101
  # @param buffer [#<<] any IO object that supports appending content using <<
102
- def generate_xmlpipe2(buffer)
102
+ def xmlpipe2(buffer)
103
103
  Mongoid::Giza::XMLPipe2.new(self, buffer).generate!
104
104
  end
105
105
  end
@@ -14,19 +14,19 @@ module Mongoid
14
14
  # Index everything, regenerating all dynamic indexes from all classes
15
15
  def full_index!
16
16
  @configuration.clear_generated_indexes
17
- giza_classes.each { |klass| klass.regenerate_dynamic_sphinx_indexes }
17
+ giza_classes.each { |klass| klass.regenerate_sphinx_indexes }
18
18
  @configuration.render
19
19
  index!
20
20
  end
21
21
 
22
22
  # Executes the sphinx indexer
23
23
  #
24
- # @param indexes [Array<Symbol>] name of the indexes that should be indexed.
24
+ # @param names [Array<Symbol>] name of the indexes that should be indexed.
25
25
  # If not provided all indexes from the configuration file are indexed
26
26
  # @param options [Hash] additional options to pass to Riddle::Controller#index
27
27
  # @option options [TrueClass, FalseClass] :verbose shows the indexer output
28
- def index!(*indexes)
29
- @controller.index(*indexes)
28
+ def index!(*names)
29
+ @controller.index(*names)
30
30
  end
31
31
 
32
32
  # @return [Array<Class>] all Mongoid models that include the {Mongoid::Giza} module
@@ -7,7 +7,7 @@ module Mongoid
7
7
 
8
8
  initializer "mongoid-giza.load-configuration" do
9
9
  # Sets the default xmlpipe_command
10
- configuration.source.xmlpipe_command = "rails r '<%= index.klass %>.sphinx_indexes[:<%= index.name %>].generate_xmlpipe2(STDOUT)'"
10
+ configuration.source.xmlpipe_command = "rails r '<%= index.klass %>.sphinx_indexes[:<%= index.name %>].xmlpipe2(STDOUT)'"
11
11
  # Loads the configuration file
12
12
  file = Rails.root.join("config", "giza.yml")
13
13
  configuration.load(file, Rails.env) if file.file?
@@ -10,11 +10,11 @@ module Mongoid
10
10
  #
11
11
  # @param host [String] the host address of sphinxd
12
12
  # @param port [Fixnum] the TCP port of sphinxd
13
- # @param indexes [String] an optional string define the indexes that the search will run on.
14
- # Defaults to "*" which means all indexes
15
- def initialize(host, port, *indexes)
13
+ # @param names [Array] an optional array defining the indexes that the search will run on.
14
+ # Defaults to "[]" which means all indexes
15
+ def initialize(host, port, names = [])
16
16
  @client = Riddle::Client.new(host, port)
17
- @indexes = indexes
17
+ @indexes = names
18
18
  end
19
19
 
20
20
  # Sets the search criteria on full-text fields
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Giza
3
- VERSION = "0.4.0"
3
+ VERSION = "0.5.0"
4
4
  end
5
5
  end
@@ -131,20 +131,20 @@ describe Mongoid::Giza::Index do
131
131
  end
132
132
  end
133
133
 
134
- describe "generate_xmlpipe2" do
134
+ describe "xmlpipe2" do
135
135
  let(:xmlpipe2) { double("XMLPipe2") }
136
136
 
137
137
  let(:buffer) { double("buffer") }
138
138
 
139
139
  it "should create a new XMLPipe2 object" do
140
140
  expect(Mongoid::Giza::XMLPipe2).to receive(:new).with(index, buffer) { xmlpipe2.as_null_object }
141
- index.generate_xmlpipe2(buffer)
141
+ index.xmlpipe2(buffer)
142
142
  end
143
143
 
144
144
  it "should generate the xml" do
145
145
  allow(Mongoid::Giza::XMLPipe2).to receive(:new) { xmlpipe2 }
146
146
  expect(xmlpipe2).to receive(:generate!)
147
- index.generate_xmlpipe2(buffer)
147
+ index.xmlpipe2(buffer)
148
148
  end
149
149
  end
150
150
 
@@ -37,21 +37,21 @@ describe Mongoid::Giza::Indexer do
37
37
  it "should clear the generated indexes from the configuration" do
38
38
  expect(config).to receive(:clear_generated_indexes)
39
39
  allow(@indexer).to receive(:giza_classes) { [klass] }
40
- allow(klass).to receive(:regenerate_dynamic_sphinx_indexes)
40
+ allow(klass).to receive(:regenerate_sphinx_indexes)
41
41
  @indexer.full_index!
42
42
  end
43
43
 
44
44
  it "should regenerate all dynamic indexes of the giza classes" do
45
45
  allow(config).to receive(:clear_generated_indexes)
46
46
  allow(@indexer).to receive(:giza_classes) { [klass] }
47
- expect(klass).to receive(:regenerate_dynamic_sphinx_indexes)
47
+ expect(klass).to receive(:regenerate_sphinx_indexes)
48
48
  @indexer.full_index!
49
49
  end
50
50
 
51
51
  it "should create the sphinx configuration file" do
52
52
  allow(config).to receive(:clear_generated_indexes)
53
53
  allow(@indexer).to receive(:giza_classes) { [klass] }
54
- allow(klass).to receive(:regenerate_dynamic_sphinx_indexes)
54
+ allow(klass).to receive(:regenerate_sphinx_indexes)
55
55
  expect(config).to receive(:render)
56
56
  @indexer.full_index!
57
57
  end
@@ -59,7 +59,7 @@ describe Mongoid::Giza::Indexer do
59
59
  it "should execute the indexer" do
60
60
  allow(config).to receive(:clear_generated_indexes)
61
61
  allow(@indexer).to receive(:giza_classes) { [klass] }
62
- allow(klass).to receive(:regenerate_dynamic_sphinx_indexes)
62
+ allow(klass).to receive(:regenerate_sphinx_indexes)
63
63
  expect(@indexer).to receive(:index!).with(no_args)
64
64
  @indexer.full_index!
65
65
  end
@@ -24,7 +24,7 @@ describe Mongoid::Giza::Search do
24
24
  end
25
25
 
26
26
  it "should accept a index list" do
27
- indexes = Mongoid::Giza::Search.new("localhost", 9132, :index1, :index2).indexes
27
+ indexes = Mongoid::Giza::Search.new("localhost", 9132, [:index1, :index2]).indexes
28
28
  expect(indexes).to eql([:index1, :index2])
29
29
  end
30
30
  end
@@ -28,7 +28,7 @@ describe Mongoid::Giza do
28
28
 
29
29
  let(:search) do
30
30
  search = double("search")
31
- allow(Mongoid::Giza::Search).to receive(:new).with("localhost", 9132) { search }
31
+ allow(Mongoid::Giza::Search).to receive(:new).with("localhost", 9132, []) { search }
32
32
  search
33
33
  end
34
34
 
@@ -145,7 +145,7 @@ describe Mongoid::Giza do
145
145
  end
146
146
 
147
147
  it "should create a search" do
148
- expect(Mongoid::Giza::Search).to receive(:new).with("localhost", 9132, :Person, :Person_2) { double("search").as_null_object }
148
+ expect(Mongoid::Giza::Search).to receive(:new).with("localhost", 9132, [:Person, :Person_2]) { double("search").as_null_object }
149
149
  Person.sphinx_index { }
150
150
  Person.sphinx_index { name :Person_2 }
151
151
  Person.search { }
@@ -261,34 +261,39 @@ describe Mongoid::Giza do
261
261
  end
262
262
  end
263
263
 
264
- describe "regenerate_dynamic_sphinx_indexes" do
264
+ describe "regenerate_sphinx_indexes" do
265
265
  let(:generated) { double("generated") }
266
266
 
267
267
  let(:dynamic) { double("dynamic") }
268
268
 
269
- it "should clear the generated indexes" do
269
+ let(:keys) { double("keys") }
270
+
271
+ before do
270
272
  allow(Person).to receive(:generated_sphinx_indexes) { generated }
271
- expect(generated).to receive(:clear)
272
- Person.regenerate_dynamic_sphinx_indexes
273
+ allow(generated).to receive(:keys) { keys }
274
+ allow(generated).to receive(:clear)
275
+ allow(config).to receive(:remove_generated_indexes)
276
+ end
277
+
278
+ it "should clear the generated indexes configuration" do
279
+ expect(config).to receive(:remove_generated_indexes).with(keys)
280
+ Person.regenerate_sphinx_indexes
281
+ end
282
+
283
+ it "should clear the generated indexes" do
284
+ expect(generated).to receive(:clear).with(no_args)
285
+ Person.regenerate_sphinx_indexes
273
286
  end
274
287
 
275
288
  it "should process all dynamic indexes" do
276
289
  allow(Person).to receive(:dynamic_sphinx_indexes) { dynamic }
277
290
  allow(dynamic).to receive(:each).and_yield(:dynamic_index)
278
291
  expect(Person).to receive(:process_dynamic_sphinx_index).with(:dynamic_index)
279
- Person.regenerate_dynamic_sphinx_indexes
292
+ Person.regenerate_sphinx_indexes
280
293
  end
281
294
  end
282
295
 
283
- describe "clear_generated_sphinx_indexes_configuration" do
284
- it "should remove all generated indexes of this class from the configuration" do
285
- allow(Person).to receive(:generated_sphinx_indexes) { {index1: :index, index2: :index} }
286
- expect(config).to receive(:remove_generated_indexes).with([:index1, :index2])
287
- Person.clear_generated_sphinx_indexes_configuration
288
- end
289
- end
290
-
291
- describe "generate_dynamic_sphinx_indexes" do
296
+ describe "generate_sphinx_indexes" do
292
297
  let(:person) { Person.new }
293
298
 
294
299
  let(:dynamic_index) { double("dynamic index") }
@@ -307,17 +312,31 @@ describe Mongoid::Giza do
307
312
 
308
313
  it "should generate all the dynamic indexes of the class for the object" do
309
314
  expect(dynamic_index).to receive(:generate_index).with(person).twice { index }
310
- person.generate_dynamic_sphinx_indexes
315
+ person.generate_sphinx_indexes
311
316
  end
312
317
 
313
318
  it "should merge the resulting indexes to the class' generated indexes" do
314
319
  expect(Person.generated_sphinx_indexes).to receive(:merge!).with({name: index}).twice
315
- person.generate_dynamic_sphinx_indexes
320
+ person.generate_sphinx_indexes
316
321
  end
317
322
 
318
323
  it "should add the indexes to the configuration" do
319
324
  expect(config).to receive(:add_index).with(index, true).twice
320
- person.generate_dynamic_sphinx_indexes
325
+ person.generate_sphinx_indexes
326
+ end
327
+ end
328
+
329
+ describe "remove_generated_sphinx_indexes" do
330
+ let(:index_name) { double("index name") }
331
+
332
+ it "should remove the indexes from the generated indexes collection" do
333
+ expect(Person.generated_sphinx_indexes).to receive(:delete).with(index_name).twice
334
+ Person.remove_generated_sphinx_indexes(index_name, index_name)
335
+ end
336
+
337
+ it "should remove the indexes from the configuration" do
338
+ expect(config).to receive(:remove_generated_indexes).with([index_name, index_name])
339
+ Person.remove_generated_sphinx_indexes(index_name, index_name)
321
340
  end
322
341
  end
323
342
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-giza
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maurício Batista
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-13 00:00:00.000000000 Z
11
+ date: 2014-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler