Class: Mpx::History
- Inherits:
-
Object
- Object
- Mpx::History
- Defined in:
- lib/mpx/history.rb
Overview
Manages history for commands.
Instance Method Summary collapse
- #all_history ⇒ Object
- #get(*commands) ⇒ Object
- #history_for(command) ⇒ Object
-
#initialize(root) ⇒ History
constructor
A new instance of History.
- #now ⇒ Object
- #write(command, *args) ⇒ Object
Constructor Details
Instance Method Details
#all_history ⇒ Object
15 16 17 18 |
# File 'lib/mpx/history.rb', line 15 def all_history return Dir.entries(@root) .select { |f| File.file?(File.join(@root, f)) } end |
#get(*commands) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/mpx/history.rb', line 20 def get(*commands) return (commands.empty? ? all_history : commands) .map { |c| history_for(c) } .flatten .sort_by do |line| time, * = line.split('$') DateTime.parse(time.strip) end end |
#history_for(command) ⇒ Object
30 31 32 33 34 35 |
# File 'lib/mpx/history.rb', line 30 def history_for(command) File.foreach(File.join(@root, command)) .map { |line| line.strip } rescue raise "no history for #{command}" end |
#now ⇒ Object
11 12 13 |
# File 'lib/mpx/history.rb', line 11 def now return DateTime.now.strftime("%d/%m/%Y %H:%M") end |
#write(command, *args) ⇒ Object
37 38 39 40 41 |
# File 'lib/mpx/history.rb', line 37 def write(command, *args) File.open(File.join(@root, command), 'a') do |f| f.puts("#{now} $ #{command} #{args.join(' ')}") end end |