Skip to content

Instantly share code, notes, and snippets.

@Sephi-Chan
Created August 22, 2014 20:32
Show Gist options
  • Save Sephi-Chan/4148f18f0111567e6a80 to your computer and use it in GitHub Desktop.
Save Sephi-Chan/4148f18f0111567e6a80 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require File.expand_path('../../config/environment', __FILE__)
require 'thor'
class PlayersCommand < Thor
desc :add, 'Add a player with the given name.'
method_option :force, desc: 'Delete the player if it already exists.', type: :boolean, default: false
def add(name)
existing_player = Player.find_by_name(name)
if existing_player && options[:force] == false
puts "A player named #{name} (##{existing_player.id}) already exists."
else
if existing_player
existing_player.destroy
puts "Existing player (##{existing_player.id}) has been removed."
end
player = Player.create(name: name, password: name)
puts "The player #{name} (##{player.id}) has been created."
end
end
desc :remove, 'Remove the player with the given ID/name.'
def remove(id_or_name)
if player = Player.find_by_id(id_or_name) || Player.find_by_name(id_or_name)
player.destroy
puts "The player #{player.name} (##{player.id}) has been removed."
else
puts "No player with ID or name #{id_or_name} was found."
end
end
end
PlayersCommand.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment