Class: Termodoro

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

Overview

A new instance of this class takes user-input as command line arguments and prepares a string to be execute as a Bash system command. The user specifies an amount of time and an optional message to display. After the amount of time elapses a terminal-notifier message appears with the message - if the message was omitted, the word “Termodoro” appears instead.

Constant Summary collapse

SECS_IN_MIN =

Used in multiplication to arrive at the number of seconds in a minute.

60
SECS_IN_HOUR =

Used in multiplication to arrive at the number of seconds in an hour.

3600

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ Termodoro

Returns a new instance of Termodoro.

Parameters:

  • arguments (String)

    The arguments passed in by the user at the command line. this includes the time after which the reminder will appear and an optional message to appear in the body of the reminder.



25
26
27
# File 'lib/termodoro/termodoro.rb', line 25

def initialize(arguments)
  @arguments = arguments
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



9
10
11
# File 'lib/termodoro/termodoro.rb', line 9

def arguments
  @arguments
end

#messageString

Returns the message specified by the user.

Returns:

  • (String)

    the message specified by the user.



10
11
12
# File 'lib/termodoro/termodoro.rb', line 10

def message
  @message
end

#number_of_unitsInteger

Returns the number of units of time specified by the user.

Returns:

  • (Integer)

    the number of units of time specified by the user.



10
11
12
# File 'lib/termodoro/termodoro.rb', line 10

def number_of_units
  @number_of_units
end

#time_unitString

Returns the unit of time specified by the user.

Returns:

  • (String)

    the unit of time specified by the user.



10
11
12
# File 'lib/termodoro/termodoro.rb', line 10

def time_unit
  @time_unit
end

Instance Method Details

#calculate_timeInteger

Depending on what unit of time is being used, determines the number of seconds using multiplication by SECS_IN_MIN and SECS_IN_HOUR constants. The check to #seconds? is not necessary, but feels nice.

Returns:

  • (Integer)

    total number of seconds for which to wait until the reminder is displayed.

See Also:



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/termodoro/termodoro.rb', line 64

def calculate_time
  if minutes?
    seconds = parse_number_of_units * SECS_IN_MIN
  elsif hours?
    seconds = parse_number_of_units * SECS_IN_HOUR
  elsif seconds?
    seconds = parse_number_of_units
  end

  seconds
end

#commandString

Workhorse method: Runs #calculate_time and #parse_message, taking the results of those methods and creating the string to execute.

Returns:

  • (String)

    the fully-formed command string ready to be run by bash as a system command.

See Also:



114
115
116
117
118
# File 'lib/termodoro/termodoro.rb', line 114

def command
  time_unit = calculate_time
  msg_part = parse_message
  "sleep #{time_unit} && terminal-notifier -message '#{msg_part}' -title 'Termodoro' & disown"
end

#hours?Boolean

Truthy if user has input a number of hours.

Returns:

  • (Boolean)

See Also:



96
97
98
99
# File 'lib/termodoro/termodoro.rb', line 96

def hours?
  hours = %w[h hr hrs hour hours]
  true if hours.include?(parse_time_unit)
end

#minutes?Boolean

Truthy if user has input a number of minutes.

Returns:

  • (Boolean)

See Also:



88
89
90
91
# File 'lib/termodoro/termodoro.rb', line 88

def minutes?
  minutes = %w[m min mins minute minutes]
  true if minutes.include?(parse_time_unit)
end

#parse_messageString

Looks into the user-supplied arguments and returns the message, if one is present. If not, sets the message to “Termodoro”.

Returns:

  • (String)

    the optional message given by the user.



48
49
50
51
52
# File 'lib/termodoro/termodoro.rb', line 48

def parse_message
  # .split(/[\d]+.[\w]+/).last 
  parsed_message = @arguments.split(/^\s*[\d]+\s*[\w]+/).last || 'Termodoro'
  self.message = parsed_message.strip
end

#parse_number_of_unitsInteger

Looks into the user-supplied arguments and parses out the number of units of time.

Returns:

  • (Integer)

    the number of units of time given by the user.



40
41
42
43
# File 'lib/termodoro/termodoro.rb', line 40

def parse_number_of_units
  number = @arguments.scan(/[\d]+/).first.strip.to_i
  self.number_of_units = number
end

#parse_time_unitString

Looks into the user-supplied arguments and retreives the unit of time. how the user inputs it (“s”, “Sec”, “seconds”, etc.)

Returns:

  • (String)

    a version of hours, minutes or seconds, depending on



32
33
34
35
# File 'lib/termodoro/termodoro.rb', line 32

def parse_time_unit
  segment = @arguments.match(/\D+/)[0].split(' ').first
  self.time_unit = segment
end

#seconds?Boolean

Truthy if user has input a number of seconds.

Returns:

  • (Boolean)

See Also:



80
81
82
83
# File 'lib/termodoro/termodoro.rb', line 80

def seconds?
  seconds = %w[s sec secs second seconds]
  true if seconds.include?(parse_time_unit)
end