Module: Rib::History

Extended by:
Plugin
Defined in:
lib/rib/core/history.rb

Instance Attribute Summary

Attributes included from Plugin

#disabled

Instance Method Summary collapse

Methods included from Plugin

disable, disabled?, enable, enabled?, extended

Instance Method Details

#after_loopObject



21
22
23
24
25
26
# File 'lib/rib/core/history.rb', line 21

def after_loop
  return super if History.disabled?
  write_history
  Rib.say("History wrote to: #{history_file_path}") if $VERBOSE
  super
end

#before_loopObject

————— Rib API —————



11
12
13
14
15
16
17
18
19
# File 'lib/rib/core/history.rb', line 11

def before_loop
  return super if History.disabled?
  history_file
  history_size
  FileUtils.mkdir_p(File.dirname(history_file_path))
  read_history
  Rib.say("History read from: #{history_file_path}") if $VERBOSE
  super
end

#get_inputObject



28
29
30
31
# File 'lib/rib/core/history.rb', line 28

def get_input
  return super if History.disabled?
  (history << super).last
end

#historyObject

The history data



36
# File 'lib/rib/core/history.rb', line 36

def history; config[:history] ||= []; end

#read_historyObject

Read config into #history, handled in history_file plugin



39
40
41
42
43
# File 'lib/rib/core/history.rb', line 39

def read_history
  return super if History.disabled?
  File.exist?(history_file_path) && history.empty? &&
    File.readlines(history_file_path).each{ |e| history << e.chomp }
end

#write_historyObject

Write #history into config, handled in history_file plugin



46
47
48
49
50
# File 'lib/rib/core/history.rb', line 46

def write_history
  return super if History.disabled?
  File.open(history_file_path, 'w'){ |f|
    f.puts(history.to_a.last(history_size).join("\n")) }
end