dnif 0.0.1.alpha.7 → 0.0.1.beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/dnif.gemspec +16 -2
- data/lib/dnif.rb +4 -1
- data/lib/dnif/configuration.rb +1 -2
- data/lib/dnif/document.rb +64 -0
- data/lib/dnif/index.rb +29 -0
- data/lib/dnif/indexer.rb +23 -64
- data/lib/dnif/schema.rb +59 -0
- data/test/fixtures/db/schema.rb +5 -3
- data/test/fixtures/sphinx_1.xml +15 -0
- data/test/fixtures/sphinx_2.xml +14 -0
- data/test/fixtures/sphinx_3.xml +14 -0
- data/test/fixtures/sphinx_4.xml +12 -0
- data/test/fixtures/sphinx_5.xml +14 -0
- data/test/test_helper.rb +6 -0
- data/test/unit/test_document.rb +41 -0
- data/test/unit/test_index.rb +21 -0
- data/test/unit/test_indexer.rb +16 -8
- data/test/unit/test_schema.rb +11 -0
- metadata +18 -4
data/dnif.gemspec
CHANGED
@@ -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.
|
8
|
+
s.version = "0.0.1.beta.1"
|
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-07-14}
|
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 = [
|
@@ -21,21 +21,32 @@ Gem::Specification.new do |s|
|
|
21
21
|
"dnif.gemspec",
|
22
22
|
"lib/dnif.rb",
|
23
23
|
"lib/dnif/configuration.rb",
|
24
|
+
"lib/dnif/document.rb",
|
25
|
+
"lib/dnif/index.rb",
|
24
26
|
"lib/dnif/index_builder.rb",
|
25
27
|
"lib/dnif/indexer.rb",
|
26
28
|
"lib/dnif/multi_attribute.rb",
|
29
|
+
"lib/dnif/schema.rb",
|
27
30
|
"lib/dnif/search.rb",
|
28
31
|
"lib/dnif/tasks.rb",
|
29
32
|
"test/fixtures/db/schema.rb",
|
30
33
|
"test/fixtures/log/searchd.pid",
|
31
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",
|
32
40
|
"test/fixtures/templates/config.erb",
|
33
41
|
"test/test_helper.rb",
|
34
42
|
"test/unit/test_configuration.rb",
|
35
43
|
"test/unit/test_dnif.rb",
|
44
|
+
"test/unit/test_document.rb",
|
45
|
+
"test/unit/test_index.rb",
|
36
46
|
"test/unit/test_index_builder.rb",
|
37
47
|
"test/unit/test_indexer.rb",
|
38
48
|
"test/unit/test_multi_attribute.rb",
|
49
|
+
"test/unit/test_schema.rb",
|
39
50
|
"test/unit/test_search.rb"
|
40
51
|
]
|
41
52
|
s.homepage = %q{http://github.com/rafaelss/dnif}
|
@@ -49,9 +60,12 @@ Gem::Specification.new do |s|
|
|
49
60
|
"test/test_helper.rb",
|
50
61
|
"test/unit/test_configuration.rb",
|
51
62
|
"test/unit/test_dnif.rb",
|
63
|
+
"test/unit/test_document.rb",
|
64
|
+
"test/unit/test_index.rb",
|
52
65
|
"test/unit/test_index_builder.rb",
|
53
66
|
"test/unit/test_indexer.rb",
|
54
67
|
"test/unit/test_multi_attribute.rb",
|
68
|
+
"test/unit/test_schema.rb",
|
55
69
|
"test/unit/test_search.rb"
|
56
70
|
]
|
57
71
|
|
data/lib/dnif.rb
CHANGED
@@ -5,7 +5,10 @@ require 'active_support'
|
|
5
5
|
require 'riddle'
|
6
6
|
|
7
7
|
require "dnif/configuration"
|
8
|
-
require "dnif/index_builder"
|
8
|
+
require "dnif/index_builder" # TODO remove
|
9
|
+
require "dnif/index"
|
10
|
+
require "dnif/schema"
|
11
|
+
require "dnif/document"
|
9
12
|
require "dnif/indexer"
|
10
13
|
require "dnif/multi_attribute"
|
11
14
|
require "dnif/search"
|
data/lib/dnif/configuration.rb
CHANGED
@@ -17,8 +17,7 @@ module Dnif
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def self.sources
|
20
|
-
|
21
|
-
classes.each do |class_name|
|
20
|
+
ActiveRecord::Base.indexes.keys.each do |class_name|
|
22
21
|
name = class_name.underscore.pluralize + "_main"
|
23
22
|
yield name, class_name
|
24
23
|
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Dnif
|
2
|
+
class Document
|
3
|
+
|
4
|
+
def initialize(object)
|
5
|
+
@object = object
|
6
|
+
end
|
7
|
+
|
8
|
+
def generate
|
9
|
+
xml = Builder::XmlMarkup.new(:indent => 2)
|
10
|
+
xml.sphinx(:document, :id => document_id) do
|
11
|
+
fields = []
|
12
|
+
@object.indexes.values.each do |index|
|
13
|
+
(index.fields - fields).each do |name|
|
14
|
+
if @object.index.fields.include?(name)
|
15
|
+
xml.tag!(name) do
|
16
|
+
xml.cdata!(@object.send(name))
|
17
|
+
end
|
18
|
+
else
|
19
|
+
xml.tag!(name, "")
|
20
|
+
end
|
21
|
+
|
22
|
+
fields << name
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
xml.class_id(class_id)
|
27
|
+
|
28
|
+
attributes = []
|
29
|
+
@object.indexes.values.each do |index|
|
30
|
+
index.attributes.each do |name, type|
|
31
|
+
next if attributes.include?(name)
|
32
|
+
|
33
|
+
if @object.index.attributes.has_key?(name)
|
34
|
+
value = @object.send(name)
|
35
|
+
|
36
|
+
if [:date, :datetime].include?(type)
|
37
|
+
if value.is_a?(Date)
|
38
|
+
value = value.to_datetime
|
39
|
+
end
|
40
|
+
value = value.to_i
|
41
|
+
end
|
42
|
+
else
|
43
|
+
value = ""
|
44
|
+
end
|
45
|
+
|
46
|
+
xml.tag!(name, value)
|
47
|
+
attributes << name
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
xml.target!
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def class_id
|
57
|
+
@class_id ||= Dnif::MultiAttribute.encode(@object.class.name)
|
58
|
+
end
|
59
|
+
|
60
|
+
def document_id
|
61
|
+
@object.id + class_id.split(',').sum { |c| c.to_i }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/lib/dnif/index.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Dnif
|
2
|
+
class Index
|
3
|
+
|
4
|
+
attr_reader :fields
|
5
|
+
attr_reader :attributes
|
6
|
+
attr_reader :conditions
|
7
|
+
|
8
|
+
def initialize(&block)
|
9
|
+
@fields = []
|
10
|
+
@attributes = {}
|
11
|
+
|
12
|
+
self.instance_eval(&block)
|
13
|
+
end
|
14
|
+
|
15
|
+
def field(name)
|
16
|
+
@fields << name
|
17
|
+
end
|
18
|
+
|
19
|
+
def attribute(name, options)
|
20
|
+
raise "You must specify the attribute type (:integer, :datetime, :date, :boolean, :float)" if options[:type].nil?
|
21
|
+
|
22
|
+
@attributes[name] = options[:type]
|
23
|
+
end
|
24
|
+
|
25
|
+
def where(conditions)
|
26
|
+
@conditions = conditions
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/dnif/indexer.rb
CHANGED
@@ -2,88 +2,47 @@ module Dnif
|
|
2
2
|
module Indexer
|
3
3
|
|
4
4
|
def define_index(&block)
|
5
|
-
|
6
|
-
|
5
|
+
@@indexes ||= {}
|
6
|
+
@@indexes[self.name] = Dnif::Index.new(&block)
|
7
7
|
|
8
8
|
include InstanceMethods
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
@@
|
11
|
+
def indexes
|
12
|
+
@@indexes
|
13
13
|
end
|
14
14
|
|
15
15
|
def to_sphinx
|
16
|
-
return nil if
|
16
|
+
return nil if indexes.blank?
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
xml <<
|
23
|
-
|
24
|
-
xml << "<sphinx:schema>\n"
|
25
|
-
builder.fields.each do |name|
|
26
|
-
xml << " <sphinx:field name=\"#{name}\"/>\n"
|
27
|
-
end
|
28
|
-
|
29
|
-
xml << " <sphinx:attr name=\"class_id\" type=\"multi\"/>\n"
|
30
|
-
builder.attributes.each do |name, type|
|
31
|
-
xml << " <sphinx:attr name=\"#{name}\" "
|
32
|
-
|
33
|
-
case type
|
34
|
-
when :integer
|
35
|
-
xml << "type=\"int\""
|
36
|
-
when :date, :datetime
|
37
|
-
xml << "type=\"timestamp\""
|
38
|
-
when :boolean
|
39
|
-
xml << "type=\"bool\""
|
40
|
-
when :float
|
41
|
-
xml << "type=\"float\""
|
42
|
-
end
|
43
|
-
|
44
|
-
xml << "/>\n"
|
45
|
-
end
|
46
|
-
|
47
|
-
xml << "</sphinx:schema>\n"
|
18
|
+
xml = Builder::XmlMarkup.new(:indent => 2)
|
19
|
+
xml.instruct!
|
20
|
+
xml.sphinx(:docset) do
|
21
|
+
schema = Schema.new(self)
|
22
|
+
xml << schema.generate
|
48
23
|
|
24
|
+
results = all(:conditions => indexes[self.name].conditions)
|
49
25
|
results.each do |object|
|
50
|
-
|
26
|
+
document = Document.new(object)
|
27
|
+
xml << document.generate
|
51
28
|
end
|
52
|
-
xml << "</sphinx:docset>"
|
53
29
|
end
|
30
|
+
xml.target!
|
54
31
|
end
|
55
32
|
|
56
33
|
module InstanceMethods
|
57
34
|
|
58
|
-
def
|
59
|
-
|
60
|
-
|
61
|
-
class_id = Dnif::MultiAttribute.encode(self.class.name)
|
62
|
-
sphinx_id = id + class_id.split(',').sum { |c| c.to_i }
|
63
|
-
xml = "<sphinx:document id=\"#{sphinx_id}\">\n"
|
64
|
-
|
65
|
-
builder.fields.each do |field|
|
66
|
-
xml << " <#{field}><![CDATA[[#{send(field)}]]></#{field}>\n"
|
67
|
-
end
|
68
|
-
|
69
|
-
xml << " <class_id>#{class_id}</class_id>\n"
|
70
|
-
|
71
|
-
builder.attributes.each do |name, type|
|
72
|
-
value = send(name)
|
73
|
-
|
74
|
-
if [:date, :datetime].include?(builder.attributes[name])
|
75
|
-
if value.is_a?(Date)
|
76
|
-
value = value.to_datetime
|
77
|
-
end
|
78
|
-
|
79
|
-
value = value.to_i
|
80
|
-
end
|
35
|
+
def indexes
|
36
|
+
self.class.indexes
|
37
|
+
end
|
81
38
|
|
82
|
-
|
83
|
-
|
39
|
+
def index
|
40
|
+
self.class.indexes[self.class.name]
|
41
|
+
end
|
84
42
|
|
85
|
-
|
86
|
-
|
43
|
+
def to_sphinx
|
44
|
+
document = Document.new(self)
|
45
|
+
document.generate
|
87
46
|
end
|
88
47
|
end
|
89
48
|
end
|
data/lib/dnif/schema.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
module Dnif
|
2
|
+
class Schema
|
3
|
+
|
4
|
+
def initialize(klass)
|
5
|
+
@klass = klass
|
6
|
+
end
|
7
|
+
|
8
|
+
def generate
|
9
|
+
xml = Builder::XmlMarkup.new(:indent => 2)
|
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
|
+
fields = []
|
22
|
+
@klass.indexes.values.each do |index|
|
23
|
+
(index.fields - fields).each do |name|
|
24
|
+
xml.sphinx(:field, :name => name)
|
25
|
+
fields << name
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
xml.sphinx(:attr, :name => "class_id", :type => "multi")
|
30
|
+
|
31
|
+
attributes = []
|
32
|
+
@klass.indexes.values.each do |index|
|
33
|
+
index.attributes.each do |name, type|
|
34
|
+
if not attributes.include?(name)
|
35
|
+
xml.sphinx(:attr, :name => name, :type => attribute_type(type))
|
36
|
+
attributes << name
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
xml.target!
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def attribute_type(type)
|
47
|
+
case type
|
48
|
+
when :integer
|
49
|
+
"int"
|
50
|
+
when :date, :datetime
|
51
|
+
"timestamp"
|
52
|
+
when :boolean
|
53
|
+
"bool"
|
54
|
+
when :float
|
55
|
+
"float"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/test/fixtures/db/schema.rb
CHANGED
@@ -3,9 +3,10 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
3
3
|
create_table "comments", :force => true do |t|
|
4
4
|
t.string :author
|
5
5
|
end
|
6
|
-
|
6
|
+
|
7
7
|
create_table "users", :force => true do |t|
|
8
8
|
t.string :name
|
9
|
+
t.boolean :active
|
9
10
|
end
|
10
11
|
|
11
12
|
create_table "people", :force => true do |t|
|
@@ -14,16 +15,17 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
14
15
|
end
|
15
16
|
|
16
17
|
create_table "posts", :force => true do |t|
|
17
|
-
t.string :title
|
18
18
|
t.datetime :published_at
|
19
19
|
t.boolean :draft, :default => true
|
20
20
|
end
|
21
21
|
|
22
|
-
create_table "
|
22
|
+
create_table "orders", :force => true do |t|
|
23
|
+
t.string :title
|
23
24
|
t.datetime :ordered_at
|
24
25
|
end
|
25
26
|
|
26
27
|
create_table "notes", :force => true do |t|
|
28
|
+
t.string :title
|
27
29
|
t.integer :clicked
|
28
30
|
t.datetime :published_at
|
29
31
|
t.date :expire_at
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<sphinx:docset>
|
3
|
+
<sphinx:schema>
|
4
|
+
<sphinx:field name="name"/>
|
5
|
+
<sphinx:field name="full_name"/>
|
6
|
+
<sphinx:field name="title"/>
|
7
|
+
<sphinx:field name="buyer"/>
|
8
|
+
<sphinx:attr name="class_id" type="multi"/>
|
9
|
+
<sphinx:attr name="active" type="bool"/>
|
10
|
+
<sphinx:attr name="clicked" type="int"/>
|
11
|
+
<sphinx:attr name="published_at" type="timestamp"/>
|
12
|
+
<sphinx:attr name="expire_at" type="timestamp"/>
|
13
|
+
<sphinx:attr name="points" type="float"/>
|
14
|
+
</sphinx:schema>
|
15
|
+
{comment}</sphinx:docset>
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<sphinx:document id="2967">
|
2
|
+
<name></name>
|
3
|
+
<full_name></full_name>
|
4
|
+
<title>
|
5
|
+
<![CDATA[Note Title]]>
|
6
|
+
</title>
|
7
|
+
<buyer></buyer>
|
8
|
+
<class_id>334,623,884,1125</class_id>
|
9
|
+
<active>true</active>
|
10
|
+
<clicked>10</clicked>
|
11
|
+
<published_at>{now}</published_at>
|
12
|
+
<expire_at>{expire}</expire_at>
|
13
|
+
<points>1000.0</points>
|
14
|
+
</sphinx:document>
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<sphinx:document id="6008">
|
2
|
+
<name></name>
|
3
|
+
<full_name>
|
4
|
+
<![CDATA[Rafael Souza]]>
|
5
|
+
</full_name>
|
6
|
+
<title></title>
|
7
|
+
<buyer></buyer>
|
8
|
+
<class_id>336,613,882,1139,1391,1646</class_id>
|
9
|
+
<active></active>
|
10
|
+
<clicked></clicked>
|
11
|
+
<published_at></published_at>
|
12
|
+
<expire_at></expire_at>
|
13
|
+
<points></points>
|
14
|
+
</sphinx:document>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<sphinx:schema>
|
2
|
+
<sphinx:field name="name"/>
|
3
|
+
<sphinx:field name="full_name"/>
|
4
|
+
<sphinx:field name="title"/>
|
5
|
+
<sphinx:field name="buyer"/>
|
6
|
+
<sphinx:attr name="class_id" type="multi"/>
|
7
|
+
<sphinx:attr name="active" type="bool"/>
|
8
|
+
<sphinx:attr name="clicked" type="int"/>
|
9
|
+
<sphinx:attr name="published_at" type="timestamp"/>
|
10
|
+
<sphinx:attr name="expire_at" type="timestamp"/>
|
11
|
+
<sphinx:attr name="points" type="float"/>
|
12
|
+
</sphinx:schema>
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<sphinx:document id="11">
|
2
|
+
<name>
|
3
|
+
<![CDATA[Rafael Souza]]>
|
4
|
+
</name>
|
5
|
+
<full_name></full_name>
|
6
|
+
<title></title>
|
7
|
+
<buyer></buyer>
|
8
|
+
<class_id>1,2,3,4</class_id>
|
9
|
+
<active>true</active>
|
10
|
+
<clicked></clicked>
|
11
|
+
<published_at></published_at>
|
12
|
+
<expire_at></expire_at>
|
13
|
+
<points></points>
|
14
|
+
</sphinx:document>
|
data/test/test_helper.rb
CHANGED
@@ -6,6 +6,9 @@ require "test/unit"
|
|
6
6
|
require "mocha"
|
7
7
|
require "active_record"
|
8
8
|
|
9
|
+
require 'database_cleaner'
|
10
|
+
DatabaseCleaner.strategy = :transaction
|
11
|
+
|
9
12
|
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
10
13
|
require "dnif"
|
11
14
|
|
@@ -28,6 +31,7 @@ class User < ActiveRecord::Base
|
|
28
31
|
|
29
32
|
define_index do
|
30
33
|
field :name
|
34
|
+
attribute :active, :type => :boolean
|
31
35
|
end
|
32
36
|
end
|
33
37
|
|
@@ -51,6 +55,7 @@ end
|
|
51
55
|
class Order < ActiveRecord::Base
|
52
56
|
|
53
57
|
define_index do
|
58
|
+
field :title
|
54
59
|
field :buyer
|
55
60
|
|
56
61
|
where ["ordered_at >= ?", 2.months.ago]
|
@@ -60,6 +65,7 @@ end
|
|
60
65
|
class Note < ActiveRecord::Base
|
61
66
|
|
62
67
|
define_index do
|
68
|
+
field :title
|
63
69
|
attribute :clicked, :type => :integer
|
64
70
|
attribute :published_at, :type => :datetime
|
65
71
|
attribute :expire_at, :type => :date
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class TestDocument < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
DatabaseCleaner.start
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
DatabaseCleaner.clean
|
12
|
+
end
|
13
|
+
|
14
|
+
test ".generate" do
|
15
|
+
Dnif::MultiAttribute.expects(:encode).with("User").returns("1,2,3,4")
|
16
|
+
|
17
|
+
user = User.create!(:name => "Rafael Souza", :active => true)
|
18
|
+
|
19
|
+
document = Dnif::Document.new(user)
|
20
|
+
expected = File.read(File.dirname(__FILE__) + "/../fixtures/sphinx_5.xml")
|
21
|
+
assert_equal expected, document.generate
|
22
|
+
end
|
23
|
+
|
24
|
+
test "convertion of date/datetime values to timestamp" do
|
25
|
+
now = DateTime.now
|
26
|
+
now.expects(:to_i)
|
27
|
+
expire = Date.today + 2.days
|
28
|
+
expire.expects(:to_i)
|
29
|
+
expire.expects(:to_datetime).returns(expire)
|
30
|
+
|
31
|
+
note = Note.create!(
|
32
|
+
:title => "Note Title",
|
33
|
+
:clicked => 10,
|
34
|
+
:published_at => now,
|
35
|
+
:expire_at => expire,
|
36
|
+
:active => true,
|
37
|
+
:points => 1000
|
38
|
+
)
|
39
|
+
note.to_sphinx
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class TestIndex < Test::Unit::TestCase
|
5
|
+
|
6
|
+
test "field definition" do
|
7
|
+
index = Dnif::Index.new(&proc { field :name })
|
8
|
+
assert_equal [:name], index.fields
|
9
|
+
end
|
10
|
+
|
11
|
+
test "attribute definition" do
|
12
|
+
index = Dnif::Index.new(&proc { attribute :another, :type => :bool })
|
13
|
+
expected = { :another => :bool }
|
14
|
+
assert_equal expected, index.attributes
|
15
|
+
end
|
16
|
+
|
17
|
+
test "where definition" do
|
18
|
+
index = Dnif::Index.new(&proc { where "field = 'value'" })
|
19
|
+
assert_equal "field = 'value'", index.conditions
|
20
|
+
end
|
21
|
+
end
|
data/test/unit/test_indexer.rb
CHANGED
@@ -3,6 +3,14 @@ require 'test_helper'
|
|
3
3
|
|
4
4
|
class TestIndexer < Test::Unit::TestCase
|
5
5
|
|
6
|
+
def setup
|
7
|
+
DatabaseCleaner.start
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
DatabaseCleaner.clean
|
12
|
+
end
|
13
|
+
|
6
14
|
test "objects without index should not have dnif included" do
|
7
15
|
assert_false Post.new.respond_to?(:to_sphinx)
|
8
16
|
assert_false Post.respond_to?(:to_sphinx)
|
@@ -11,25 +19,25 @@ class TestIndexer < Test::Unit::TestCase
|
|
11
19
|
test "to_sphinx returns a string with sphinx document" do
|
12
20
|
comment = Person.create!(:first_name => "Rafael", :last_name => "Souza")
|
13
21
|
|
14
|
-
expected =
|
22
|
+
expected = File.read(File.dirname(__FILE__) + "/../fixtures/sphinx_3.xml")
|
15
23
|
assert_equal expected, comment.to_sphinx
|
16
24
|
end
|
17
25
|
|
18
26
|
test "attributes" do
|
19
|
-
note = Note.create!(:clicked => 10, :published_at => (now = DateTime.now), :expire_at => (expire = Date.today + 2.days), :active => true, :points => 1000)
|
27
|
+
note = Note.create!(:title => "Note Title", :clicked => 10, :published_at => (now = DateTime.now), :expire_at => (expire = Date.today + 2.days), :active => true, :points => 1000)
|
20
28
|
|
21
|
-
expected =
|
22
|
-
assert_equal expected, note.to_sphinx
|
29
|
+
expected = File.read(File.dirname(__FILE__) + "/../fixtures/sphinx_2.xml")
|
30
|
+
assert_equal expected.gsub("{now}", now.to_i.to_s).gsub("{expire}", expire.to_datetime.to_i.to_s), note.to_sphinx
|
23
31
|
end
|
24
32
|
|
25
33
|
test ".to_sphinx should generate a full sphinx xml" do
|
26
34
|
comment = Person.create!(:first_name => "Rafael", :last_name => "Souza")
|
27
35
|
|
28
|
-
expected =
|
29
|
-
assert_equal expected, Person.to_sphinx
|
36
|
+
expected = File.read(File.dirname(__FILE__) + "/../fixtures/sphinx_1.xml")
|
37
|
+
assert_equal expected.gsub("{comment}", comment.to_sphinx), Person.to_sphinx
|
30
38
|
end
|
31
|
-
|
39
|
+
|
32
40
|
test "return all indexed classes" do
|
33
|
-
assert_equal ["User", "Person", "Order", "Note"], ActiveRecord::Base.
|
41
|
+
assert_equal ["User", "Person", "Order", "Note"], ActiveRecord::Base.indexes.keys
|
34
42
|
end
|
35
43
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class TestSchema < Test::Unit::TestCase
|
5
|
+
|
6
|
+
test ".generate" do
|
7
|
+
schema = Dnif::Schema.new(User)
|
8
|
+
expected = File.read(File.dirname(__FILE__) + "/../fixtures/sphinx_4.xml")
|
9
|
+
assert_equal expected, schema.generate
|
10
|
+
end
|
11
|
+
end
|
metadata
CHANGED
@@ -6,9 +6,9 @@ version: !ruby/object:Gem::Version
|
|
6
6
|
- 0
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
-
|
11
|
-
version: 0.0.1.
|
9
|
+
- beta
|
10
|
+
- 1
|
11
|
+
version: 0.0.1.beta.1
|
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-
|
19
|
+
date: 2010-07-14 00:00:00 -03:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -72,21 +72,32 @@ files:
|
|
72
72
|
- dnif.gemspec
|
73
73
|
- lib/dnif.rb
|
74
74
|
- lib/dnif/configuration.rb
|
75
|
+
- lib/dnif/document.rb
|
76
|
+
- lib/dnif/index.rb
|
75
77
|
- lib/dnif/index_builder.rb
|
76
78
|
- lib/dnif/indexer.rb
|
77
79
|
- lib/dnif/multi_attribute.rb
|
80
|
+
- lib/dnif/schema.rb
|
78
81
|
- lib/dnif/search.rb
|
79
82
|
- lib/dnif/tasks.rb
|
80
83
|
- test/fixtures/db/schema.rb
|
81
84
|
- test/fixtures/log/searchd.pid
|
82
85
|
- test/fixtures/models.rb
|
86
|
+
- test/fixtures/sphinx_1.xml
|
87
|
+
- test/fixtures/sphinx_2.xml
|
88
|
+
- test/fixtures/sphinx_3.xml
|
89
|
+
- test/fixtures/sphinx_4.xml
|
90
|
+
- test/fixtures/sphinx_5.xml
|
83
91
|
- test/fixtures/templates/config.erb
|
84
92
|
- test/test_helper.rb
|
85
93
|
- test/unit/test_configuration.rb
|
86
94
|
- test/unit/test_dnif.rb
|
95
|
+
- test/unit/test_document.rb
|
96
|
+
- test/unit/test_index.rb
|
87
97
|
- test/unit/test_index_builder.rb
|
88
98
|
- test/unit/test_indexer.rb
|
89
99
|
- test/unit/test_multi_attribute.rb
|
100
|
+
- test/unit/test_schema.rb
|
90
101
|
- test/unit/test_search.rb
|
91
102
|
has_rdoc: true
|
92
103
|
homepage: http://github.com/rafaelss/dnif
|
@@ -128,7 +139,10 @@ test_files:
|
|
128
139
|
- test/test_helper.rb
|
129
140
|
- test/unit/test_configuration.rb
|
130
141
|
- test/unit/test_dnif.rb
|
142
|
+
- test/unit/test_document.rb
|
143
|
+
- test/unit/test_index.rb
|
131
144
|
- test/unit/test_index_builder.rb
|
132
145
|
- test/unit/test_indexer.rb
|
133
146
|
- test/unit/test_multi_attribute.rb
|
147
|
+
- test/unit/test_schema.rb
|
134
148
|
- test/unit/test_search.rb
|