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.
Files changed (44) hide show
  1. data/.gitignore +4 -0
  2. data/.rspec +2 -0
  3. data/Gemfile +15 -0
  4. data/Gemfile.lock +52 -0
  5. data/Rakefile +24 -26
  6. data/dnif.gemspec +81 -55
  7. data/lib/dnif.rb +73 -24
  8. data/lib/dnif/configuration.rb +4 -3
  9. data/lib/dnif/document.rb +8 -8
  10. data/lib/dnif/indexer.rb +0 -2
  11. data/lib/dnif/schema.rb +1 -1
  12. data/lib/dnif/search.rb +1 -50
  13. data/lib/dnif/tasks.rb +11 -11
  14. data/lib/dnif/version.rb +10 -0
  15. data/spec/dnif/configuration_spec.rb +41 -0
  16. data/spec/dnif/dnif_spec.rb +157 -0
  17. data/spec/dnif/document_spec.rb +46 -0
  18. data/spec/dnif/index_spec.rb +20 -0
  19. data/spec/dnif/indexer_spec.rb +64 -0
  20. data/spec/dnif/multi_attribute_spec.rb +20 -0
  21. data/{test/unit/test_schema.rb → spec/dnif/schema_spec.rb} +5 -5
  22. data/spec/dnif/search_spec.rb +18 -0
  23. data/{test → spec}/fixtures/db/schema.rb +1 -0
  24. data/{test → spec}/fixtures/log/searchd.pid +0 -0
  25. data/{test → spec}/fixtures/models.rb +0 -0
  26. data/{test → spec}/fixtures/sphinx_1.xml +1 -0
  27. data/{test → spec}/fixtures/sphinx_2.xml +1 -0
  28. data/{test → spec}/fixtures/sphinx_3.xml +1 -0
  29. data/{test → spec}/fixtures/sphinx_4.xml +1 -0
  30. data/{test → spec}/fixtures/sphinx_5.xml +1 -0
  31. data/{test → spec}/fixtures/templates/config.erb +0 -0
  32. data/spec/spec_helper.rb +31 -0
  33. data/{test/test_helper.rb → spec/support/activerecord/models.rb} +2 -21
  34. data/templates/config.erb +38 -0
  35. metadata +158 -46
  36. data/lib/dnif/index_builder.rb +0 -30
  37. data/test/unit/test_configuration.rb +0 -37
  38. data/test/unit/test_dnif.rb +0 -21
  39. data/test/unit/test_document.rb +0 -41
  40. data/test/unit/test_index.rb +0 -21
  41. data/test/unit/test_index_builder.rb +0 -33
  42. data/test/unit/test_indexer.rb +0 -60
  43. data/test/unit/test_multi_attribute.rb +0 -17
  44. data/test/unit/test_search.rb +0 -116
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+ *.gem
3
+ pkg
4
+ .bundle/config
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format documentation
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
@@ -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
- require 'jeweler'
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
- Jeweler::Tasks.new do |gemspec|
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
- gemspec.has_rdoc = false
13
- gemspec.files = %w(Rakefile dnif.gemspec README.rdoc) + Dir["{lib,test}/**/*"]
12
+ $LOAD_PATH.unshift('lib')
13
+ require "dnif"
14
14
 
15
- gemspec.add_dependency "activerecord"
16
- gemspec.add_dependency "activesupport"
17
- gemspec.add_dependency "riddle"
18
- end
19
- rescue LoadError
20
- puts "Jeweler not available. Install it with: gem install jeweler"
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
- Jeweler::GemcutterTasks.new
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)
@@ -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.3"
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-15}
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
- "Rakefile",
21
- "dnif.gemspec",
22
- "lib/dnif.rb",
23
- "lib/dnif/configuration.rb",
24
- "lib/dnif/document.rb",
25
- "lib/dnif/index.rb",
26
- "lib/dnif/index_builder.rb",
27
- "lib/dnif/indexer.rb",
28
- "lib/dnif/multi_attribute.rb",
29
- "lib/dnif/schema.rb",
30
- "lib/dnif/search.rb",
31
- "lib/dnif/tasks.rb",
32
- "test/fixtures/db/schema.rb",
33
- "test/fixtures/log/searchd.pid",
34
- "test/fixtures/models.rb",
35
- "test/fixtures/sphinx_1.xml",
36
- "test/fixtures/sphinx_2.xml",
37
- "test/fixtures/sphinx_3.xml",
38
- "test/fixtures/sphinx_4.xml",
39
- "test/fixtures/sphinx_5.xml",
40
- "test/fixtures/templates/config.erb",
41
- "test/test_helper.rb",
42
- "test/unit/test_configuration.rb",
43
- "test/unit/test_dnif.rb",
44
- "test/unit/test_document.rb",
45
- "test/unit/test_index.rb",
46
- "test/unit/test_index_builder.rb",
47
- "test/unit/test_indexer.rb",
48
- "test/unit/test_multi_attribute.rb",
49
- "test/unit/test_schema.rb",
50
- "test/unit/test_search.rb"
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
- "test/fixtures/db/schema.rb",
59
- "test/fixtures/models.rb",
60
- "test/test_helper.rb",
61
- "test/unit/test_configuration.rb",
62
- "test/unit/test_dnif.rb",
63
- "test/unit/test_document.rb",
64
- "test/unit/test_index.rb",
65
- "test/unit/test_index_builder.rb",
66
- "test/unit/test_indexer.rb",
67
- "test/unit/test_multi_attribute.rb",
68
- "test/unit/test_schema.rb",
69
- "test/unit/test_search.rb"
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<activerecord>, [">= 0"])
78
- s.add_runtime_dependency(%q<activesupport>, [">= 0"])
79
- s.add_runtime_dependency(%q<riddle>, [">= 0"])
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<activerecord>, [">= 0"])
82
- s.add_dependency(%q<activesupport>, [">= 0"])
83
- s.add_dependency(%q<riddle>, [">= 0"])
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<activerecord>, [">= 0"])
87
- s.add_dependency(%q<activesupport>, [">= 0"])
88
- s.add_dependency(%q<riddle>, [">= 0"])
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
 
@@ -1,55 +1,104 @@
1
1
  # encoding: utf-8
2
+ require "bundler"
3
+ Bundler.setup(:default)
2
4
 
3
- require 'active_record'
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 self.root_path
19
+ def root_path
19
20
  @root_path
20
21
  end
21
22
 
22
- def self.root_path=(value)
23
+ def root_path=(value)
23
24
  @root_path = value
24
25
  end
25
26
 
26
- def self.environment
27
+ def environment
27
28
  @environment || 'development'
28
29
  end
30
+ alias :env :environment
29
31
 
30
- def self.environment=(value)
32
+ def environment=(value)
31
33
  @environment = value
32
34
  end
35
+ alias :env= :environment=
33
36
 
34
- def self.models_path
37
+ def models_path
35
38
  @models_path
36
39
  end
37
40
 
38
- def self.models_path=(value)
41
+ def models_path=(value)
39
42
  @models_path = value
40
43
  end
41
44
 
42
- def self.load_models
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?(Rails)
52
- Dnif.root_path = RAILS_ROOT
53
- Dnif.environment = RAILS_ENV
54
- Dnif.models_path = File.join(RAILS_ROOT, "app", "models")
55
- end
101
+ if defined?(ActiveRecord::Base)
102
+ ActiveRecord::Base.extend(Dnif::Indexer)
103
+ ActiveRecord::Base.extend(Dnif::Search)
104
+ end
@@ -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.open(Dnif.root_path + "/config/sphinx/" + Dnif.environment + ".conf", "w") do |f|
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
- returning({}) do |options|
42
+ {}.tap do |options|
42
43
  lines = section.split(/\n+/)
43
44
  while line = lines.shift
44
45
  if line =~ /(.*?)\s*=\s*(.*)/