mongoid-giza 0.6.2 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +13 -3
- data/.travis.yml +4 -3
- data/CHANGELOG.md +13 -0
- data/README.md +12 -2
- data/lib/mongoid/giza.rb +3 -3
- data/lib/mongoid/giza/configuration.rb +15 -6
- data/lib/mongoid/giza/index.rb +6 -6
- data/lib/mongoid/giza/index/attribute.rb +6 -5
- data/lib/mongoid/giza/search.rb +14 -7
- data/lib/mongoid/giza/version.rb +1 -1
- data/lib/mongoid/giza/xml_pipe2.rb +1 -2
- data/mongoid-giza.gemspec +4 -2
- data/spec/mongoid/giza/configuration_spec.rb +2 -2
- data/spec/mongoid/giza/search_spec.rb +7 -2
- data/spec/mongoid/giza_spec.rb +2 -3
- data/spec/spec_helper.rb +1 -1
- metadata +16 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8593011380c8765b61fdf11371d82e7427889d30
|
4
|
+
data.tar.gz: 5d74f081406d8d50a3c76f588d17283ca2734606
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b3fc45f6e90137143f01bce79c31a525a1fada5174077c89ee7ac0dfe8aa267f8f219fdcf8a244b7bdbebe74f0082fa9e988e31b1c05294259b26d1ea1c0c920
|
7
|
+
data.tar.gz: edadbb151bff73bb6834a05db6775b2565e1c4a4006d3169abbacb6abbcc310ba4c42c45ea8f5c31729d02fcfab77585fe7a6f8e5c4cf0e50a4c5673a08fae99
|
data/.rubocop.yml
CHANGED
@@ -1,10 +1,20 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.0
|
3
|
+
|
4
|
+
Metrics/BlockLength:
|
5
|
+
Exclude:
|
6
|
+
- "**/*_spec.rb"
|
7
|
+
- "mongoid-giza.gemspec"
|
3
8
|
|
4
9
|
Style/Encoding:
|
5
10
|
Enabled: true
|
6
11
|
EnforcedStyle: when_needed
|
7
12
|
|
13
|
+
Style/FrozenStringLiteralComment:
|
14
|
+
Enabled: false
|
15
|
+
|
16
|
+
Style/SpaceInsideHashLiteralBraces:
|
17
|
+
EnforcedStyle: no_space
|
18
|
+
|
8
19
|
Style/StringLiterals:
|
9
20
|
EnforcedStyle: double_quotes
|
10
|
-
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 0.7.0
|
4
|
+
|
5
|
+
#### Mongoid::Giza now requires Ruby >= 2.0
|
6
|
+
|
7
|
+
### Breaking changes
|
8
|
+
|
9
|
+
* `Mongoid::Giza::Search#order_by` now accepts multiple ordering as a `Hash` (i.e.: `order_by age: :asc, :@id => :desc`)
|
10
|
+
|
11
|
+
### Fixes
|
12
|
+
|
13
|
+
* Defined **Mongoid < 5.0** and **ActiveSupport < 5.0** as greater versions are not yet supported
|
14
|
+
* Use `YAML#safe_load` when available **(Ruby >= 2.1)**
|
15
|
+
|
3
16
|
## 0.6.2
|
4
17
|
|
5
18
|
* Allow underscore on fields' and attributes' names
|
data/README.md
CHANGED
@@ -180,8 +180,9 @@ Every other [Riddle::Client](http://rdoc.info/github/pat/riddle/Riddle/Client) s
|
|
180
180
|
```ruby
|
181
181
|
result = Person.search do
|
182
182
|
fulltext "john"
|
183
|
-
with :age 18..40
|
184
|
-
|
183
|
+
with :age, 18..40
|
184
|
+
sort_mode :extended
|
185
|
+
order_by age: :asc, :@id => :desc
|
185
186
|
end
|
186
187
|
|
187
188
|
result[:Person].each do |person|
|
@@ -189,6 +190,15 @@ result[:Person].each do |person|
|
|
189
190
|
end
|
190
191
|
```
|
191
192
|
|
193
|
+
#### Sorting MongoDB results
|
194
|
+
|
195
|
+
MongoDB doesn't return the documents on the "arbitrary" order defined by Sphinx. To maintain the Sphinx ordering you can do:
|
196
|
+
|
197
|
+
```ruby
|
198
|
+
giza_ids = result[:matches].map { |match| match[:doc] }
|
199
|
+
people = result[:Person].sort_by { |person| giza_ids.index(person._giza_id) }
|
200
|
+
```
|
201
|
+
|
192
202
|
## TODO
|
193
203
|
|
194
204
|
* Support delta indexing
|
data/lib/mongoid/giza.rb
CHANGED
@@ -59,7 +59,7 @@ module Mongoid
|
|
59
59
|
def generate_sphinx_indexes
|
60
60
|
self.class.dynamic_sphinx_indexes.each do |dynamic_index|
|
61
61
|
index = dynamic_index.generate_index(self)
|
62
|
-
self.class.generated_sphinx_indexes
|
62
|
+
self.class.generated_sphinx_indexes[index.name] = index
|
63
63
|
self.class.giza_configuration.add_index(index, true)
|
64
64
|
end
|
65
65
|
end
|
@@ -174,13 +174,13 @@ module Mongoid
|
|
174
174
|
# @param names [Array] a list of index names of this class that will be
|
175
175
|
# indexed
|
176
176
|
def sphinx_indexer!(*names)
|
177
|
-
if names.
|
177
|
+
if !names.empty?
|
178
178
|
indexes_names =
|
179
179
|
sphinx_indexes_names.select { |name| names.include?(name) }
|
180
180
|
else
|
181
181
|
indexes_names = sphinx_indexes_names
|
182
182
|
end
|
183
|
-
Indexer.instance.index!(*indexes_names)
|
183
|
+
Indexer.instance.index!(*indexes_names) unless indexes_names.empty?
|
184
184
|
end
|
185
185
|
|
186
186
|
# Retrieves all the sphinx indexes defined on this class, static and
|
@@ -27,8 +27,8 @@ module Mongoid
|
|
27
27
|
# defined
|
28
28
|
# @param env [String] environment whoose settings will be loaded
|
29
29
|
def load(path, env)
|
30
|
-
|
31
|
-
section = instance_variable_get("@#{
|
30
|
+
yaml_safe_load(File.open(path).read)[env].each do |section, settings|
|
31
|
+
section = instance_variable_get("@#{section}")
|
32
32
|
next unless section
|
33
33
|
settings.each do |setting, value|
|
34
34
|
unless section == @index || section == @source
|
@@ -98,7 +98,7 @@ module Mongoid
|
|
98
98
|
# set
|
99
99
|
def apply_default_settings(default, section, index)
|
100
100
|
default.class.settings.each do |setting|
|
101
|
-
value = interpolate_string(default.send(
|
101
|
+
value = interpolate_string(default.send(setting.to_s), index)
|
102
102
|
setter(section, setting, value) unless value.nil?
|
103
103
|
end
|
104
104
|
end
|
@@ -128,7 +128,7 @@ module Mongoid
|
|
128
128
|
# a generated {Mongoid::Giza::Index}
|
129
129
|
def clear_generated_indexes
|
130
130
|
@generated_indexes.each { |_, index| indices.delete(index) }
|
131
|
-
@generated_indexes =
|
131
|
+
@generated_indexes = {}
|
132
132
|
end
|
133
133
|
|
134
134
|
# Removes Riddle::Index's specifieds as params
|
@@ -157,9 +157,8 @@ module Mongoid
|
|
157
157
|
namespace = index.nil? ? Object.new : OpenStruct.new(index: index)
|
158
158
|
if value.is_a?(String)
|
159
159
|
return ERB.new(value).result(namespace.instance_eval { binding })
|
160
|
-
else
|
161
|
-
return value
|
162
160
|
end
|
161
|
+
value
|
163
162
|
end
|
164
163
|
|
165
164
|
# Helper method to set a value to a setting from a section (i.e. indexer,
|
@@ -174,6 +173,16 @@ module Mongoid
|
|
174
173
|
method = "#{setting}="
|
175
174
|
section.send(method, value) if section.respond_to?(method)
|
176
175
|
end
|
176
|
+
|
177
|
+
private
|
178
|
+
|
179
|
+
def yaml_safe_load(yaml_code)
|
180
|
+
if YAML.respond_to?(:safe_load)
|
181
|
+
YAML.safe_load(yaml_code)
|
182
|
+
else
|
183
|
+
YAML.load(yaml_code) # rubocop:disable Security/YAMLLoad
|
184
|
+
end
|
185
|
+
end
|
177
186
|
end
|
178
187
|
end
|
179
188
|
end
|
data/lib/mongoid/giza/index.rb
CHANGED
@@ -20,7 +20,7 @@ module Mongoid
|
|
20
20
|
Hash => :json,
|
21
21
|
BSON::ObjectId => :string,
|
22
22
|
ActiveSupport::TimeWithZone => :timestamp
|
23
|
-
}
|
23
|
+
}.freeze
|
24
24
|
|
25
25
|
attr_accessor :klass, :settings, :fields, :attributes
|
26
26
|
|
@@ -79,11 +79,11 @@ module Mongoid
|
|
79
79
|
def attribute(name, type = nil, options = {}, &block)
|
80
80
|
unless type
|
81
81
|
field = @klass.fields[name.to_s]
|
82
|
-
if field
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
82
|
+
type = if field
|
83
|
+
TYPES_MAP[field.type] || :string
|
84
|
+
else
|
85
|
+
:string
|
86
|
+
end
|
87
87
|
end
|
88
88
|
@attributes << Attribute.new(name, type, options, &block)
|
89
89
|
end
|
@@ -9,7 +9,7 @@ module Mongoid
|
|
9
9
|
TYPES = [
|
10
10
|
:uint, :bool, :bigint, :timestamp, :float,
|
11
11
|
:multi, :multi_64, :string, :json
|
12
|
-
]
|
12
|
+
].freeze
|
13
13
|
|
14
14
|
attr_accessor :default, :bits, :block
|
15
15
|
attr_reader :name, :type
|
@@ -31,10 +31,11 @@ module Mongoid
|
|
31
31
|
# @raise [TypeError] if the type is not valid. (see
|
32
32
|
# {Mongoid::Giza::Index::Attribute::TYPES})
|
33
33
|
def initialize(name, type, options = {}, &block)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
unless TYPES.include? type
|
35
|
+
raise TypeError,
|
36
|
+
"Attribute type not supported. It must be one of the " \
|
37
|
+
"following: #{TYPES.join(', ')}"
|
38
|
+
end
|
38
39
|
@name = normalize(name)
|
39
40
|
@type = type
|
40
41
|
@block = block
|
data/lib/mongoid/giza/search.rb
CHANGED
@@ -5,7 +5,7 @@ module Mongoid
|
|
5
5
|
attr_accessor :indexes, :query_string
|
6
6
|
attr_reader :client
|
7
7
|
|
8
|
-
|
8
|
+
alias fulltext query_string=
|
9
9
|
|
10
10
|
# Creates a new search
|
11
11
|
#
|
@@ -43,18 +43,19 @@ module Mongoid
|
|
43
43
|
|
44
44
|
# Sets the order in which the results will be returned
|
45
45
|
#
|
46
|
-
# @param
|
47
|
-
#
|
48
|
-
|
49
|
-
|
50
|
-
|
46
|
+
# @param ordering [Hash] a hash with attribute names as keys and order
|
47
|
+
# as values (i.e.: attr1: :desc, attr2: :asc)
|
48
|
+
def order_by(ordering)
|
49
|
+
@client.sort_by = ordering.map do |attribute, order|
|
50
|
+
"#{attribute} #{order.to_s.upcase}"
|
51
|
+
end.join(", ")
|
51
52
|
end
|
52
53
|
|
53
54
|
# Executes the configured query
|
54
55
|
#
|
55
56
|
# @return [Array] an Array of Hashes as specified by Riddle::Response
|
56
57
|
def run
|
57
|
-
index = indexes.
|
58
|
+
index = !indexes.empty? ? indexes.join(" ") : "*"
|
58
59
|
@client.query(query_string, index)
|
59
60
|
end
|
60
61
|
|
@@ -92,6 +93,12 @@ module Mongoid
|
|
92
93
|
@client.send method, *args
|
93
94
|
end
|
94
95
|
end
|
96
|
+
|
97
|
+
def respond_to_missing?(method, include_private = false)
|
98
|
+
@client.respond_to?(method) ||
|
99
|
+
@client.respond_to?("#{method}=") ||
|
100
|
+
super
|
101
|
+
end
|
95
102
|
end
|
96
103
|
end
|
97
104
|
end
|
data/lib/mongoid/giza/version.rb
CHANGED
@@ -104,9 +104,8 @@ module Mongoid
|
|
104
104
|
def process_value(content, object)
|
105
105
|
if content.is_a?(Index::Attribute) && content.type == :timestamp
|
106
106
|
return object[content.name].to_time.to_i
|
107
|
-
else
|
108
|
-
return object[content.name]
|
109
107
|
end
|
108
|
+
object[content.name]
|
110
109
|
end
|
111
110
|
end
|
112
111
|
end
|
data/mongoid-giza.gemspec
CHANGED
@@ -19,6 +19,8 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
+
spec.required_ruby_version = ">= 2.0"
|
23
|
+
|
22
24
|
spec.add_development_dependency "bundler", "~> 1.3"
|
23
25
|
spec.add_development_dependency "rake"
|
24
26
|
spec.add_development_dependency "rspec", ">= 2.14"
|
@@ -27,9 +29,9 @@ Gem::Specification.new do |spec|
|
|
27
29
|
spec.add_development_dependency "database_cleaner", ">= 1.2.0"
|
28
30
|
spec.add_development_dependency "rubocop", ">= 0.29.0"
|
29
31
|
|
30
|
-
spec.add_runtime_dependency "mongoid", ">= 4.0"
|
32
|
+
spec.add_runtime_dependency "mongoid", ">= 4.0", "< 5.0"
|
31
33
|
spec.add_runtime_dependency "riddle", ">= 1.5.11"
|
32
34
|
spec.add_runtime_dependency "builder", ">= 3.0"
|
33
35
|
spec.add_runtime_dependency "docile", ">= 1.1"
|
34
|
-
spec.add_runtime_dependency "activesupport", ">= 4.0"
|
36
|
+
spec.add_runtime_dependency "activesupport", ">= 4.0", "< 5.0"
|
35
37
|
end
|
@@ -359,7 +359,7 @@ describe Mongoid::Giza::Configuration do
|
|
359
359
|
end
|
360
360
|
|
361
361
|
describe "setter" do
|
362
|
-
let(:section) { double("section")
|
362
|
+
let(:section) { double("section") }
|
363
363
|
|
364
364
|
let(:value) { double("value") }
|
365
365
|
|
@@ -381,7 +381,7 @@ describe Mongoid::Giza::Configuration do
|
|
381
381
|
end
|
382
382
|
|
383
383
|
describe "remove_generated_indexes" do
|
384
|
-
let(:indices) { double("indices")
|
384
|
+
let(:indices) { double("indices") }
|
385
385
|
|
386
386
|
before do
|
387
387
|
@config.instance_variable_set("@generated_indexes", name: :index,
|
@@ -22,7 +22,7 @@ describe Mongoid::Giza::Search do
|
|
22
22
|
|
23
23
|
it "should accept a index list" do
|
24
24
|
indexes = Mongoid::Giza::Search.new("localhost", 9132, [:index1, :index2])
|
25
|
-
|
25
|
+
.indexes
|
26
26
|
expect(indexes).to eql([:index1, :index2])
|
27
27
|
end
|
28
28
|
end
|
@@ -59,7 +59,12 @@ describe Mongoid::Giza::Search do
|
|
59
59
|
describe "order_by" do
|
60
60
|
it "should set the search order" do
|
61
61
|
expect(client).to receive(:sort_by=).with("attr ASC")
|
62
|
-
search.order_by(:
|
62
|
+
search.order_by(attr: :asc)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should accept a multiple ordering" do
|
66
|
+
expect(client).to receive(:sort_by=).with("attr1 ASC, attr2 DESC")
|
67
|
+
search.order_by(attr1: :asc, attr2: :desc)
|
63
68
|
end
|
64
69
|
end
|
65
70
|
|
data/spec/mongoid/giza_spec.rb
CHANGED
@@ -287,14 +287,13 @@ describe Mongoid::Giza do
|
|
287
287
|
|
288
288
|
it "should generate all the dynamic indexes of the class for the object" do
|
289
289
|
expect(dynamic_index).to receive(:generate_index).with(person)
|
290
|
-
|
290
|
+
.twice { index }
|
291
291
|
person.generate_sphinx_indexes
|
292
292
|
end
|
293
293
|
|
294
294
|
it "should merge the resulting indexes to the class' generated indexes" do
|
295
|
-
expect(Person.generated_sphinx_indexes).to receive(:merge!)
|
296
|
-
.with(name: index).twice
|
297
295
|
person.generate_sphinx_indexes
|
296
|
+
expect(Person.generated_sphinx_indexes[:name]).to be(index)
|
298
297
|
end
|
299
298
|
|
300
299
|
it "should add the indexes to the configuration" do
|
data/spec/spec_helper.rb
CHANGED
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
|
+
version: 0.7.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:
|
11
|
+
date: 2017-03-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -115,6 +115,9 @@ dependencies:
|
|
115
115
|
- - ">="
|
116
116
|
- !ruby/object:Gem::Version
|
117
117
|
version: '4.0'
|
118
|
+
- - "<"
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '5.0'
|
118
121
|
type: :runtime
|
119
122
|
prerelease: false
|
120
123
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -122,6 +125,9 @@ dependencies:
|
|
122
125
|
- - ">="
|
123
126
|
- !ruby/object:Gem::Version
|
124
127
|
version: '4.0'
|
128
|
+
- - "<"
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '5.0'
|
125
131
|
- !ruby/object:Gem::Dependency
|
126
132
|
name: riddle
|
127
133
|
requirement: !ruby/object:Gem::Requirement
|
@@ -171,6 +177,9 @@ dependencies:
|
|
171
177
|
- - ">="
|
172
178
|
- !ruby/object:Gem::Version
|
173
179
|
version: '4.0'
|
180
|
+
- - "<"
|
181
|
+
- !ruby/object:Gem::Version
|
182
|
+
version: '5.0'
|
174
183
|
type: :runtime
|
175
184
|
prerelease: false
|
176
185
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -178,6 +187,9 @@ dependencies:
|
|
178
187
|
- - ">="
|
179
188
|
- !ruby/object:Gem::Version
|
180
189
|
version: '4.0'
|
190
|
+
- - "<"
|
191
|
+
- !ruby/object:Gem::Version
|
192
|
+
version: '5.0'
|
181
193
|
description: Mongoid layer for the Sphinx fulltext search server that supports block
|
182
194
|
fields and dynamic indexes
|
183
195
|
email:
|
@@ -232,7 +244,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
232
244
|
requirements:
|
233
245
|
- - ">="
|
234
246
|
- !ruby/object:Gem::Version
|
235
|
-
version: '0'
|
247
|
+
version: '2.0'
|
236
248
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
237
249
|
requirements:
|
238
250
|
- - ">="
|
@@ -240,7 +252,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
240
252
|
version: '0'
|
241
253
|
requirements: []
|
242
254
|
rubyforge_project:
|
243
|
-
rubygems_version: 2.
|
255
|
+
rubygems_version: 2.6.10
|
244
256
|
signing_key:
|
245
257
|
specification_version: 4
|
246
258
|
summary: Mongoid layer for the Sphinx fulltext search server
|
@@ -257,4 +269,3 @@ test_files:
|
|
257
269
|
- spec/mongoid/giza/xml_pipe2_spec.rb
|
258
270
|
- spec/mongoid/giza_spec.rb
|
259
271
|
- spec/spec_helper.rb
|
260
|
-
has_rdoc:
|