Class: MailProvider::Trie
- Inherits:
-
Object
- Object
- MailProvider::Trie
show all
- Defined in:
- lib/mail_provider/trie.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name) ⇒ Trie
Returns a new instance of Trie.
24
25
26
27
|
# File 'lib/mail_provider/trie.rb', line 24
def initialize(name)
@name = name
@native = Hamster::Trie.new(0)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *a, &b) ⇒ Object
62
63
64
65
66
|
# File 'lib/mail_provider/trie.rb', line 62
def method_missing(m, *a, &b)
return @native.send(m, *a, &b) if @native.respond_to?(m, true)
super
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
22
23
24
|
# File 'lib/mail_provider/trie.rb', line 22
def name
@name
end
|
Class Method Details
.base_for(name, directory) ⇒ Object
18
19
20
|
# File 'lib/mail_provider/trie.rb', line 18
def self.base_for(name, directory)
File.join(directory, "#{name}.dump")
end
|
.create(name, entries, opts = {}) ⇒ Object
5
6
7
8
9
10
|
# File 'lib/mail_provider/trie.rb', line 5
def self.create(name, entries, opts = {})
new(name).tap do |trie|
entries.each { |key, val| trie.put! key, val }
trie.save opts[:directory]
end
end
|
.load(name, opts = {}) ⇒ Object
12
13
14
15
16
|
# File 'lib/mail_provider/trie.rb', line 12
def self.load(name, opts = {})
new(name).tap do |trie|
trie.load opts[:directory]
end
end
|
Instance Method Details
#ending_with(str) ⇒ Object
40
41
42
|
# File 'lib/mail_provider/trie.rb', line 40
def ending_with(str)
entries { |en| en.end_with?(str) }
end
|
#entries_where ⇒ Object
Also known as:
entries
29
30
31
32
33
|
# File 'lib/mail_provider/trie.rb', line 29
def entries_where
selected = []
@native.each { |en| selected << en if !block_given? || yield(en[0]) }
selected.sort_by(&:last).to_h
end
|
#load(directory) ⇒ Object
48
49
50
51
52
53
54
55
|
# File 'lib/mail_provider/trie.rb', line 48
def load(directory)
base = self.class.base_for(@name, directory)
unless File.exist?(base)
raise TrieLoadError, "Saved trie does not exist at: #{base}"
end
@native = Hamster::Trie[Marshal.load(File.read(base))]
end
|
#matching(regex) ⇒ Object
36
37
38
|
# File 'lib/mail_provider/trie.rb', line 36
def matching(regex)
entries { |en| en =~ regex }
end
|
#respond_to_missing?(m, *a, &b) ⇒ Boolean
68
69
70
|
# File 'lib/mail_provider/trie.rb', line 68
def respond_to_missing?(m, *a, &b)
@native.respond_to?(m, *a, &b) || super
end
|
#save(directory) ⇒ Object
57
58
59
60
|
# File 'lib/mail_provider/trie.rb', line 57
def save(directory)
base = self.class.base_for(@name, directory)
File.open(base, 'wb') { |f| f.write Marshal.dump(entries) }
end
|
#starting_with(str) ⇒ Object
44
45
46
|
# File 'lib/mail_provider/trie.rb', line 44
def starting_with(str)
entries { |en| en.start_with?(str) }
end
|