This repository has been archived by the owner on Nov 16, 2017. It is now read-only.
forked from ulbrich/couchsphinx
/
properties.rb
75 lines (61 loc) · 2.13 KB
/
properties.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# CouchSphinx, a full text indexing extension for CouchDB/CouchRest using
# Sphinx.
#
# This file contains the CouchRest::Mixins::Properties module.
# Patches to the CouchRest library.
module CouchRest # :nodoc:
module Mixins # :nodoc:
# Patches to the CouchRest Properties module: Adds the "attributes" method
# plus some fulltext relevant stuff.
#
# Samples:
#
# data = SERVER.default_database.view('CouchSphinxIndex/couchrests_by_timestamp')
# rows = data['rows']
# post = Post.new(rows.first)
#
# post.attributes
# => {:tags=>"one, two, three", :updated_at=>Tue Jun 09 14:45:00 +0200 2009,
# :author=>nil, :title=>"First Post",
# :created_at=>Tue Jun 09 14:45:00 +0200 2009,
# :body=>"This is the first post. This is the [...] first post. "}
#
# post.fulltext_attributes
# => {:title=>"First Post", :author=>nil,
# :created_at=>Tue Jun 09 14:45:00 +0200 2009
# :body=>"This is the first post. This is the [...] first post. "}
#
# post.sphinx_id
# => "921744775"
# post.id
# => "Post-921744775"
module Properties
# Returns a Hash of all properties plus the ID of the document.
def attributes
data = {}
self.properties.collect { |p|
{ p.name.intern => self.send(p.name) } }.each { |h|
data.merge! h }
return data
end
# Returns a Hash of all attributes allowed to be indexed. As a side
# effect it sets the fulltext_keys variable if still blank or empty.
def fulltext_attributes
clas = self.class
if not clas.fulltext_keys or clas.fulltext_keys.empty?
clas.fulltext_keys = self.properties.collect { |p| p.name.intern }
end
return self.attributes.reject { |k, v|
not (clas.fulltext_keys.include? k) }
end
# Returns the numeric part of the document ID (compatible to Sphinx).
def sphinx_id
if (match = self.id.match(/#{self.class}-([0-9]+)/))
return match[1]
else
return nil
end
end
end
end
end