Sha256: 8b8f437de4760c896ce1ac4dcd67fff3aa6fd708e8d1ddd6d954c147d67454ee
Contents?: true
Size: 1.32 KB
Versions: 4
Compression:
Stored size: 1.32 KB
Contents
require 'pathname' module Ru class CommandManager class InvalidCodeError < ArgumentError; end class InvalidNameError < ArgumentError; end 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 def get(name) validate_name(name) commands = get_commands commands[name] end def all get_commands end private 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 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 def validate_name(name) if name !~ /^[\w_]+$/ raise InvalidNameError.new("Invalid command name '#{name}'. Command names may only contain alphanumerics and underscores.") end end def path Pathname.new(::File.expand_path('~')).join('.ru_commands') end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
ru-0.1.4 | lib/ru/command_manager.rb |
ru-0.1.3 | lib/ru/command_manager.rb |
ru-0.1.2 | lib/ru/command_manager.rb |
ru-0.1.1 | lib/ru/command_manager.rb |