class PathTable

Controller for paths

Constants

PATHTABLE_FAVORITE_KEY

Attributes

favorite[RW]
history_limit[RW]
table[RW]

Public Class Methods

new(file_path) click to toggle source
# File lib/overpath/path_table.rb, line 5
def initialize(file_path)
  @file_path = file_path
  @table = {}
  @history_limit = 5
end

Public Instance Methods

load() click to toggle source
# File lib/overpath/path_table.rb, line 31
def load
  if File.exist?(@file_path)
    table = JSON.parse(File.open(@file_path, 'r').gets)
    if table.is_a?(Hash)
      @table = table.reject { |key, _value| key == PATHTABLE_FAVORITE_KEY }
      @table.each_value { |value| value.chomp!('/') }
      @favorite = table[PATHTABLE_FAVORITE_KEY]
    end
  end
  @table
end
save() click to toggle source
# File lib/overpath/path_table.rb, line 11
def save
  File.open(@file_path + '.new', 'w+') do |file|
    save_to_file(file)
  end
  File.rename(@file_path, @file_path + '.old') if File.exist?(@file_path)
  File.rename(@file_path + '.new', @file_path)
end
save_to_file(file) click to toggle source
# File lib/overpath/path_table.rb, line 19
def save_to_file(file)
  favorite = { PATHTABLE_FAVORITE_KEY => @favorite }
  table = @favorite ? @table.merge(favorite) : @table
  file.puts JSON.generate(table)
  if File.exist?(@file_path)
    File.foreach(@file_path).with_index do |line, i|
      break if i > (@history_limit - 1)
      file.puts line
    end
  end
end