Class: Termodoro
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
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.
26 27 28 |
# File 'lib/termodoro/termodoro.rb', line 26 def initialize(arguments) @arguments = arguments end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
10 11 12 |
# File 'lib/termodoro/termodoro.rb', line 10 def arguments @arguments end |
#message ⇒ String
Returns the message specified by the user.
11 12 13 |
# File 'lib/termodoro/termodoro.rb', line 11 def @message end |
#number_of_units ⇒ Integer
Returns 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_unit ⇒ String
Returns 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_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.
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 |
#command ⇒ String
Workhorse method: Runs #calculate_time and #parse_message, taking the results of those methods and creating the string to execute.
115 116 117 118 119 |
# File 'lib/termodoro/termodoro.rb', line 115 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.
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.
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_message ⇒ String
Looks into the user-supplied arguments and returns the message, if one is present. If not, sets the message to “Termodoro”.
49 50 51 52 53 |
# File 'lib/termodoro/termodoro.rb', line 49 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.
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_unit ⇒ String
Looks into the user-supplied arguments and retreives the unit of time. how the user inputs it (“s”, “Sec”, “seconds”, etc.)
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.
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 |