Class: PathTable
- Inherits:
-
Object
- Object
- PathTable
- Defined in:
- lib/overpath/path_table.rb
Overview
Controller for paths
Constant Summary collapse
- PATHTABLE_FAVORITE_KEY =
'PATHTABLE_FAVORITE_KEY'.freeze
Instance Attribute Summary collapse
-
#favorite ⇒ Object
Returns the value of attribute favorite.
-
#table ⇒ Object
Returns the value of attribute table.
Instance Method Summary collapse
-
#initialize(file_path) ⇒ PathTable
constructor
A new instance of PathTable.
- #load ⇒ Object
- #save ⇒ Object
- #save_to_file(file) ⇒ Object
Constructor Details
#initialize(file_path) ⇒ PathTable
Returns a new instance of PathTable.
5 6 7 8 |
# File 'lib/overpath/path_table.rb', line 5 def initialize(file_path) @file_path = file_path @table = {} end |
Instance Attribute Details
#favorite ⇒ Object
Returns the value of attribute favorite.
40 41 42 |
# File 'lib/overpath/path_table.rb', line 40 def favorite @favorite end |
#table ⇒ Object
Returns the value of attribute table.
39 40 41 |
# File 'lib/overpath/path_table.rb', line 39 def table @table end |
Instance Method Details
#load ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/overpath/path_table.rb', line 28 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 ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/overpath/path_table.rb', line 10 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) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/overpath/path_table.rb', line 18 def save_to_file(file) favorite = { PATHTABLE_FAVORITE_KEY => @favorite } table = @favorite ? @table.merge(favorite) : @table file.puts JSON.generate(table) File.foreach(@file_path).with_index do |line, i| break if i > 14 file.puts line end end |