-
Notifications
You must be signed in to change notification settings - Fork 49
/
indexes.rb
82 lines (68 loc) · 1.63 KB
/
indexes.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
76
77
78
79
80
81
82
module Picky
# Holds all indexes and provides operations
# for extracting and working on them.
#
# Delegates a number of operations to the
# indexes.
#
class Indexes
attr_reader :indexes,
:index_mapping
delegate :size,
:each,
:to => :indexes
each_delegate :reindex,
:to => :indexes
def initialize *indexes
clear_indexes
indexes.each { |index| register index }
end
# Return the Indexes instance.
#
def self.instance
@instance ||= new
end
def self.identifier
name
end
instance_delegate :clear,
:register,
:reindex,
:[],
:to_s,
:size,
:each,
:each_category
# Clears the indexes and the mapping.
#
def clear_indexes
@indexes = []
@index_mapping = {}
end
# Registers an index with the indexes.
#
def register index
self.indexes << index
self.index_mapping[index.name] = index
end
def self.register index
self.instance.register index
end
# Extracts an index, given its identifier.
#
def [] identifier
index_name = identifier.intern
index_mapping[index_name] || raise_not_found(index_name)
end
# Raises a not found for the index.
#
def raise_not_found index_name
raise %Q{Index "#{index_name}" not found. Possible indexes: "#{indexes.map(&:name).join('", "')}".}
end
#
#
def to_s
indexes.indented_to_s
end
end
end