Class: HCl::App

Inherits:
Object
  • Object
show all
Includes:
Commands, Utility
Defined in:
lib/hcl/app.rb

Defined Under Namespace

Classes: UnknownCommand

Constant Summary collapse

SETTINGS_FILE =
"#{ENV['HOME']}/.hcl_settings"
CONFIG_FILE =
"#{ENV['HOME']}/.hcl_config"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Commands

#aliases, #note, #set, #show, #start, #stop, #tasks, #unset

Methods included from Utility

#as_hours, #time2float

Constructor Details

#initializeApp

Returns a new instance of App.



44
45
46
47
# File 'lib/hcl/app.rb', line 44

def initialize
  read_config
  read_settings
end

Class Method Details

.command(*args) ⇒ Object

Run the given command and arguments.



50
51
52
# File 'lib/hcl/app.rb', line 50

def self.command *args
  hcl = new.process_args(*args).run
end

Instance Method Details

#command?(command) ⇒ true, false

Return true if the string is a known command, false otherwise.

Parameters:

  • command (#to_s)

    name of command

Returns:

  • (true, false)


58
59
60
# File 'lib/hcl/app.rb', line 58

def command? command
  Commands.instance_methods.include? command.to_s
end

#process_args(*args) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/hcl/app.rb', line 87

def process_args *args
  Trollop::options(args) do
    stop_on Commands.instance_methods
    version "HCl version #{VERSION}"
    banner <<-EOM
HCl is a command-line client for manipulating Harvest time sheets.

Commands:
hcl show [date]
hcl tasks
hcl aliases
hcl set <key> <value ...>
hcl unset <key>
hcl start <task> [msg]
hcl stop [msg]
hcl note <msg>

Examples:
$ hcl tasks
$ hcl start 1234 4567 this is my log message
$ hcl set task.mytask 1234 4567
$ hcl start mytask this is my next log message
$ hcl show yesterday
$ hcl show last tuesday

Options:
EOM
  end
  @command = args.shift
  @args = args
  self
end

#runObject

Start the application.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hcl/app.rb', line 63

def run
  begin
    if @command
      if command? @command
        result = send @command, *@args
        if not result.nil?
          if result.respond_to? :to_a
            puts result.to_a.join(', ')
          elsif result.respond_to? :to_s
            puts result
          end
        end
      else
        raise UnknownCommand, "unrecognized command `#{@command}'"
      end
    else
      show
    end
  rescue TimesheetResource::Failure => e
    puts "Internal failure. #{e}"
    exit 1
  end
end