class Inspector

Attributes

command_counts[RW]
history[R]

Public Class Methods

new(history) click to toggle source
# File lib/inspector.rb, line 2
def initialize(history)
  @history ||= history
  @command_counts ||= count_commands(isolated_commands)
end

Public Instance Methods

count(command) click to toggle source
# File lib/inspector.rb, line 15
def count(command)
  command_counts[command].to_i
end
top(n) click to toggle source
# File lib/inspector.rb, line 7
def top(n)
  top_commands.take(n)
end
top_ten() click to toggle source
# File lib/inspector.rb, line 11
def top_ten
  top(10)
end

Private Instance Methods

combine_first_two(line) click to toggle source
# File lib/inspector.rb, line 50
def combine_first_two(line)
  if line
    commands = line.split(" ", 3)
    commands[1] ? [commands[0], commands[1]].join(" ") : commands[0]
  end
end
count_commands(commands) click to toggle source
# File lib/inspector.rb, line 34
def count_commands(commands)
  commands.reduce({}) do |memo, command|
    c = combine_first_two(command)
    memo[c] ? memo[c] += 1 : memo[c] = 1
    memo
  end
end
isolated_commands() click to toggle source
# File lib/inspector.rb, line 28
def isolated_commands
  history.split("\n").map do |line|
    remove_line_numbers(line)
  end
end
line_comes_from_histfile?(line) click to toggle source
# File lib/inspector.rb, line 57
def line_comes_from_histfile?(line)
  line[0] == ':'
end
remove_line_numbers(line) click to toggle source
# File lib/inspector.rb, line 42
def remove_line_numbers(line)
  if line_comes_from_histfile?(line)
    line.split(/:.*;/)[1]
  else
    line.lstrip.split(/\d+../, 2)[1]
  end
end
top_commands() click to toggle source
# File lib/inspector.rb, line 24
def top_commands
  command_counts.sort_by { |_, count| count }.reverse
end