mongoid-giza 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,6 @@
1
1
  module Mongoid
2
+ # :nodoc:
2
3
  module Giza
3
- VERSION = "0.5.1"
4
+ VERSION = "0.6.0"
4
5
  end
5
6
  end
@@ -2,19 +2,24 @@ require "builder"
2
2
 
3
3
  module Mongoid
4
4
  module Giza
5
+ # Represents the xmlpipe2 data source
5
6
  class XMLPipe2
6
-
7
- # Creates a new XMLPipe2 object based on the specified index and that will write to the specified buffer.
8
- # Note that the actual XML will be generated only when {#generate!} is called
7
+ # Creates a new XMLPipe2 object based on the specified index and that will
8
+ # write to the specified buffer.
9
+ # Note that the actual XML will be generated only when {#generate!} is
10
+ # called
9
11
  #
10
- # @param index [Mongoid::Giza::Index] the index which will be used to generate the data
12
+ # @param index [Mongoid::Giza::Index] the index which will be used to
13
+ # generate the data
11
14
  # @param buffer any object that supports the method <<
12
15
  def initialize(index, buffer)
13
16
  @index = index
14
17
  @xml = Builder::XmlMarkup.new(target: buffer)
15
18
  end
16
19
 
17
- # Generates a XML document with the {http://sphinxsearch.com/docs/current.html#xmlpipe2 xmlpipe2 specification}.
20
+ # Generates a XML document with the
21
+ # {http://sphinxsearch.com/docs/current.html#xmlpipe2
22
+ # xmlpipe2 specification}.
18
23
  # The buffer passed on object creation will contain the XML
19
24
  def generate!
20
25
  @xml.instruct! :xml, version: "1.0", encoding: "utf-8"
@@ -25,43 +30,84 @@ module Mongoid
25
30
  end
26
31
 
27
32
  # Generates the schema part of the XML document.
28
- # Used internally by {#generate!} so you should never need to call it directly
33
+ # Used internally by {#generate!} so you should never need to call it
34
+ # directly
29
35
  def generate_schema
30
36
  @xml.sphinx :schema do |schema|
31
37
  @index.fields.each do |field|
32
- attrs = {name: field.name}
33
- attrs[:attr] = :string if field.attribute
34
- schema.sphinx :field, attrs
38
+ schema.sphinx :field, field_attrs(field)
39
+ end
40
+ @index.attributes.each do |attribute|
41
+ schema.sphinx :attr, attribute_attrs(attribute)
35
42
  end
36
- @index.attributes.each { |attribute| schema.sphinx :attr, name: attribute.name, type: attribute.type }
37
43
  end
38
44
  end
39
45
 
46
+ # Returns a Hash of the field's attributes
47
+ #
48
+ # @return [Hash] The field's attributes
49
+ def field_attrs(field)
50
+ attrs = {name: field.name}
51
+ attrs[:attr] = :string if field.attribute
52
+ attrs
53
+ end
54
+
55
+ # Returns a Hash of the attribute's attributes
56
+ #
57
+ # @return [Hash] The attribute's attributes
58
+ def attribute_attrs(attribute)
59
+ attrs = {name: attribute.name, type: attribute.type}
60
+ attrs[:default] = attribute.default if attribute.default
61
+ attrs[:bits] = attribute.bits if attribute.bits
62
+ attrs
63
+ end
64
+
40
65
  # Generates the content part of the XML document.
41
- # Used internally by {#generate!} so you should never need to call it directly
66
+ # Used internally by {#generate!} so you should never need to call it
67
+ # directly
42
68
  def generate_docset
43
69
  @index.criteria.each do |object|
44
- @xml.sphinx :document, id: object.giza_id do
70
+ @xml.sphinx :document, id: object._giza_id do
45
71
  generate_doc_tags(@index.fields, object)
46
72
  generate_doc_tags(@index.attributes, object)
47
73
  end
48
74
  end
49
75
  end
50
76
 
51
- # Generates the tags with the content to be indexed of every field or attribute.
52
- # Used internally by {#generate_docset} so you should never need to call it directly
77
+ # Generates the tags with the content to be indexed of every field and
78
+ # attribute.
79
+ # Used internally by {#generate_docset} so you should never need to call
80
+ # it directly
53
81
  #
54
- # @param contents [Array] list of fields or attributes to generate the tags for
82
+ # @param contents [Array] list of fields or attributes to generate the
83
+ # tags for
55
84
  # @param object [Object] the object being indexed
56
85
  def generate_doc_tags(contents, object)
57
86
  contents.each do |content|
58
- if content.block.nil?
59
- @xml.tag! content.name, object[content.name]
60
- else
87
+ if content.block
61
88
  @xml.tag! content.name, content.block.call(object)
89
+ else
90
+ @xml.tag! content.name, process_value(content, object)
62
91
  end
63
92
  end
64
93
  end
94
+
95
+ # Process values
96
+ # * Converts Date, Time and DateTime objects to unix time
97
+ #
98
+ # @param content
99
+ # [Mongoid::Giza::Index::Field, Mongoid::Giza::Index::Attribute] field
100
+ # or attribute to process content
101
+ # @param object [Object] object being indexed
102
+ #
103
+ # @return the processed object attribute value
104
+ def process_value(content, object)
105
+ if content.is_a?(Index::Attribute) && content.type == :timestamp
106
+ return object[content.name].to_time.to_i
107
+ else
108
+ return object[content.name]
109
+ end
110
+ end
65
111
  end
66
112
  end
67
113
  end
@@ -1,21 +1,22 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
2
+ lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'mongoid/giza/version'
4
+ require "mongoid/giza/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "mongoid-giza"
8
8
  spec.version = Mongoid::Giza::VERSION
9
9
  spec.authors = ["Maurício Batista"]
10
10
  spec.email = ["eddloschi@gmail.com"]
11
- spec.description = %q{Mongoid layer for the Sphinx fulltext search server that supports block fields and dynamic indexes}
12
- spec.summary = %q{Mongoid layer for the Sphinx fulltext search server}
11
+ spec.description = "Mongoid layer for the Sphinx fulltext search server " \
12
+ "that supports block fields and dynamic indexes"
13
+ spec.summary = %(Mongoid layer for the Sphinx fulltext search server)
13
14
  spec.homepage = "https://github.com/yadevteam/mongoid-giza"
14
15
  spec.license = "MIT"
15
16
 
16
- spec.files = `git ls-files`.split($/)
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.files = `git ls-files`.split($RS)
18
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(/^(test|spec|features)\//)
19
20
  spec.require_paths = ["lib"]
20
21
 
21
22
  spec.add_development_dependency "bundler", "~> 1.3"
@@ -24,10 +25,11 @@ Gem::Specification.new do |spec|
24
25
  spec.add_development_dependency "mongoid-rspec", ">= 1.9"
25
26
  spec.add_development_dependency "yard", ">= 0.8.7"
26
27
  spec.add_development_dependency "database_cleaner", ">= 1.2.0"
28
+ spec.add_development_dependency "rubocop", ">= 0.29.0"
27
29
 
28
- spec.add_runtime_dependency "mongoid", ">= 3.1"
29
- spec.add_runtime_dependency "riddle", ">= 1.5"
30
+ spec.add_runtime_dependency "mongoid", ">= 4.0"
31
+ spec.add_runtime_dependency "riddle", ">= 1.5.11"
30
32
  spec.add_runtime_dependency "builder", ">= 3.0"
31
33
  spec.add_runtime_dependency "docile", ">= 1.1"
32
- spec.add_runtime_dependency "activesupport"
34
+ spec.add_runtime_dependency "activesupport", ">= 4.0"
33
35
  end
@@ -4,8 +4,10 @@ describe Mongoid::Giza::Configuration do
4
4
  before do
5
5
  @default_source = double("default_source")
6
6
  @default_index = double("default_index")
7
- allow(Riddle::Configuration::XMLSource).to receive(:new).with(:source, :xmlpipe2) { @default_source }
8
- allow(Riddle::Configuration::Index).to receive(:new).with(:index, @default_source) { @default_index }
7
+ allow(Riddle::Configuration::XMLSource).to receive(:new)
8
+ .with(:source, :xmlpipe2) { @default_source }
9
+ allow(Riddle::Configuration::Index).to receive(:new)
10
+ .with(:index, @default_source) { @default_index }
9
11
  @config = Mongoid::Giza::Configuration.send(:new)
10
12
  end
11
13
 
@@ -14,7 +16,8 @@ describe Mongoid::Giza::Configuration do
14
16
  expect(@config.index).to be(@default_index)
15
17
  end
16
18
 
17
- it "should create a Riddle::Configuration::XMLSource for default settings" do
19
+ it "should create a Riddle::Configuration::XMLSource for default " \
20
+ "settings" do
18
21
  expect(@config.source).to be(@default_source)
19
22
  end
20
23
 
@@ -35,33 +38,44 @@ describe Mongoid::Giza::Configuration do
35
38
  let(:file_open) { allow(File).to receive(:open).with("giza.yml") { file } }
36
39
 
37
40
  it "should load the configuration file" do
38
- expect(file).to receive(:read) { "test:\n searchd:\n address: localhost" }
41
+ expect(file).to receive(:read) do
42
+ "test:\n searchd:\n address: localhost"
43
+ end
39
44
  expect(File).to receive(:open).with("giza.yml") { file }
40
45
  @config.load("giza.yml", "test")
41
46
  end
42
47
 
43
48
  it "should set settings" do
44
- allow(file).to receive(:read) { "test:\n searchd:\n address: localhost" }
49
+ allow(file).to receive(:read) do
50
+ "test:\n searchd:\n address: localhost"
51
+ end
45
52
  file_open
46
53
  @config.load("giza.yml", "test")
47
54
  expect(@config.searchd.address).to eql("localhost")
48
55
  end
49
56
 
50
57
  it "should ignore non-existent sections" do
51
- allow(file).to receive(:read) { "test:\n miss_section:\n address: localhost" }
58
+ allow(file).to receive(:read) do
59
+ "test:\n miss_section:\n address: localhost"
60
+ end
52
61
  file_open
53
62
  expect { @config.load("giza.yml", "test") }.not_to raise_error
54
63
  end
55
64
 
56
65
  it "should ignore non-existent settings" do
57
- allow(file).to receive(:read) { "test:\n searchd:\n miss_setting: false" }
58
- expect(@config.searchd).not_to receive(:method_missing).with(:miss_setting=, false)
66
+ allow(file).to receive(:read) do
67
+ "test:\n searchd:\n miss_setting: false"
68
+ end
69
+ expect(@config.searchd).not_to receive(:method_missing)
70
+ .with(:miss_setting=, false)
59
71
  file_open
60
72
  @config.load("giza.yml", "test")
61
73
  end
62
74
 
63
75
  it "should interpolate the string with ERB" do
64
- allow(file).to receive(:read) { "test:\n searchd:\n address: localhost" }
76
+ allow(file).to receive(:read) do
77
+ "test:\n searchd:\n address: localhost"
78
+ end
65
79
  expect(@config).to receive(:interpolate_string).with("localhost", nil)
66
80
  file_open
67
81
  @config.load("giza.yml", "test")
@@ -75,7 +89,9 @@ describe Mongoid::Giza::Configuration do
75
89
  end
76
90
 
77
91
  it "should not interpolate source settings" do
78
- allow(file).to receive(:read) { "test:\n source:\n xmlpipe_command: cmd" }
92
+ allow(file).to receive(:read) do
93
+ "test:\n source:\n xmlpipe_command: cmd"
94
+ end
79
95
  expect(@config).not_to receive(:interpolate_string)
80
96
  file_open
81
97
  @config.load("giza.yml", "test")
@@ -142,14 +158,18 @@ describe Mongoid::Giza::Configuration do
142
158
  before do
143
159
  allow(Riddle::Configuration::Index).to receive(:settings) { [] }
144
160
  allow(Riddle::Configuration::XMLSource).to receive(:new) { source }
145
- allow(Riddle::Configuration::Index).to receive(:new).with(index.name, source) { riddle_index }
161
+ allow(Riddle::Configuration::Index).to receive(:new)
162
+ .with(index.name, source) { riddle_index }
146
163
  allow(@config).to receive(:apply_default_settings)
147
164
  allow(@config).to receive(:apply_user_settings)
148
165
  end
149
166
 
150
167
  it "should create a xmlpipe2 source with the same name of the index" do
151
- expect(Riddle::Configuration::XMLSource).to receive(:new).with(index.name, :xmlpipe2) { source }
152
- allow(Riddle::Configuration::Index).to receive(:new) { double("riddle_index").as_null_object }
168
+ expect(Riddle::Configuration::XMLSource).to receive(:new)
169
+ .with(index.name, :xmlpipe2) { source }
170
+ allow(Riddle::Configuration::Index).to receive(:new) do
171
+ double("riddle_index").as_null_object
172
+ end
153
173
  @config.create_index(index)
154
174
  end
155
175
 
@@ -158,17 +178,20 @@ describe Mongoid::Giza::Configuration do
158
178
  allow(@default_index).to receive(:path) { "/path/to/index" }
159
179
  allow(riddle_index).to receive(:path=).with("/path/to/index")
160
180
  allow(riddle_index).to receive(:path) { "/path/to/index" }
161
- expect(riddle_index).to receive(:path=).with("/path/to/index/#{index.name}")
181
+ expect(riddle_index).to receive(:path=)
182
+ .with("/path/to/index/#{index.name}")
162
183
  @config.create_index(index)
163
184
  end
164
185
 
165
186
  it "should apply default settings to the index" do
166
- expect(@config).to receive(:apply_default_settings).with(@default_index, riddle_index, index)
187
+ expect(@config).to receive(:apply_default_settings)
188
+ .with(@default_index, riddle_index, index)
167
189
  @config.create_index(index)
168
190
  end
169
191
 
170
192
  it "should apply default settings to the source" do
171
- expect(@config).to receive(:apply_default_settings).with(@default_source, source, index)
193
+ expect(@config).to receive(:apply_default_settings)
194
+ .with(@default_source, source, index)
172
195
  @config.create_index(index)
173
196
  end
174
197
 
@@ -188,38 +211,30 @@ describe Mongoid::Giza::Configuration do
188
211
  end
189
212
 
190
213
  describe "register_index" do
191
- let(:indices) { double("indices") }
192
-
193
- let(:length) { double("length") }
194
-
195
- let(:index) { double("index") }
196
-
197
- let(:indexes) { double("indexes") }
214
+ let(:riddle_index) do
215
+ instance_double("Riddle::Configuration::Index", name: "name")
216
+ end
198
217
 
199
- before do
200
- allow(@config).to receive(:indices) { indices }
201
- allow(indices).to receive(:length) { length }
202
- allow(index).to receive(:name) { :index }
203
- allow(indexes).to receive(:[]=)
204
- allow(indexes).to receive(:has_key?)
218
+ let(:riddle_index_copy) do
219
+ instance_double("Riddle::Configuration::Index", name: "name")
205
220
  end
206
221
 
222
+ let(:indexes) { {riddle_index.name => riddle_index} }
223
+
207
224
  it "should add the index to the given hash" do
208
- expect(indexes).to receive(:[]=).with(:index, index)
209
- @config.register_index(index, indexes)
225
+ @config.register_index(riddle_index, indexes)
226
+ expect(indexes).to include riddle_index.name => riddle_index
210
227
  end
211
228
 
212
- it "should return the position to replace the index on the configuration indices" do
213
- position = double("position")
214
- allow(indexes).to receive(:has_key?).with(:index) { true }
215
- allow(indexes).to receive(:[]).with(:index) { index }
216
- allow(indices).to receive(:index).with(index) { position }
217
- expect(@config.register_index(index, indexes)).to be(position)
229
+ it "should return the position of the index in the indices array" do
230
+ @config.indices.push(riddle_index)
231
+ position = @config.register_index(riddle_index_copy, indexes)
232
+ expect(position).to eql 0
218
233
  end
219
234
 
220
- it "should return the position to add the index on the configuration indices" do
221
- allow(indexes).to receive(:has_key?).with(:index) { false }
222
- expect(@config.register_index(index, indexes)).to be(length)
235
+ it "should return the indices array length if it's not on the array" do
236
+ position = @config.register_index(riddle_index, indexes)
237
+ expect(position).to eql @config.indices.length
223
238
  end
224
239
  end
225
240
 
@@ -317,8 +332,9 @@ describe Mongoid::Giza::Configuration do
317
332
  allow(@config.indexer).to receive(:render) { "indexer" }
318
333
  allow(@config.searchd).to receive(:render) { "searchd" }
319
334
  allow(index).to receive(:render) { "source\nindex" }
320
- expect(File).to receive(:open).with(@config.file.output_path, "w").and_yield(file)
321
- expect(file).to receive(:write).with("indexer\nsearchd\nsource\nindex")
335
+ expect(File).to receive(:open).with(@config.file.output_path, "w")
336
+ .and_yield(file)
337
+ expect(file).to receive(:write).with("\nindexer\nsearchd\nsource\nindex")
322
338
  @config.render
323
339
  end
324
340
  end
@@ -327,7 +343,7 @@ describe Mongoid::Giza::Configuration do
327
343
  let(:indices) { double("indices") }
328
344
 
329
345
  before do
330
- @config.instance_variable_set("@generated_indexes", {name: :index})
346
+ @config.instance_variable_set("@generated_indexes", name: :index)
331
347
  end
332
348
 
333
349
  it "should delete the generated riddle indexes" do
@@ -356,7 +372,8 @@ describe Mongoid::Giza::Configuration do
356
372
  @config.setter(section, :setting, value)
357
373
  end
358
374
 
359
- it "should no set the value if the section does not respond to the attribute setter" do
375
+ it "should no set the value if the section does not respond to the " \
376
+ "attribute setter" do
360
377
  allow(section).to receive(:respond_to?).with("setting=") { false }
361
378
  expect(section).not_to receive("setting=")
362
379
  @config.setter(section, :setting, value)
@@ -367,7 +384,8 @@ describe Mongoid::Giza::Configuration do
367
384
  let(:indices) { double("indices") }
368
385
 
369
386
  before do
370
- @config.instance_variable_set("@generated_indexes", {name: :index, two: :index2})
387
+ @config.instance_variable_set("@generated_indexes", name: :index,
388
+ two: :index2)
371
389
  end
372
390
 
373
391
  it "should remove the indexes from the indices array" do
@@ -378,7 +396,8 @@ describe Mongoid::Giza::Configuration do
378
396
 
379
397
  it "should remove the index from the generated indexes collection" do
380
398
  @config.remove_generated_indexes([:name])
381
- expect(@config.instance_variable_get("@generated_indexes")).not_to include(name: :index)
399
+ expect(@config.instance_variable_get("@generated_indexes"))
400
+ .not_to include(name: :index)
382
401
  end
383
402
  end
384
403
  end
@@ -3,7 +3,7 @@ require "spec_helper"
3
3
  describe Mongoid::Giza::DynamicIndex do
4
4
  describe "initialize" do
5
5
  it "should accept the class, settings and a proc" do
6
- dynamic_index = Mongoid::Giza::DynamicIndex.new(Object, {}, Proc.new { })
6
+ dynamic_index = Mongoid::Giza::DynamicIndex.new(Object, {}, -> {})
7
7
  expect(dynamic_index.klass).to be(Object)
8
8
  expect(dynamic_index.settings).to eql({})
9
9
  expect(dynamic_index.block).to be_a_kind_of(Proc)
@@ -11,7 +11,7 @@ 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 { }) }
14
+ let(:dynamic_index) { Mongoid::Giza::DynamicIndex.new(Object, {}, -> {}) }
15
15
 
16
16
  let(:index) { double("index") }
17
17
 
@@ -51,19 +51,21 @@ describe Mongoid::Giza::DynamicIndex do
51
51
  end
52
52
 
53
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)
54
+ expect(Mongoid::Giza::DynamicIndex.new(String, {}, -> {})
55
+ .generate_index([])).to eql(nil)
55
56
  end
56
57
 
57
58
  it "should execute the index dsl on the parameter" do
58
59
  object = Object.new
59
- block = Proc.new { }
60
+ block = proc {}
60
61
  expect(Docile).to receive(:dsl_eval).with(index, object, &block)
61
62
  Mongoid::Giza::DynamicIndex.new(Object, {}, block).generate_index(object)
62
63
  end
63
64
 
64
65
  it "should return an Index" do
65
66
  allow(Docile).to receive(:dsl_eval) { index }
66
- expect(Mongoid::Giza::DynamicIndex.new(String, {}, Proc.new { }).generate_index("")).to be(index)
67
+ expect(Mongoid::Giza::DynamicIndex.new(String, {}, -> {})
68
+ .generate_index("")).to be(index)
67
69
  end
68
70
  end
69
71
  end