Class: Termodoro

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

Overview

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

Constants included from Version

Version::VERSION

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.



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

def initialize(arguments)
  @arguments = arguments
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



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

def arguments
  @arguments
end

#messageString

Returns the message specified by the user.

Returns:

  • (String)

    the message specified by the user.



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

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.



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

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.



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

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:



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

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:



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

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:



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

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:



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

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.



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

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.



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

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



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

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:



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

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