dnif 0.0.1.beta.3 → 0.0.1.beta.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +52 -0
- data/Rakefile +24 -26
- data/dnif.gemspec +81 -55
- data/lib/dnif.rb +73 -24
- data/lib/dnif/configuration.rb +4 -3
- data/lib/dnif/document.rb +8 -8
- data/lib/dnif/indexer.rb +0 -2
- data/lib/dnif/schema.rb +1 -1
- data/lib/dnif/search.rb +1 -50
- data/lib/dnif/tasks.rb +11 -11
- data/lib/dnif/version.rb +10 -0
- data/spec/dnif/configuration_spec.rb +41 -0
- data/spec/dnif/dnif_spec.rb +157 -0
- data/spec/dnif/document_spec.rb +46 -0
- data/spec/dnif/index_spec.rb +20 -0
- data/spec/dnif/indexer_spec.rb +64 -0
- data/spec/dnif/multi_attribute_spec.rb +20 -0
- data/{test/unit/test_schema.rb → spec/dnif/schema_spec.rb} +5 -5
- data/spec/dnif/search_spec.rb +18 -0
- data/{test → spec}/fixtures/db/schema.rb +1 -0
- data/{test → spec}/fixtures/log/searchd.pid +0 -0
- data/{test → spec}/fixtures/models.rb +0 -0
- data/{test → spec}/fixtures/sphinx_1.xml +1 -0
- data/{test → spec}/fixtures/sphinx_2.xml +1 -0
- data/{test → spec}/fixtures/sphinx_3.xml +1 -0
- data/{test → spec}/fixtures/sphinx_4.xml +1 -0
- data/{test → spec}/fixtures/sphinx_5.xml +1 -0
- data/{test → spec}/fixtures/templates/config.erb +0 -0
- data/spec/spec_helper.rb +31 -0
- data/{test/test_helper.rb → spec/support/activerecord/models.rb} +2 -21
- data/templates/config.erb +38 -0
- metadata +158 -46
- data/lib/dnif/index_builder.rb +0 -30
- data/test/unit/test_configuration.rb +0 -37
- data/test/unit/test_dnif.rb +0 -21
- data/test/unit/test_document.rb +0 -41
- data/test/unit/test_index.rb +0 -21
- data/test/unit/test_index_builder.rb +0 -33
- data/test/unit/test_indexer.rb +0 -60
- data/test/unit/test_multi_attribute.rb +0 -17
- data/test/unit/test_search.rb +0 -116
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
gem "rake"
|
4
|
+
gem "activerecord", ">= 3.0.0", :require => "active_record"
|
5
|
+
gem "activesupport", ">= 3.0.0", :require => "active_support"
|
6
|
+
gem "riddle", ">= 1.1.0"
|
7
|
+
gem "builder", ">= 2.1.2"
|
8
|
+
|
9
|
+
group :development do
|
10
|
+
gem "jeweler", ">= 1.5.0.pre2"
|
11
|
+
gem "rspec", ">= 2.0.0.beta.20"
|
12
|
+
gem "database_cleaner"
|
13
|
+
gem "awesome_print", :require => "ap"
|
14
|
+
gem "sqlite3-ruby", :require => "sqlite3"
|
15
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.0.0)
|
5
|
+
activesupport (= 3.0.0)
|
6
|
+
builder (~> 2.1.2)
|
7
|
+
i18n (~> 0.4.1)
|
8
|
+
activerecord (3.0.0)
|
9
|
+
activemodel (= 3.0.0)
|
10
|
+
activesupport (= 3.0.0)
|
11
|
+
arel (~> 1.0.0)
|
12
|
+
tzinfo (~> 0.3.23)
|
13
|
+
activesupport (3.0.0)
|
14
|
+
arel (1.0.1)
|
15
|
+
activesupport (~> 3.0.0)
|
16
|
+
awesome_print (0.2.1)
|
17
|
+
builder (2.1.2)
|
18
|
+
database_cleaner (0.5.2)
|
19
|
+
diff-lcs (1.1.2)
|
20
|
+
git (1.2.5)
|
21
|
+
i18n (0.4.1)
|
22
|
+
jeweler (1.5.0.pre2)
|
23
|
+
bundler (>= 1.0.0.rc.5)
|
24
|
+
git (>= 1.2.5)
|
25
|
+
rake
|
26
|
+
rake (0.8.7)
|
27
|
+
riddle (1.1.0)
|
28
|
+
rspec (2.0.0.beta.20)
|
29
|
+
rspec-core (= 2.0.0.beta.20)
|
30
|
+
rspec-expectations (= 2.0.0.beta.20)
|
31
|
+
rspec-mocks (= 2.0.0.beta.20)
|
32
|
+
rspec-core (2.0.0.beta.20)
|
33
|
+
rspec-expectations (2.0.0.beta.20)
|
34
|
+
diff-lcs (>= 1.1.2)
|
35
|
+
rspec-mocks (2.0.0.beta.20)
|
36
|
+
sqlite3-ruby (1.3.1)
|
37
|
+
tzinfo (0.3.23)
|
38
|
+
|
39
|
+
PLATFORMS
|
40
|
+
ruby
|
41
|
+
|
42
|
+
DEPENDENCIES
|
43
|
+
activerecord (>= 3.0.0)
|
44
|
+
activesupport (>= 3.0.0)
|
45
|
+
awesome_print
|
46
|
+
builder (>= 2.1.2)
|
47
|
+
database_cleaner
|
48
|
+
jeweler (>= 1.5.0.pre2)
|
49
|
+
rake
|
50
|
+
riddle (>= 1.1.0)
|
51
|
+
rspec (>= 2.0.0.beta.20)
|
52
|
+
sqlite3-ruby
|
data/Rakefile
CHANGED
@@ -1,38 +1,36 @@
|
|
1
|
+
require "bundler"
|
1
2
|
begin
|
2
|
-
|
3
|
+
Bundler.setup(:default, :development)
|
4
|
+
rescue Bundler::BundlerError => e
|
5
|
+
$stderr.puts e.message
|
6
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
7
|
+
exit e.status_code
|
8
|
+
end
|
3
9
|
|
4
|
-
|
5
|
-
gemspec.name = "dnif"
|
6
|
-
gemspec.summary = "dnif is the new find... for sphinx"
|
7
|
-
gemspec.description = "dnif is a gem to index data using ActiveRecord finders, letting you index your custom methods and not only your table fields"
|
8
|
-
gemspec.email = "me@rafaelss.com"
|
9
|
-
gemspec.homepage = "http://github.com/rafaelss/dnif"
|
10
|
-
gemspec.authors = ["Rafael Souza"]
|
10
|
+
require "jeweler"
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
$LOAD_PATH.unshift('lib')
|
13
|
+
require "dnif"
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
Jeweler::Tasks.new do |gemspec|
|
16
|
+
gemspec.name = "dnif"
|
17
|
+
gemspec.version = Dnif::Version::STRING
|
18
|
+
gemspec.summary = "dnif is the new find... for sphinx"
|
19
|
+
gemspec.description = "dnif is a gem to index data using ActiveRecord finders, letting you index your custom methods and not only your table fields"
|
20
|
+
gemspec.email = "me@rafaelss.com"
|
21
|
+
gemspec.homepage = "http://github.com/rafaelss/dnif"
|
22
|
+
gemspec.authors = ["Rafael Souza"]
|
23
|
+
gemspec.has_rdoc = false
|
24
|
+
gemspec.files.include %w(.gitignore .rspec templates/config.erb)
|
21
25
|
end
|
22
26
|
|
27
|
+
Jeweler::GemcutterTasks.new
|
28
|
+
|
23
29
|
desc "Generate gemspec and build gem"
|
24
30
|
task :build_gem do
|
25
31
|
Rake::Task["gemspec"].invoke
|
26
32
|
Rake::Task["build"].invoke
|
27
33
|
end
|
28
34
|
|
29
|
-
|
30
|
-
|
31
|
-
require 'rake/testtask'
|
32
|
-
Rake::TestTask.new(:test) do |test|
|
33
|
-
test.test_files = FileList.new('test/**/test_*.rb') do |list|
|
34
|
-
list.exclude 'test/test_helper.rb'
|
35
|
-
end
|
36
|
-
test.libs << 'test'
|
37
|
-
test.verbose = true
|
38
|
-
end
|
35
|
+
require "rspec/core/rake_task"
|
36
|
+
RSpec::Core::RakeTask.new(:spec)
|
data/dnif.gemspec
CHANGED
@@ -5,68 +5,73 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{dnif}
|
8
|
-
s.version = "0.0.1.beta.
|
8
|
+
s.version = "0.0.1.beta.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Rafael Souza"]
|
12
|
-
s.date = %q{2010-07
|
12
|
+
s.date = %q{2010-09-07}
|
13
13
|
s.description = %q{dnif is a gem to index data using ActiveRecord finders, letting you index your custom methods and not only your table fields}
|
14
14
|
s.email = %q{me@rafaelss.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"README.rdoc"
|
17
17
|
]
|
18
18
|
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
".rspec",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
19
23
|
"README.rdoc",
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
24
|
+
"Rakefile",
|
25
|
+
"dnif.gemspec",
|
26
|
+
"lib/dnif.rb",
|
27
|
+
"lib/dnif/configuration.rb",
|
28
|
+
"lib/dnif/document.rb",
|
29
|
+
"lib/dnif/index.rb",
|
30
|
+
"lib/dnif/indexer.rb",
|
31
|
+
"lib/dnif/multi_attribute.rb",
|
32
|
+
"lib/dnif/schema.rb",
|
33
|
+
"lib/dnif/search.rb",
|
34
|
+
"lib/dnif/tasks.rb",
|
35
|
+
"lib/dnif/version.rb",
|
36
|
+
"spec/dnif/configuration_spec.rb",
|
37
|
+
"spec/dnif/dnif_spec.rb",
|
38
|
+
"spec/dnif/document_spec.rb",
|
39
|
+
"spec/dnif/index_spec.rb",
|
40
|
+
"spec/dnif/indexer_spec.rb",
|
41
|
+
"spec/dnif/multi_attribute_spec.rb",
|
42
|
+
"spec/dnif/schema_spec.rb",
|
43
|
+
"spec/dnif/search_spec.rb",
|
44
|
+
"spec/fixtures/db/schema.rb",
|
45
|
+
"spec/fixtures/log/searchd.pid",
|
46
|
+
"spec/fixtures/models.rb",
|
47
|
+
"spec/fixtures/sphinx_1.xml",
|
48
|
+
"spec/fixtures/sphinx_2.xml",
|
49
|
+
"spec/fixtures/sphinx_3.xml",
|
50
|
+
"spec/fixtures/sphinx_4.xml",
|
51
|
+
"spec/fixtures/sphinx_5.xml",
|
52
|
+
"spec/fixtures/templates/config.erb",
|
53
|
+
"spec/spec_helper.rb",
|
54
|
+
"spec/support/activerecord/models.rb",
|
55
|
+
"templates/config.erb"
|
51
56
|
]
|
57
|
+
s.has_rdoc = false
|
52
58
|
s.homepage = %q{http://github.com/rafaelss/dnif}
|
53
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
54
59
|
s.require_paths = ["lib"]
|
55
60
|
s.rubygems_version = %q{1.3.7}
|
56
61
|
s.summary = %q{dnif is the new find... for sphinx}
|
57
62
|
s.test_files = [
|
58
|
-
"
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
63
|
+
"spec/dnif/configuration_spec.rb",
|
64
|
+
"spec/dnif/dnif_spec.rb",
|
65
|
+
"spec/dnif/document_spec.rb",
|
66
|
+
"spec/dnif/index_spec.rb",
|
67
|
+
"spec/dnif/indexer_spec.rb",
|
68
|
+
"spec/dnif/multi_attribute_spec.rb",
|
69
|
+
"spec/dnif/schema_spec.rb",
|
70
|
+
"spec/dnif/search_spec.rb",
|
71
|
+
"spec/fixtures/db/schema.rb",
|
72
|
+
"spec/fixtures/models.rb",
|
73
|
+
"spec/spec_helper.rb",
|
74
|
+
"spec/support/activerecord/models.rb"
|
70
75
|
]
|
71
76
|
|
72
77
|
if s.respond_to? :specification_version then
|
@@ -74,18 +79,39 @@ Gem::Specification.new do |s|
|
|
74
79
|
s.specification_version = 3
|
75
80
|
|
76
81
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
77
|
-
s.add_runtime_dependency(%q<
|
78
|
-
s.add_runtime_dependency(%q<
|
79
|
-
s.add_runtime_dependency(%q<
|
82
|
+
s.add_runtime_dependency(%q<rake>, [">= 0"])
|
83
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
|
84
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
|
85
|
+
s.add_runtime_dependency(%q<riddle>, [">= 1.1.0"])
|
86
|
+
s.add_runtime_dependency(%q<builder>, [">= 2.1.2"])
|
87
|
+
s.add_development_dependency(%q<jeweler>, [">= 1.5.0.pre2"])
|
88
|
+
s.add_development_dependency(%q<rspec>, [">= 2.0.0.beta.20"])
|
89
|
+
s.add_development_dependency(%q<database_cleaner>, [">= 0"])
|
90
|
+
s.add_development_dependency(%q<awesome_print>, [">= 0"])
|
91
|
+
s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
|
80
92
|
else
|
81
|
-
s.add_dependency(%q<
|
82
|
-
s.add_dependency(%q<
|
83
|
-
s.add_dependency(%q<
|
93
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
94
|
+
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
95
|
+
s.add_dependency(%q<activesupport>, [">= 3.0.0"])
|
96
|
+
s.add_dependency(%q<riddle>, [">= 1.1.0"])
|
97
|
+
s.add_dependency(%q<builder>, [">= 2.1.2"])
|
98
|
+
s.add_dependency(%q<jeweler>, [">= 1.5.0.pre2"])
|
99
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0.beta.20"])
|
100
|
+
s.add_dependency(%q<database_cleaner>, [">= 0"])
|
101
|
+
s.add_dependency(%q<awesome_print>, [">= 0"])
|
102
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
84
103
|
end
|
85
104
|
else
|
86
|
-
s.add_dependency(%q<
|
87
|
-
s.add_dependency(%q<
|
88
|
-
s.add_dependency(%q<
|
105
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
106
|
+
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
107
|
+
s.add_dependency(%q<activesupport>, [">= 3.0.0"])
|
108
|
+
s.add_dependency(%q<riddle>, [">= 1.1.0"])
|
109
|
+
s.add_dependency(%q<builder>, [">= 2.1.2"])
|
110
|
+
s.add_dependency(%q<jeweler>, [">= 1.5.0.pre2"])
|
111
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0.beta.20"])
|
112
|
+
s.add_dependency(%q<database_cleaner>, [">= 0"])
|
113
|
+
s.add_dependency(%q<awesome_print>, [">= 0"])
|
114
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
89
115
|
end
|
90
116
|
end
|
91
117
|
|
data/lib/dnif.rb
CHANGED
@@ -1,55 +1,104 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
require "bundler"
|
3
|
+
Bundler.setup(:default)
|
2
4
|
|
3
|
-
require
|
4
|
-
require 'active_support'
|
5
|
-
require 'riddle'
|
6
|
-
|
7
|
-
require "dnif/configuration"
|
8
|
-
require "dnif/index_builder" # TODO remove
|
9
|
-
require "dnif/index"
|
10
|
-
require "dnif/schema"
|
11
|
-
require "dnif/document"
|
12
|
-
require "dnif/indexer"
|
13
|
-
require "dnif/multi_attribute"
|
14
|
-
require "dnif/search"
|
5
|
+
require "riddle"
|
15
6
|
|
16
7
|
module Dnif
|
8
|
+
extend self
|
9
|
+
|
10
|
+
autoload :Configuration, "dnif/configuration"
|
11
|
+
autoload :Index, "dnif/index"
|
12
|
+
autoload :Schema, "dnif/schema"
|
13
|
+
autoload :Document, "dnif/document"
|
14
|
+
autoload :Indexer, "dnif/indexer"
|
15
|
+
autoload :MultiAttribute, "dnif/multi_attribute"
|
16
|
+
autoload :Search, "dnif/search"
|
17
|
+
autoload :Version, "dnif/version"
|
17
18
|
|
18
|
-
def
|
19
|
+
def root_path
|
19
20
|
@root_path
|
20
21
|
end
|
21
22
|
|
22
|
-
def
|
23
|
+
def root_path=(value)
|
23
24
|
@root_path = value
|
24
25
|
end
|
25
26
|
|
26
|
-
def
|
27
|
+
def environment
|
27
28
|
@environment || 'development'
|
28
29
|
end
|
30
|
+
alias :env :environment
|
29
31
|
|
30
|
-
def
|
32
|
+
def environment=(value)
|
31
33
|
@environment = value
|
32
34
|
end
|
35
|
+
alias :env= :environment=
|
33
36
|
|
34
|
-
def
|
37
|
+
def models_path
|
35
38
|
@models_path
|
36
39
|
end
|
37
40
|
|
38
|
-
def
|
41
|
+
def models_path=(value)
|
39
42
|
@models_path = value
|
40
43
|
end
|
41
44
|
|
42
|
-
def
|
45
|
+
def load_models
|
43
46
|
models = Dir["#{self.models_path}/*.rb"]
|
44
47
|
models.map! do |filename|
|
45
48
|
filename = File.basename(filename, '.rb')
|
46
49
|
filename.classify.constantize
|
47
50
|
end
|
48
51
|
end
|
52
|
+
|
53
|
+
def search(query, options = {})
|
54
|
+
options.reverse_merge!(:index => '*')
|
55
|
+
|
56
|
+
if options[:classes]
|
57
|
+
ids = if options[:classes].is_a?(Array)
|
58
|
+
options[:classes].map { |name| ActiveRecord::Base.indexes.keys.index(name) }
|
59
|
+
else
|
60
|
+
[ActiveRecord::Base.indexes.keys.index(options[:classes])]
|
61
|
+
end
|
62
|
+
|
63
|
+
client.filters = [Riddle::Client::Filter.new("class_id", ids)]
|
64
|
+
end
|
65
|
+
|
66
|
+
results = client.query(query, options[:index])
|
67
|
+
raise results[:error] if results[:error]
|
68
|
+
|
69
|
+
models = results[:matches].inject({}) do |memo, match|
|
70
|
+
encoded_class_name = match[:attributes]["class_name"].split(',').flatten
|
71
|
+
class_name = Dnif::MultiAttribute.decode(encoded_class_name)
|
72
|
+
|
73
|
+
memo[class_name] ||= []
|
74
|
+
memo[class_name] << (match[:doc] - encoded_class_name.sum { |c| c.to_i })
|
75
|
+
memo
|
76
|
+
end
|
77
|
+
|
78
|
+
models.map do |class_name, ids|
|
79
|
+
class_name.constantize.find_all_by_id(ids)
|
80
|
+
end.flatten
|
81
|
+
end
|
82
|
+
|
83
|
+
def client
|
84
|
+
searchd = Dnif::Configuration.options_for("searchd", config_path)
|
85
|
+
if searchd["listen"]
|
86
|
+
address, port = searchd["listen"].split(":")
|
87
|
+
else
|
88
|
+
address = searchd["address"] || "127.0.0.1"
|
89
|
+
port = searchd["port"] || 3313
|
90
|
+
end
|
91
|
+
|
92
|
+
@client ||= Riddle::Client.new(address, port)
|
93
|
+
end
|
94
|
+
|
95
|
+
def config_path
|
96
|
+
Dnif.root_path ||= File.expand_path(File.dirname("."))
|
97
|
+
File.join(Dnif.root_path, "config/sphinx", Dnif.environment + ".erb")
|
98
|
+
end
|
49
99
|
end
|
50
100
|
|
51
|
-
if defined?(
|
52
|
-
Dnif
|
53
|
-
Dnif
|
54
|
-
|
55
|
-
end
|
101
|
+
if defined?(ActiveRecord::Base)
|
102
|
+
ActiveRecord::Base.extend(Dnif::Indexer)
|
103
|
+
ActiveRecord::Base.extend(Dnif::Search)
|
104
|
+
end
|
data/lib/dnif/configuration.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'fileutils'
|
2
|
-
require 'dnif'
|
3
2
|
|
4
3
|
module Dnif
|
5
4
|
|
@@ -11,9 +10,11 @@ module Dnif
|
|
11
10
|
|
12
11
|
# TODO turn "db/sphinx" and "config/sphinx" configurable
|
13
12
|
FileUtils.mkdir_p(File.join(Dnif.root_path, "db", "sphinx", Dnif.environment))
|
14
|
-
File.
|
13
|
+
path = File.join(Dnif.root_path, "config", "sphinx", Dnif.environment + ".conf")
|
14
|
+
File.open(path, "w") do |f|
|
15
15
|
f.puts output
|
16
16
|
end
|
17
|
+
path
|
17
18
|
end
|
18
19
|
|
19
20
|
def self.sources
|
@@ -38,7 +39,7 @@ module Dnif
|
|
38
39
|
section.gsub!(/^\s*(.*?)\s*(?:#.*)?$/, '\1')
|
39
40
|
|
40
41
|
# Convert to a hash
|
41
|
-
|
42
|
+
{}.tap do |options|
|
42
43
|
lines = section.split(/\n+/)
|
43
44
|
while line = lines.shift
|
44
45
|
if line =~ /(.*?)\s*=\s*(.*)/
|