Class: ReFe::CompletionTable
- Inherits:
-
Object
- Object
- ReFe::CompletionTable
- Includes:
- TraceUtils
- Defined in:
- lib/refe/completiontable.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #add(item) ⇒ Object
- #expand(re) ⇒ Object
- #flush ⇒ Object
-
#initialize(path, writeok = false) ⇒ CompletionTable
constructor
A new instance of CompletionTable.
- #inspect ⇒ Object
- #list ⇒ Object
Methods included from TraceUtils
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
#path ⇒ Object (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
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 ( 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 |
#flush ⇒ Object
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 |
#inspect ⇒ Object
31 32 33 |
# File 'lib/refe/completiontable.rb', line 31 def inspect "\#<#{self.class} path=#{@path}>" end |
#list ⇒ Object
35 36 37 |
# File 'lib/refe/completiontable.rb', line 35 def list @list.dup end |