Module: HCl::Utility

Included in:
App, DayEntry
Defined in:
lib/hcl/utility.rb

Instance Method Summary collapse

Instance Method Details

#as_hours(hours) ⇒ String

Convert from decimal to a string of the form HH:MM.

Parameters:

  • hours (#to_f)

    number of hours in decimal

Returns:

  • (String)

    of the form “HH:MM”



46
47
48
49
# File 'lib/hcl/utility.rb', line 46

def as_hours hours
  minutes = hours.to_f * 60.0
  sprintf "%d:%02d", (minutes / 60).to_i, (minutes % 60).to_i
end

#current_timeObject



38
39
40
# File 'lib/hcl/utility.rb', line 38

def current_time
  Time.now.strftime('%I:%M %p').downcase
end

#fail(*message) ⇒ Object

Raises:



4
5
6
# File 'lib/hcl/utility.rb', line 4

def fail *message
  raise CommandError, message.join(' ')
end

#get_ident(args) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/hcl/utility.rb', line 16

def get_ident args
  ident = args.detect {|a| a[0] == '@' }
  if ident
    args.delete(ident)
    ident.slice(1..-1)
  else
    args.shift
  end
end

#get_starting_time(args) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/hcl/utility.rb', line 30

def get_starting_time args
  starting_time = args.detect {|x| x =~ /^\+\d*(\.|:)?\d+$/ }
  if starting_time
    args.delete(starting_time)
    time2float starting_time
  end
end

#get_task(args) ⇒ Object



26
27
28
# File 'lib/hcl/utility.rb', line 26

def get_task args
  Task.find *get_task_ids(get_ident(args), args)
end

#get_task_ids(ident, args) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/hcl/utility.rb', line 8

def get_task_ids ident, args
  if @settings.key? "task.#{ident}"
    @settings["task.#{ident}"].split(/\s+/)
  else
    [ident, args.shift]
  end
end

#time2float(time_string) ⇒ #to_f

Convert from a time span in hour or decimal format to a float.

Parameters:

  • time_string (String)

    either “M:MM” or decimal

Returns:

  • (#to_f)

    converted to a floating-point number



55
56
57
58
59
60
61
62
# File 'lib/hcl/utility.rb', line 55

def time2float time_string
  if time_string =~ /:/
    hours, minutes = time_string.split(':')
    hours.to_f + (minutes.to_f / 60.0)
  else
    time_string.to_f
  end
end