Class: WebArchive::Completer

Inherits:
Object
  • Object
show all
Defined in:
lib/webarchive.rb

Overview

completer for URLs

Instance Method Summary collapse

Constructor Details

#initialize(history_file) ⇒ Completer

Returns a new instance of Completer.



113
114
115
116
117
# File 'lib/webarchive.rb', line 113

def initialize(history_file)
  @file = File.expand_path(history_file)
  @trie = Trie.new
  self.reload
end

Instance Method Details

#append_to_history(str) ⇒ Object



141
142
143
144
145
# File 'lib/webarchive.rb', line 141

def append_to_history(str)
  File.open(@file, mode: 'a', encoding: 'utf-8') do |f|
    f.puts str
  end
end

#reloadObject



123
124
125
126
127
128
129
130
131
132
# File 'lib/webarchive.rb', line 123

def reload
  if File.exist? @file
    File.open(@file, encoding: 'utf-8').each_line do |x|
      @trie.add x.strip
    end
  else
    File.new(@file, 'w', encoding: 'utf-8').flush
  end
  @lastupdate = Time.now
end

#to_procObject



134
135
136
137
138
139
# File 'lib/webarchive.rb', line 134

def to_proc
  proc do |s|
    self.update
    @trie.children(s)
  end
end

#updateObject



119
120
121
# File 'lib/webarchive.rb', line 119

def update
  self.reload if File.stat(@file).mtime > @lastupdate
end