Class: TimesheetParser

Inherits:
Object
  • Object
show all
Defined in:
lib/timesheet/timesheet_parser.rb

Constant Summary collapse

TIME_PERIODS =
[:today, :current_week, :current_month, :yesterday, :last_week, :last_month]
SUB_COMMANDS =
%w(add list edit delete report)
WEEK_START =
"monday"
USAGE =
<<-EOS
Timesheet is a script for keeping track of time spent on various projects.

Usage:

timesheet [OPTIONS] COMMAND [ARGS]

COMMAND is any of the following:
  add
  edit
  delete
  list
  report

see 'timesheet COMMAND --help' for more information on a specific command.

Note: your timesheet data will be stored in a hidden directory under your user account. Timesheet
figures out where this is by referencing the "HOME" environment variable. The default location is
therefore: /Users/someuser/.timesheet/store.yaml

You may override this location to any specific location you want by setting the "TIMESHEET_DATA_FILE"
environment variable. This should be the full path to where you want the data stored. Including filename
and extension. You only need to set this if you are unsatisfied with the default location.

OPTIONS are:

EOS

Class Method Summary collapse

Class Method Details

.convert_to_start_end(period) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/timesheet/timesheet_parser.rb', line 206

def self.convert_to_start_end(period)
  periods = {}
  case period
    when :today
      periods[:start] = Chronic.parse('today at 12 am')
      periods[:end] = periods[:start] + 1.day

    when :current_week
      periods[:start] = Chronic.parse("this week #{WEEK_START} at 12 am")
      periods[:end] = periods[:start] + 1.week

    when :current_month
      today = Date.today
      next_month = today >> 1
      periods[:start] = Time.local(today.year, today.month, 1, 0, 0, 0)
      periods[:end] = Time.local(next_month.year, next_month.month, 1, 0, 0, 0)

    when :yesterday
      periods[:start] = Chronic.parse('yesterday at 12 am')
      periods[:end] = periods[:start] + 1.day

    when :last_week
      periods[:start] = Chronic.parse("this week #{WEEK_START} at 12 am") - 1.week
      periods[:end] = periods[:start] + 1.week

    when :last_month
      today = Date.today
      last_month = today << 1
      periods[:start] = Time.local(last_month.year, last_month.month, 1, 0, 0, 0)
      periods[:end] = Time.local(today.year, today.month, 1, 0, 0, 0)

    else raise "Unknown time period #{period.to_s}"
  end
  periods
end

.parse(args, outstream = STDOUT) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/timesheet/timesheet_parser.rb', line 38

def self.parse(args, outstream=STDOUT)

  global_opts = Trollop::options(args) do
    version "Timesheet #{Timesheet::VERSION} (c) 2009 John F. Schank III"
    banner USAGE
    stop_on SUB_COMMANDS

    opt :debug, "Show debugging information while processing"
  end

  if (args.nil? || args.empty?)
    Trollop::die "No parameters on command-line, use -h for help"
  end

  command = args.shift
  command_opts = case command
    when "add"
      add_options = Trollop::options(args) do
        banner <<-EOS
timesheet add [OPTIONS]

    Adds an entry to the database.
    If there is an overlap, it should present a choice to split the entry, or abort.

OPTIONS are:

EOS
        opt :project, "Project name", {:type => :string, :required => true}
        opt :start, "Start date-time", {:type => :time, :required => true}
        opt :end, "End date-time", {:type => :time, :required => true}
        opt :comment, "comment", {:type => :string}
        depends :start, :end
      end
      add_options[:command] = :add
      add_options

    when "edit"
      edit_options = Trollop::options(args) do
        banner <<-EOS
timesheet edit [OPTIONS]

    Allows editing of existing entries

OPTIONS are:

EOS
        opt :record_number, "Record Number", {:type => :int, :required => true}
        opt :project, "Project name", {:type => :string}
        opt :start, "Start date-time", {:type => :time}
        opt :end, "End date-time", {:type => :time}
        opt :comment, "comment", {:type => :string}
      end

      edit_options[:command] = :edit
      edit_options

    when "delete"
      delete_options = Trollop::options(args) do
        banner <<-EOS
timesheet delete [OPTIONS]

    Allows deletion of existing entries

OPTIONS are:


EOS
        opt :record_number, "Record Number", {:type => :int, :required => true}
      end

      delete_options[:command] = :delete
      delete_options

    when "list"
      list_options = Trollop::options(args) do
        banner <<-EOS
timesheet list [OPTIONS]

    Prints all matching entries. Any entry where the given date is covered by the entry.
    Each entry's record number is displayed

OPTIONS are:


EOS
        opt :today, "Current Day"
        opt :current_week, "Current Week"
        opt :current_month, "Current Month"
        opt :yesterday, "Yesterday"
        opt :last_week, "Last Week"
        opt :last_month, "Last Month"
        opt :start, "Start date-time", {:type => :time}
        opt :end, "End date-time", {:type => :time}
        depends :start, :end
        conflicts :today, :current_week, :current_month, :yesterday, :last_week, :last_month, :start
      end

      # a list is just a detailed report.
      list_options[:command] = :report
      list_options[:detail] = true
      TIME_PERIODS.each do |period|
        list_options.merge!(convert_to_start_end(period)) if list_options[period]
        list_options.delete(period)
      end
      list_options.merge!(convert_to_start_end(:today)) unless list_options[:start]  # add default period
      list_options

    when "report"
      report_options = Trollop::options(args) do
        banner <<-EOS
timesheet report [OPTIONS]

   displays total hours by project. For entries in the period.

   summary report shows hours spent on top level projects, and excludes comments
   detail shows hours for each item.
   byday shows hours for each day, with entries and comments. (daily breakout)

   defaults: --summary, and --today 

OPTIONS are:


EOS
        opt :summary, "Summary report"
        opt :detail, "Detailed report"
        opt :byday, "By Day report"
        opt :today, "Current Day"
        opt :current_week, "Current Week"
        opt :current_month, "Current Month"
        opt :yesterday, "Yesterday"
        opt :last_week, "Last Week"
        opt :last_month, "Last Month"
        opt :start, "Start date-time", {:type => :time}
        opt :end, "End date-time", {:type => :time}
        conflicts :summary, :detail, :byday
        conflicts :today, :current_week, :current_month, :yesterday, :last_week, :last_month, :start
        depends :start, :end
      end
      report_options[:command] = :report
      TIME_PERIODS.each do |period|
        report_options.merge!(convert_to_start_end(period)) if report_options[period]
        report_options.delete(period)
      end
      report_options.merge!(convert_to_start_end(:today)) unless report_options[:start]  # add default period
      report_options.merge!({:summary => true}) unless report_options[:summary] || report_options[:detail] || report_options[:byday]
      report_options

    else
      Trollop::die "unknown subcommand #{command.inspect}"
  end

  opts = {}
  opts.merge! global_opts
  opts.merge! command_opts
  opts.merge!({:remainder => args})

  if (opts[:debug] )
    outstream.puts "Command-line options hash..."
    outstream.puts opts.inspect
  end

  raise ArgumentError.new("Command was not fully understood. Use --debug flag to diagnose.") unless opts[:remainder].empty?

  opts

end