mongoid-giza 0.3.0 → 0.4.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: 879640b0caf8a604f0bd1c30a4f2e28fe8701ebd
4
- data.tar.gz: 1574d2f43cc5dd16e9012f6567162c161f59d7af
3
+ metadata.gz: 3e001380b635b1d6791949047b359df006939e31
4
+ data.tar.gz: da994906999be72baff0a36730ef846bbfe3bcfb
5
5
  SHA512:
6
- metadata.gz: fde8f8c1aaee58cd6e3bdce7cd91c68681052d52ae392bc8d31f331b0a591e47febe5743654a077ea8280985ff28b89d46ec9ea9df5426761db0ff1fd72fa34f
7
- data.tar.gz: c949aee6ad8406d3d2b618cda8497769a3345544c7d5d79523c7d652e5777806cb51d8ab8a4ab9e749b8d74d5e57c597c7460a7a39d547f809cf1f51d8b562e8
6
+ metadata.gz: e7d42d8a4c29c6d664e5d450b2322ee3c21de92e01602f2924c9a6a94db2a433092d4224664888d7a4785a732403d636ec4abc37bff04801fa7e45e2dfcb2de2
7
+ data.tar.gz: c6fc8765245d9261839cdbe189fea7efff0ed22b841e0a6eac357322d51196279074cc4b926580dd7190d03d83c69a5ea288bebd86ded7add89902fcab2f1013
data/.travis.yml CHANGED
@@ -3,5 +3,6 @@ rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
5
  - 2.1.0
6
+ - 2.1.1
6
7
  services:
7
8
  - mongodb
data/CHANGELOG.md CHANGED
@@ -1,15 +1,24 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.0
4
+
5
+ * Do not render the configuration file on `Mongoid::Giza::Indexer#index!`
6
+ * Do not force the verbose indexing
7
+ * Added `Mongoid::Giza::giza_configuration` accessor
8
+ * Fix converting the index name to symbol
9
+ * Added `Mongoid::Giza::DynamicIndex#generate_index` method to generate the dynamic index for a single object
10
+ * Added `Mongoid::Giza#generate_dynamic_sphinx_indexes` method to generate all dynamic indexes for the object
11
+
3
12
  ## 0.3.0
4
13
 
5
14
  * Downcase all fields' and attributes' names
6
15
  * Made possible to selective remove generated indexes from the configuration -- `Mongoid::Giza::Configuration#remove_generated_indexes`
7
- * Made possible to remove from the configuratio only the generated indexes of a model -- `Mongoid::Giza::clear_generated_sphinx_indexes_configuration`
16
+ * Made possible to remove from the configuration only the generated indexes of a model -- `Mongoid::Giza::clear_generated_sphinx_indexes_configuration`
8
17
 
9
18
  ## 0.2.0
10
19
 
11
20
  * Use ERB to parse string settings of every section of the configuration
12
- * Renamed `Mongoid::Giza::Indexer::full_index` to `Mongoid::Giza::Indexer::full_index!`
21
+ * Renamed `Mongoid::Giza::Indexer#full_index` to `Mongoid::Giza::Indexer#full_index!`
13
22
  * Always convert `Mongoid::Giza::Index::Field` and `Mongoid::Giza::Index::Attribute` names to symbol
14
23
 
15
24
  ## 0.1.0
data/README.md CHANGED
@@ -144,9 +144,9 @@ There are 3 ways to populate the Sphinx index: use the model class' `sphinx_inde
144
144
 
145
145
  * **sphinx_indexer!:** Will execute the indexer program only on the indexes of the class.
146
146
  Does not regenerate dynamic indexes.
147
- * **index!:** Will execute the indexer program on all indexes.
147
+ * **index!:** Will execute the indexer.
148
148
  Does not regenerate dynamic indexes.
149
- * **full_index!:** Will regenerate dynamic indexes and execute the indexer program on all indexes.
149
+ * **full_index!:** Will regenerate dynamic indexes, render the configuration file and execute the indexer program on all indexes.
150
150
 
151
151
  This gem does not execute none of those automatically to let the you define what is the best reindexing strategy for your software.
152
152
 
data/lib/mongoid/giza.rb CHANGED
@@ -59,8 +59,17 @@ module Mongoid
59
59
  self[:giza_id]
60
60
  end
61
61
 
62
+ # Generates all the dynamic indexes defined on the class for the object
63
+ def generate_dynamic_sphinx_indexes
64
+ self.class.dynamic_sphinx_indexes.each do |dynamic_index|
65
+ index = dynamic_index.generate_index(self)
66
+ self.class.generated_sphinx_indexes.merge!({index.name => index})
67
+ self.class.giza_configuration.add_index(index, true)
68
+ end
69
+ end
70
+
62
71
  module ClassMethods
63
- attr_reader :static_sphinx_indexes, :generated_sphinx_indexes, :dynamic_sphinx_indexes
72
+ attr_reader :giza_configuration, :static_sphinx_indexes, :generated_sphinx_indexes, :dynamic_sphinx_indexes
64
73
 
65
74
  # Class method that defines a index relative to the current class' objects.
66
75
  # If an argument is given in the block then a dynamic index will be created.
@@ -98,7 +107,7 @@ module Mongoid
98
107
  index = Index.new(self, settings)
99
108
  Docile.dsl_eval(index, &block)
100
109
  static_sphinx_indexes[index.name] = index
101
- @giza_configuration.add_index(index)
110
+ giza_configuration.add_index(index)
102
111
  end
103
112
 
104
113
  # Generates the indexes from the dynamic index and
@@ -108,7 +117,7 @@ module Mongoid
108
117
  def process_dynamic_sphinx_index(dynamic_index)
109
118
  generated = dynamic_index.generate!
110
119
  generated_sphinx_indexes.merge!(generated)
111
- generated.each { |name, index| @giza_configuration.add_index(index, true) }
120
+ generated.each { |name, index| giza_configuration.add_index(index, true) }
112
121
  end
113
122
 
114
123
  # Class method that implements a search DSL using a {Mongoid::Giza::Search} object.
@@ -119,7 +128,7 @@ module Mongoid
119
128
  # @return [Array] an Array with Riddle result hashes containing an additional key with the name of the class.
120
129
  # The value of this aditional key is a Mongoid::Criteria that return the actual objects of the match
121
130
  def search(&block)
122
- 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)
123
132
  Docile.dsl_eval(search, &block)
124
133
  results = search.run
125
134
  results.each { |result| result[name.to_sym] = self.in(giza_id: result[:matches].map { |match| match[:doc] }) }
@@ -133,7 +142,7 @@ module Mongoid
133
142
 
134
143
  # Removes all generated indexes of the class from the configuration
135
144
  def clear_generated_sphinx_indexes_configuration
136
- @giza_configuration.remove_generated_indexes(generated_sphinx_indexes.keys)
145
+ giza_configuration.remove_generated_indexes(generated_sphinx_indexes.keys)
137
146
  end
138
147
 
139
148
  # Execute the indexing routines of the indexes defined on the class.
@@ -111,7 +111,7 @@ module Mongoid
111
111
  File.open(@file.output_path, "w") { |file| file.write(super) }
112
112
  end
113
113
 
114
- # Removes all Riddle::Index from the indices Array that where created from a generated {Mongoid::Giza::Index}
114
+ # Removes all Riddle::Index from the indices Array that were created from a generated {Mongoid::Giza::Index}
115
115
  def clear_generated_indexes
116
116
  @generated_indexes.each { |name, index| indices.delete(index) }
117
117
  @generated_indexes = {}
@@ -21,16 +21,29 @@ module Mongoid
21
21
  # The name of the index is unique so in case of a name collision,
22
22
  # the last index to be generated is the one that will persist
23
23
  #
24
- # @return [Hash<Symbol, Mongoid::Giza::Index] an hash with every key being the index name
24
+ # @return [Hash<Symbol, Mongoid::Giza::Index>] an hash with every key being the index name
25
25
  # and the value the index itself
26
26
  def generate!
27
27
  indexes = {}
28
28
  klass.all.each do |object|
29
+ index = generate_index(object)
30
+ indexes[index.name] = index if !index.nil?
31
+ end
32
+ indexes
33
+ end
34
+
35
+ # Generates the index for the object passed as parameter.
36
+ # It is only generated if the object's class is the class or a subclass of the index's class
37
+ #
38
+ # @param object [Mongoid::Document] the object which the index block wil be evaluated for
39
+ #
40
+ # @return [Mongoid::Giza::Index, NilClass] the resulting index from the evaluation
41
+ # or nil if the object's class is not the index's class or a subclass of it
42
+ def generate_index(object)
43
+ if object.is_a?(klass)
29
44
  index = Mongoid::Giza::Index.new(klass, settings)
30
45
  Docile.dsl_eval(index, object, &block)
31
- indexes[index.name] = index
32
46
  end
33
- indexes
34
47
  end
35
48
  end
36
49
  end
@@ -83,7 +83,8 @@ module Mongoid
83
83
  #
84
84
  # @return [Symbol] The name of the index
85
85
  def name(new_name = nil)
86
- @name = new_name || @name.to_sym
86
+ @name = new_name.to_sym if !new_name.nil?
87
+ @name
87
88
  end
88
89
 
89
90
  # Defines the Mongoid::Criteria that will be used to retrive objects when indexing.
@@ -15,13 +15,18 @@ module Mongoid
15
15
  def full_index!
16
16
  @configuration.clear_generated_indexes
17
17
  giza_classes.each { |klass| klass.regenerate_dynamic_sphinx_indexes }
18
+ @configuration.render
18
19
  index!
19
20
  end
20
21
 
21
- # Creates the sphinx configuration file then executes the indexer on it
22
+ # Executes the sphinx indexer
23
+ #
24
+ # @param indexes [Array<Symbol>] name of the indexes that should be indexed.
25
+ # If not provided all indexes from the configuration file are indexed
26
+ # @param options [Hash] additional options to pass to Riddle::Controller#index
27
+ # @option options [TrueClass, FalseClass] :verbose shows the indexer output
22
28
  def index!(*indexes)
23
- @configuration.render
24
- @controller.index(*indexes, verbose: true)
29
+ @controller.index(*indexes)
25
30
  end
26
31
 
27
32
  # @return [Array<Class>] all Mongoid models that include the {Mongoid::Giza} module
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Giza
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
@@ -11,14 +11,22 @@ describe Mongoid::Giza::DynamicIndex do
11
11
  end
12
12
 
13
13
  describe "generate!" do
14
- let(:dynamic_index) { Mongoid::Giza::DynamicIndex.new(Object, {}, Proc.new { |object| name(object[:name]) }) }
14
+ let(:dynamic_index) { Mongoid::Giza::DynamicIndex.new(Object, {}, Proc.new { }) }
15
+
16
+ let(:index) { double("index") }
15
17
 
16
18
  before do
17
19
  klass = double("class")
18
- index = double("index")
19
- allow(klass).to receive(:all) { [{name: "object"}, {name: "other"}, {name: "other"}] }
20
+ allow(index).to receive(:name) { :name }
21
+ allow(klass).to receive(:all) { Array.new(3) }
20
22
  allow(klass).to receive(:name) { :class }
21
23
  allow(dynamic_index).to receive(:klass) { klass }
24
+ allow(dynamic_index).to receive(:generate_index) { index }
25
+ end
26
+
27
+ it "should generate index for each object of the class" do
28
+ expect(dynamic_index).to receive(:generate_index).exactly(3).times
29
+ dynamic_index.generate!
22
30
  end
23
31
 
24
32
  it "should return a collection of indexes" do
@@ -26,7 +34,36 @@ describe Mongoid::Giza::DynamicIndex do
26
34
  end
27
35
 
28
36
  it "should only return indexes with unique names" do
29
- expect(dynamic_index.generate!.length).to eql(2)
37
+ expect(dynamic_index.generate!.length).to eql(1)
38
+ end
39
+
40
+ it "should not an Index if it's nil" do
41
+ allow(dynamic_index).to receive(:generate_index) { nil }
42
+ expect(dynamic_index.generate!.length).to eql(0)
43
+ end
44
+ end
45
+
46
+ describe "generate_index" do
47
+ let(:index) { double("index") }
48
+
49
+ before do
50
+ allow(Mongoid::Giza::Index).to receive(:new) { index }
51
+ end
52
+
53
+ it "should check if the object is from the index's class" do
54
+ expect(Mongoid::Giza::DynamicIndex.new(String, {}, Proc.new { }).generate_index(Array.new)).to eql(nil)
55
+ end
56
+
57
+ it "should execute the index dsl on the parameter" do
58
+ object = Object.new
59
+ block = Proc.new { }
60
+ expect(Docile).to receive(:dsl_eval).with(index, object, &block)
61
+ Mongoid::Giza::DynamicIndex.new(Object, {}, block).generate_index(object)
62
+ end
63
+
64
+ it "should return an Index" do
65
+ allow(Docile).to receive(:dsl_eval) { index }
66
+ expect(Mongoid::Giza::DynamicIndex.new(String, {}, Proc.new { }).generate_index("")).to be(index)
30
67
  end
31
68
  end
32
69
  end
@@ -124,6 +124,11 @@ describe Mongoid::Giza::Index do
124
124
  index.name(:Index)
125
125
  expect(index.name).to eql(:Index)
126
126
  end
127
+
128
+ it "should be converted to symbol" do
129
+ index.name("Index")
130
+ expect(index.name).to eql(:Index)
131
+ end
127
132
  end
128
133
 
129
134
  describe "generate_xmlpipe2" do
@@ -10,23 +10,20 @@ describe Mongoid::Giza::Indexer do
10
10
  let(:config) { Mongoid::Giza::Configuration.instance }
11
11
 
12
12
  describe "index!" do
13
- it "should create the sphinx configuration file" do
14
- allow(@controller).to receive(:index)
15
- expect(config).to receive(:render)
16
- @indexer.index!
17
- end
18
-
19
13
  it "should execute the sphinx indexer" do
20
- allow(config).to receive(:render)
21
- expect(@controller).to receive(:index).with(verbose: true)
14
+ expect(@controller).to receive(:index).with(no_args())
22
15
  @indexer.index!
23
16
  end
24
17
 
25
18
  it "should accept an index list" do
26
- allow(config).to receive(:render)
27
- expect(@controller).to receive(:index).with(:Person, :Person_2, verbose: true)
19
+ expect(@controller).to receive(:index).with(:Person, :Person_2)
28
20
  @indexer.index!(:Person, :Person_2)
29
21
  end
22
+
23
+ it "should accept verbose option" do
24
+ expect(@controller).to receive(:index).with(verbose: true)
25
+ @indexer.index!(verbose: true)
26
+ end
30
27
  end
31
28
 
32
29
  describe "full_index!" do
@@ -34,6 +31,7 @@ describe Mongoid::Giza::Indexer do
34
31
 
35
32
  before do
36
33
  allow(@indexer).to receive(:index!)
34
+ allow(config).to receive(:render)
37
35
  end
38
36
 
39
37
  it "should clear the generated indexes from the configuration" do
@@ -50,6 +48,14 @@ describe Mongoid::Giza::Indexer do
50
48
  @indexer.full_index!
51
49
  end
52
50
 
51
+ it "should create the sphinx configuration file" do
52
+ allow(config).to receive(:clear_generated_indexes)
53
+ allow(@indexer).to receive(:giza_classes) { [klass] }
54
+ allow(klass).to receive(:regenerate_dynamic_sphinx_indexes)
55
+ expect(config).to receive(:render)
56
+ @indexer.full_index!
57
+ end
58
+
53
59
  it "should execute the indexer" do
54
60
  allow(config).to receive(:clear_generated_indexes)
55
61
  allow(@indexer).to receive(:giza_classes) { [klass] }
@@ -287,4 +287,37 @@ describe Mongoid::Giza do
287
287
  Person.clear_generated_sphinx_indexes_configuration
288
288
  end
289
289
  end
290
+
291
+ describe "generate_dynamic_sphinx_indexes" do
292
+ let(:person) { Person.new }
293
+
294
+ let(:dynamic_index) { double("dynamic index") }
295
+
296
+ let(:dynamic_index2) { double("dynamic index 2") }
297
+
298
+ let(:index) { double("index") }
299
+
300
+ let(:index2) { double("index 2") }
301
+
302
+ before do
303
+ allow(Person).to receive(:dynamic_sphinx_indexes) { [dynamic_index, dynamic_index] }
304
+ allow(dynamic_index).to receive(:generate_index) { index }
305
+ allow(index).to receive(:name) { :name }
306
+ end
307
+
308
+ it "should generate all the dynamic indexes of the class for the object" do
309
+ expect(dynamic_index).to receive(:generate_index).with(person).twice { index }
310
+ person.generate_dynamic_sphinx_indexes
311
+ end
312
+
313
+ it "should merge the resulting indexes to the class' generated indexes" do
314
+ expect(Person.generated_sphinx_indexes).to receive(:merge!).with({name: index}).twice
315
+ person.generate_dynamic_sphinx_indexes
316
+ end
317
+
318
+ it "should add the indexes to the configuration" do
319
+ expect(config).to receive(:add_index).with(index, true).twice
320
+ person.generate_dynamic_sphinx_indexes
321
+ end
322
+ end
290
323
  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.3.0
4
+ version: 0.4.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-01-23 00:00:00.000000000 Z
11
+ date: 2014-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -223,7 +223,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
223
223
  version: '0'
224
224
  requirements: []
225
225
  rubyforge_project:
226
- rubygems_version: 2.2.1
226
+ rubygems_version: 2.2.2
227
227
  signing_key:
228
228
  specification_version: 4
229
229
  summary: Mongoid layer for the Sphinx fulltext search server