Module: Dsu::Support::CommandOptions::TimeMnemonic
- Includes:
- TimeMnemonics
- Included in:
- CLI, Dsu::Subcommands::Browse, Dsu::Subcommands::Delete, Dsu::Subcommands::Export, Dsu::Subcommands::Import, Dsu::Subcommands::List
- Defined in:
- lib/dsu/support/command_options/time_mnemonic.rb
Overview
The purpose of this module is to take a command option that is a string and return a Time object. The command option is expected to be a time mneumoic.
Constant Summary
Constants included from TimeMnemonics
Dsu::Support::CommandOptions::TimeMnemonics::RELATIVE_REGEX, Dsu::Support::CommandOptions::TimeMnemonics::TODAY, Dsu::Support::CommandOptions::TimeMnemonics::TOMORROW, Dsu::Support::CommandOptions::TimeMnemonics::YESTERDAY
Class Method Summary collapse
-
.relative_time_mnemonic?(mnemonic) ⇒ Boolean
This method returns true if mnemonic is a valid relative time mnemonic.
- .time_from_mnemonic(command_option:, relative_time: nil) ⇒ Object
-
.time_from_mnemonic!(command_option:, relative_time: nil) ⇒ Object
command_option: is expected to me a time mnemonic.
-
.time_mnemonic?(mnemonic) ⇒ Boolean
This method returns true if mnemonic is a valid mnemonic OR a relative time mnemonic.
Class Method Details
.relative_time_mnemonic?(mnemonic) ⇒ Boolean
This method returns true if mnemonic is a valid relative time mnemonic.
44 45 46 47 48 |
# File 'lib/dsu/support/command_options/time_mnemonic.rb', line 44 def relative_time_mnemonic?(mnemonic) return false unless mnemonic.is_a?(String) mnemonic.match?(RELATIVE_REGEX) end |
.time_from_mnemonic(command_option:, relative_time: nil) ⇒ Object
15 16 17 18 19 |
# File 'lib/dsu/support/command_options/time_mnemonic.rb', line 15 def time_from_mnemonic(command_option:, relative_time: nil) time_from_mnemonic!(command_option: command_option, relative_time: relative_time) rescue ArgumentError nil end |
.time_from_mnemonic!(command_option:, relative_time: nil) ⇒ Object
command_option: is expected to me a time mnemonic. If relative_time is NOT nil, all time mnemonics are relative to relative_time. Otherwise, they are relative to Time.now. relative_time: is a Time object that is required IF command_option is expected to be a relative time mnemonic. Otherwise, it is optional.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/dsu/support/command_options/time_mnemonic.rb', line 25 def time_from_mnemonic!(command_option:, relative_time: nil) validate_argument!(command_option: command_option, command_option_name: :command_option) unless relative_time.nil? || relative_time.is_a?(::Time) raise ArgumentError, "relative_time is not a Time object: \"#{relative_time}\"" end relative_time ||= ::Time.now time_for_mnemonic(mnemonic: command_option, relative_time: relative_time) end |
.time_mnemonic?(mnemonic) ⇒ Boolean
This method returns true if mnemonic is a valid mnemonic OR a relative time mnemonic.
38 39 40 |
# File 'lib/dsu/support/command_options/time_mnemonic.rb', line 38 def time_mnemonic?(mnemonic) mnemonic?(mnemonic) || relative_time_mnemonic?(mnemonic) end |