dnif 0.0.1.beta.2 → 0.0.1.beta.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -38,6 +38,8 @@ dnif is a gem to index data using ActiveRecord finders (for now), letting you in
38
38
  - Improve test suite
39
39
  - Index in chunks
40
40
  - Support to delta index (or something like that)
41
+ - Remove the class_name (seems not be necessary anymore)
42
+ - Move this TODO to Issues
41
43
 
42
44
  == Maintainer
43
45
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dnif}
8
- s.version = "0.0.1.beta.2"
8
+ s.version = "0.0.1.beta.3"
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-14}
12
+ s.date = %q{2010-07-15}
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 = [
@@ -23,7 +23,8 @@ module Dnif
23
23
  end
24
24
  end
25
25
 
26
- xml.class_id(class_id)
26
+ xml.class_id(@object.indexes.keys.index(@object.class.name))
27
+ xml.class_name(encoded_class_name)
27
28
 
28
29
  attributes = []
29
30
  @object.indexes.values.each do |index|
@@ -53,12 +54,12 @@ module Dnif
53
54
 
54
55
  private
55
56
 
56
- def class_id
57
- @class_id ||= Dnif::MultiAttribute.encode(@object.class.name)
57
+ def encoded_class_name
58
+ @encoded_class_name ||= Dnif::MultiAttribute.encode(@object.class.name)
58
59
  end
59
60
 
60
61
  def document_id
61
- @object.id + class_id.split(',').sum { |c| c.to_i }
62
+ @object.id + encoded_class_name.split(',').sum { |c| c.to_i }
62
63
  end
63
64
  end
64
65
  end
@@ -1,9 +1,10 @@
1
1
  module Dnif
2
2
  module Indexer
3
3
 
4
+ @@indexes ||= {}
5
+
4
6
  def define_index(&block)
5
- @@indexes ||= {}
6
- @@indexes[self.name] = Dnif::Index.new(&block)
7
+ indexes[self.name] = Dnif::Index.new(&block)
7
8
 
8
9
  include InstanceMethods
9
10
  end
@@ -48,4 +49,4 @@ module Dnif
48
49
  end
49
50
  end
50
51
 
51
- ActiveRecord::Base.extend(Dnif::Indexer)
52
+ ActiveRecord::Base.extend(Dnif::Indexer) if defined?(ActiveRecord::Base)
@@ -8,16 +8,6 @@ module Dnif
8
8
  def generate
9
9
  xml = Builder::XmlMarkup.new(:indent => 2)
10
10
  xml.sphinx(:schema) do
11
- # fields = @klass.indexes[@klass.name].fields.map do |name|
12
- # xml.sphinx(:field, :name => name)
13
- # name
14
- # end
15
- #
16
- # attributes = @klass.indexes[@klass.name].attributes.map do |name, type|
17
- # xml.sphinx(:attr, :name => name, :type => attribute_type(type))
18
- # name
19
- # end
20
-
21
11
  fields = []
22
12
  @klass.indexes.values.each do |index|
23
13
  (index.fields - fields).each do |name|
@@ -26,7 +16,8 @@ module Dnif
26
16
  end
27
17
  end
28
18
 
29
- xml.sphinx(:attr, :name => "class_id", :type => "multi")
19
+ xml.sphinx(:attr, :name => "class_id", :type => "int")
20
+ xml.sphinx(:attr, :name => "class_name", :type => "multi")
30
21
 
31
22
  attributes = []
32
23
  @klass.indexes.values.each do |index|
@@ -3,20 +3,25 @@ module Dnif
3
3
  def self.search(query, options = {})
4
4
  options.reverse_merge!(:index => '*')
5
5
 
6
- if options[:class]
7
- filter_value = Dnif::MultiAttribute.encode(options[:class]).split(",").map(&:to_i)
8
- client.filters = [Riddle::Client::Filter.new("class_id", filter_value)]
6
+ if options[:classes]
7
+ ids = if options[:classes].is_a?(Array)
8
+ options[:classes].map { |name| ActiveRecord::Base.indexes.keys.index(name) }
9
+ else
10
+ [ActiveRecord::Base.indexes.keys.index(options[:classes])]
11
+ end
12
+
13
+ client.filters = [Riddle::Client::Filter.new("class_id", ids)]
9
14
  end
10
15
 
11
16
  results = client.query(query, options[:index])
12
17
  raise results[:error] if results[:error]
13
18
 
14
19
  models = results[:matches].inject({}) do |memo, match|
15
- class_id = match[:attributes]["class_id"].split(',').flatten
16
- class_name = Dnif::MultiAttribute.decode(class_id)
20
+ encoded_class_name = match[:attributes]["class_name"].split(',').flatten
21
+ class_name = Dnif::MultiAttribute.decode(encoded_class_name)
17
22
 
18
23
  memo[class_name] ||= []
19
- memo[class_name] << (match[:doc] - class_id.sum { |c| c.to_i })
24
+ memo[class_name] << (match[:doc] - encoded_class_name.sum { |c| c.to_i })
20
25
  memo
21
26
  end
22
27
 
@@ -45,9 +50,9 @@ module Dnif
45
50
  module Search
46
51
 
47
52
  def search(query)
48
- Dnif.search(query, :class => self.name)
53
+ Dnif.search(query, :classes => self.name)
49
54
  end
50
55
  end
51
56
  end
52
57
 
53
- ActiveRecord::Base.extend(Dnif::Search)
58
+ ActiveRecord::Base.extend(Dnif::Search) if defined?(ActiveRecord::Base)
@@ -5,7 +5,8 @@
5
5
  <sphinx:field name="full_name"/>
6
6
  <sphinx:field name="title"/>
7
7
  <sphinx:field name="buyer"/>
8
- <sphinx:attr name="class_id" type="multi"/>
8
+ <sphinx:attr name="class_id" type="int"/>
9
+ <sphinx:attr name="class_name" type="multi"/>
9
10
  <sphinx:attr name="active" type="bool"/>
10
11
  <sphinx:attr name="clicked" type="int"/>
11
12
  <sphinx:attr name="published_at" type="timestamp"/>
@@ -5,7 +5,8 @@
5
5
  <![CDATA[Note Title]]>
6
6
  </title>
7
7
  <buyer></buyer>
8
- <class_id>334,623,884,1125</class_id>
8
+ <class_id>3</class_id>
9
+ <class_name>334,623,884,1125</class_name>
9
10
  <active>true</active>
10
11
  <clicked>10</clicked>
11
12
  <published_at>{now}</published_at>
@@ -5,7 +5,8 @@
5
5
  </full_name>
6
6
  <title></title>
7
7
  <buyer></buyer>
8
- <class_id>336,613,882,1139,1391,1646</class_id>
8
+ <class_id>1</class_id>
9
+ <class_name>336,613,882,1139,1391,1646</class_name>
9
10
  <active></active>
10
11
  <clicked></clicked>
11
12
  <published_at></published_at>
@@ -3,7 +3,8 @@
3
3
  <sphinx:field name="full_name"/>
4
4
  <sphinx:field name="title"/>
5
5
  <sphinx:field name="buyer"/>
6
- <sphinx:attr name="class_id" type="multi"/>
6
+ <sphinx:attr name="class_id" type="int"/>
7
+ <sphinx:attr name="class_name" type="multi"/>
7
8
  <sphinx:attr name="active" type="bool"/>
8
9
  <sphinx:attr name="clicked" type="int"/>
9
10
  <sphinx:attr name="published_at" type="timestamp"/>
@@ -5,7 +5,8 @@
5
5
  <full_name></full_name>
6
6
  <title></title>
7
7
  <buyer></buyer>
8
- <class_id>1,2,3,4</class_id>
8
+ <class_id>0</class_id>
9
+ <class_name>1,2,3,4</class_name>
9
10
  <active>true</active>
10
11
  <clicked></clicked>
11
12
  <published_at></published_at>
@@ -5,6 +5,7 @@
5
5
  require "test/unit"
6
6
  require "mocha"
7
7
  require "active_record"
8
+ require "ap"
8
9
 
9
10
  require 'database_cleaner'
10
11
  DatabaseCleaner.strategy = :transaction
@@ -11,6 +11,23 @@ class TestIndexer < Test::Unit::TestCase
11
11
  DatabaseCleaner.clean
12
12
  end
13
13
 
14
+ test ".define_index" do
15
+ klass = Class.new
16
+ klass.extend(Dnif::Indexer)
17
+ klass.stubs(:name).returns("Klass")
18
+ klass.define_index do
19
+ field :a
20
+ attribute :b, :type => :integer
21
+ where "c"
22
+ end
23
+
24
+ assert_equal [:a], klass.indexes["Klass"].fields
25
+ assert_equal({ :b => :integer }, klass.indexes["Klass"].attributes)
26
+ assert_equal "c", klass.indexes["Klass"].conditions
27
+
28
+ klass.indexes.delete("Klass")
29
+ end
30
+
14
31
  test "objects without index should not have dnif included" do
15
32
  assert_false Post.new.respond_to?(:to_sphinx)
16
33
  assert_false Post.respond_to?(:to_sphinx)
@@ -40,12 +40,12 @@ class TestSearch < Test::Unit::TestCase
40
40
  :matches => [{
41
41
  :doc => 2983,
42
42
  :attributes => {
43
- "class_id" => "336,623,883,1140"
43
+ "class_name" => "336,623,883,1140"
44
44
  }
45
45
  }, {
46
46
  :doc => 7893,
47
47
  :attributes => {
48
- "class_id" => "323,623,877,1133,1381,1646,1908"
48
+ "class_name" => "323,623,877,1133,1381,1646,1908"
49
49
  }
50
50
  }]
51
51
  }
@@ -60,36 +60,57 @@ class TestSearch < Test::Unit::TestCase
60
60
  end
61
61
 
62
62
  test ".search through models" do
63
- results_for_post = {
63
+ riddle = mock("Riddle")
64
+ riddle.expects(:query).with("post", "*").returns(results_for_post)
65
+ riddle.expects(:query).with("comment", "*").returns(results_for_comment)
66
+ riddle.expects(:filters=).twice.returns([])
67
+ Dnif.expects(:client).times(4).returns(riddle)
68
+
69
+ ActiveRecord::Base.expects(:indexes).twice.returns({ "Post" => mock, "Comment" => mock })
70
+
71
+ Riddle::Client::Filter.expects(:new).with("class_id", [0])
72
+ Riddle::Client::Filter.expects(:new).with("class_id", [1])
73
+
74
+ Post.expects(:find_all_by_id).once.with([1])
75
+ Comment.expects(:find_all_by_id).once.with([2])
76
+
77
+ Post.search("post")
78
+ Comment.search("comment")
79
+ end
80
+
81
+ test "search only in specified models" do
82
+ riddle = mock("Riddle")
83
+ riddle.expects(:query).with("post", "*").returns(results_for_post)
84
+ riddle.expects(:filters=).returns([])
85
+ Dnif.expects(:client).times(2).returns(riddle)
86
+
87
+ ActiveRecord::Base.expects(:indexes).returns({ "Post" => mock })
88
+
89
+ Riddle::Client::Filter.expects(:new).with("class_id", [0])
90
+ Post.expects(:find_all_by_id).once.with([1])
91
+
92
+ Dnif.search("post", :classes => "Post")
93
+ end
94
+
95
+ def results_for_post
96
+ {
64
97
  :matches => [{
65
98
  :doc => 2983,
66
99
  :attributes => {
67
- "class_id" => "336,623,883,1140"
100
+ "class_name" => "336,623,883,1140"
68
101
  }
69
102
  }]
70
103
  }
71
- results_for_comment = {
104
+ end
105
+
106
+ def results_for_comment
107
+ {
72
108
  :matches => [{
73
109
  :doc => 7893,
74
110
  :attributes => {
75
- "class_id" => "323,623,877,1133,1381,1646,1908"
111
+ "class_name" => "323,623,877,1133,1381,1646,1908"
76
112
  }
77
113
  }]
78
114
  }
79
-
80
- riddle = mock("Riddle")
81
- riddle.expects(:query).with("post", "*").returns(results_for_post)
82
- riddle.expects(:query).with("comment", "*").returns(results_for_comment)
83
- riddle.expects(:filters).twice.returns([])
84
- Dnif.expects(:client).times(4).returns(riddle)
85
-
86
- Riddle::Client::Filter.expects(:new).with("class_id", [336, 623, 883, 1140])
87
- Riddle::Client::Filter.expects(:new).with("class_id", [323, 623, 877, 1133, 1381, 1646, 1908])
88
-
89
- Post.expects(:find_all_by_id).once.with([1])
90
- Comment.expects(:find_all_by_id).once.with([2])
91
-
92
- Post.search("post")
93
- Comment.search("comment")
94
115
  end
95
116
  end
metadata CHANGED
@@ -7,8 +7,8 @@ version: !ruby/object:Gem::Version
7
7
  - 0
8
8
  - 1
9
9
  - beta
10
- - 2
11
- version: 0.0.1.beta.2
10
+ - 3
11
+ version: 0.0.1.beta.3
12
12
  platform: ruby
13
13
  authors:
14
14
  - Rafael Souza
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-07-14 00:00:00 -03:00
19
+ date: 2010-07-15 00:00:00 -03:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency