Class: Termodoro
- Inherits:
-
Object
- Object
- Termodoro
- 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
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#message ⇒ String
The message specified by the user.
-
#number_of_units ⇒ Integer
The number of units of time specified by the user.
-
#time_unit ⇒ String
The unit of time specified by the user.
Instance Method Summary collapse
-
#calculate_time ⇒ Integer
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.
-
#command ⇒ String
Workhorse method: Runs #calculate_time and #parse_message, taking the results of those methods and creating the string to execute.
-
#hours? ⇒ Boolean
Truthy if user has input a number of hours.
-
#initialize(arguments) ⇒ Termodoro
constructor
A new instance of Termodoro.
-
#minutes? ⇒ Boolean
Truthy if user has input a number of minutes.
-
#parse_message ⇒ String
Looks into the user-supplied arguments and returns the message, if one is present.
-
#parse_number_of_units ⇒ Integer
Looks into the user-supplied arguments and parses out the number of units of time.
-
#parse_time_unit ⇒ String
Looks into the user-supplied arguments and retreives the unit of time.
-
#seconds? ⇒ Boolean
Truthy if user has input a number of seconds.
Constructor Details
#initialize(arguments) ⇒ Termodoro
Returns a new instance of Termodoro.
25 26 27 |
# File 'lib/termodoro/termodoro.rb', line 25 def initialize(arguments) @arguments = arguments end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
9 10 11 |
# File 'lib/termodoro/termodoro.rb', line 9 def arguments @arguments end |
#message ⇒ String
Returns the message specified by the user.
10 11 12 |
# File 'lib/termodoro/termodoro.rb', line 10 def end |
#number_of_units ⇒ Integer
Returns 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_unit ⇒ String
Returns 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_time ⇒ Integer
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.
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 |
#command ⇒ String
Workhorse method: Runs #calculate_time and #parse_message, taking the results of those methods and creating the string to execute.
114 115 116 117 118 |
# File 'lib/termodoro/termodoro.rb', line 114 def command time_unit = calculate_time msg_part = "sleep #{time_unit} && terminal-notifier -message '#{msg_part}' -title 'Termodoro' & disown" end |
#hours? ⇒ Boolean
Truthy if user has input a number of hours.
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.
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_message ⇒ String
Looks into the user-supplied arguments and returns the message, if one is present. If not, sets the message to “Termodoro”.
48 49 50 51 52 |
# File 'lib/termodoro/termodoro.rb', line 48 def # .split(/[\d]+.[\w]+/).last = @arguments.split(/^\s*[\d]+\s*[\w]+/).last || 'Termodoro' self. = .strip end |
#parse_number_of_units ⇒ Integer
Looks into the user-supplied arguments and parses out the number of units of time.
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_unit ⇒ String
Looks into the user-supplied arguments and retreives the unit of time. how the user inputs it (“s”, “Sec”, “seconds”, etc.)
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.
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 |