class Ru::CommandManager

Public Instance Methods

all() click to toggle source
# File lib/ru/command_manager.rb, line 22
def all
  get_commands
end
get(name) click to toggle source
# File lib/ru/command_manager.rb, line 16
def get(name)
  validate_name(name)
  commands = get_commands
  commands[name]
end
save(name, code) click to toggle source
# File lib/ru/command_manager.rb, line 8
def save(name, code)
  raise InvalidCodeError.new("Invalid code. Code cannot be blank.") if code.blank?
  validate_name(name)
  commands = get_commands
  commands[name] = code
  save_commands(commands)
end

Private Instance Methods

get_commands() click to toggle source
# File lib/ru/command_manager.rb, line 37
def get_commands
  if ::File.exists?(path)
    commands = {}
    ::File.readlines(path).each do |line|
      pieces = line.chomp.split(',', 2)
      commands[pieces[0]] = pieces[1]
    end
    commands
  else
    {}
  end
end
path() click to toggle source
# File lib/ru/command_manager.rb, line 56
def path
  Pathname.new(::File.expand_path('~')).join('.ru_commands')
end
save_commands(commands) click to toggle source
# File lib/ru/command_manager.rb, line 28
def save_commands(commands)
  lines = []
  commands.each do |name, code|
    lines << "#{name},#{code}"
  end
  content = lines.join("\n")
  ::File.open(path, 'w') { |file| file.write(content) }
end
validate_name(name) click to toggle source
# File lib/ru/command_manager.rb, line 50
def validate_name(name)
  if name !~ /^[\w_]+$/
    raise InvalidNameError.new("Invalid command name '#{name}'. Command names may only contain alphanumerics and underscores.")
  end
end