Class: ReFe::CompletionTable

Inherits:
Object
  • Object
show all
Includes:
TraceUtils
Defined in:
lib/refe/completiontable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TraceUtils

#trace

Constructor Details

#initialize(path, writeok = false) ⇒ CompletionTable

Returns a new instance of CompletionTable.



19
20
21
22
23
24
25
26
27
# File 'lib/refe/completiontable.rb', line 19

def initialize( path, writeok = false )
  @path = path
  @writeok = writeok
  begin
    @list = File.readlines(path).map {|line| line.strip }
  rescue Errno::ENOENT
    @list = []
  end
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



29
30
31
# File 'lib/refe/completiontable.rb', line 29

def path
  @path
end

Instance Method Details

#add(item) ⇒ Object

Raises:

  • (ArgumentError)


50
51
52
53
# File 'lib/refe/completiontable.rb', line 50

def add( item )
  raise ArgumentError, 'database is not writable' unless @writeok
  @list.push item
end

#expand(re) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/refe/completiontable.rb', line 39

def expand( re )
  trace "comp: pattern = #{re.inspect}"
  results = @list.select {|item| re === item }
  if results.size < 20
    trace "comp: results = #{results.join(' ')}"
  else
    trace "comp: results = (too many candidates: #{results.size})"
  end
  results
end

#flushObject

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/refe/completiontable.rb', line 55

def flush
  raise ArgumentError, "database is readonly: path=#{@path}" unless @writeok
  begin
    File.open("#{@path}_tmp#{$$}", 'w') {|f|
      @list.uniq.sort.each do |i|
        f.puts i
      end
    }
    File.rename "#{@path}_tmp#{$$}", @path
  rescue
    begin
      File.unlink "#{@path}_tmp#{$$}"
    rescue Errno::ENOENT
      ;
    end
  end
end

#inspectObject



31
32
33
# File 'lib/refe/completiontable.rb', line 31

def inspect
  "\#<#{self.class} path=#{@path}>"
end

#listObject



35
36
37
# File 'lib/refe/completiontable.rb', line 35

def list
  @list.dup
end