class R2do::App

Public Class Methods

new(args) click to toggle source

Creates an instance of the application.

@param [Array] args the command line args.

# File lib/r2do.rb, line 45
def initialize(args)
  @args = args

  @file_name = ".r2do.yml"
  @state = load_state(@file_name)

  @commands = create_commands()
end

Public Instance Methods

run() click to toggle source

Evaluates the command passed by the user and calls the corresponding application command.

@return [void]

# File lib/r2do.rb, line 57
def run()
  option = @args[0]

  if @args.length > 0
    cmd = find_command(option)
    if not cmd.nil?
      begin
        cmd.execute(@args)
      rescue Exception => e
        UI.rescue(e)
      end
    else
      invalid_command(option)
    end
  else
    show_help(@args)
  end
end
save() click to toggle source

Saves the state of the application

@return [void]

# File lib/r2do.rb, line 79
def save()
  if @state.modified
    save_state(@file_name, @state)
  end
end

Private Instance Methods

create_commands() click to toggle source

Creates the list of commands the application responds to.

@return [Array] the collection of commands.

# File lib/r2do.rb, line 90
def create_commands()
  cmd_list = Array.new
  cmd_list << InitCommand.new(@state)
  cmd_list << CategoryCommand.new(@state)
  cmd_list << TaskCommand.new(@state)
  cmd_list << DisplayCategoriesCommand.new(@state)
  cmd_list << NowCommand.new(@state)

  cmd_list << HelpCommand.new(Array.new(cmd_list))

  cmd_list << Option.new('-v', '--version', 'Prints the application version.', method(:show_version))
  cmd_list << Option.new('-h', '--help', 'You are looking at it.', method(:show_help))

  cmd_list
end
find_command(option) click to toggle source

Finds the command based on the option value passed by the user

@param [String] option the option the user passed the application @return [Command] the command identified by option, else nil

# File lib/r2do.rb, line 110
def find_command(option)
  @commands.find { |cmd| cmd.short == option or cmd.extended == option }
end
invalid_command(option) click to toggle source

Invalid command handler

@param [String] option the option the user passed the application @return [void]

# File lib/r2do.rb, line 118
def invalid_command(option)
  UI.status("r2do: '#{option}' is not an r2do command. See 'r2do -h'.")
end