Class: Mpx::History

Inherits:
Object
  • Object
show all
Defined in:
lib/mpx/history.rb

Overview

Manages history for commands.

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ History

Returns a new instance of History.



7
8
9
# File 'lib/mpx/history.rb', line 7

def initialize(root)
  @root = root
end

Instance Method Details

#all_historyObject



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

#nowObject



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