module ClockFlipCommand
ClockFlipCommand — parse !time show/add commands for flip clock meanings. Public: usage_message, parse_command Depends: ClockFlipStore Tests: test/plugins/clock_test.rb, test/lib/clock_flip_command_test.rb
Public Instance Methods
Source
# File lib/clock/flip_command.rb, line 77 def build_add_result(parsed, abbrev:, meaning:) { action: :add, hour: parsed[:hour], minute: parsed[:minute], time_label: parsed[:time_label], abbrev: abbrev, meaning: meaning } end
Source
# File lib/clock/flip_command.rb, line 28 def parse_add_prefixed(text) body = text.to_s.strip return { action: :error, error: usage_message } if body.empty? time_token, remainder = body.split(/\s+/, 2) parsed = ClockFlipStore.parse_time_label(time_token) unless parsed return { action: :error, error: "Invalid time — use HH:MM or H.MM (e.g. 7:50)." } end remainder = remainder.to_s.strip return { action: :error, error: "Usage: !time add #{parsed[:time_label]} <meaning>" } if remainder.empty? abbrev, meaning = split_abbrev_and_meaning(parsed[:hour], parsed[:minute], remainder) return { action: :error, error: "Usage: !time add #{parsed[:time_label]} <meaning>" } if meaning.empty? build_add_result(parsed, abbrev: abbrev, meaning: meaning) end
Source
# File lib/clock/flip_command.rb, line 17 def parse_command(args) text = args.to_s.strip return { action: :error, error: usage_message } if text.empty? if text.match?(/\Aadd\s+/i) return parse_add_prefixed(text.sub(/\Aadd\s+/i, "")) end parse_standard(text) end
Source
# File lib/clock/flip_command.rb, line 47 def parse_standard(text) parts = text.split(/\s+/, 3) parsed = ClockFlipStore.parse_time_label(parts[0]) unless parsed return { action: :error, error: "Invalid time — use HH:MM or H.MM (e.g. 7:50)." } end if parts.length == 1 return { action: :show, hour: parsed[:hour], minute: parsed[:minute], time_label: parsed[:time_label] } end meaning = parts[2].to_s.strip if meaning.empty? return { action: :error, error: "Usage: !time #{parsed[:time_label]} <abbrev> <meaning>" } end build_add_result(parsed, abbrev: parts[1], meaning: meaning) end
Source
# File lib/clock/flip_command.rb, line 66 def split_abbrev_and_meaning(hour, minute, meaning_text) candidate, rest = meaning_text.split(/\s+/, 2) token = candidate.to_s.strip.downcase if rest && ClockFlipStore.valid_flip_word?(hour, minute, token) [token, rest.strip] else [ClockFlipStore.expected_flip_word(hour, minute), meaning_text.strip] end end
Source
# File lib/clock/flip_command.rb, line 13 def usage_message "Usage: !time <HH:MM> — !time <HH:MM> <abbrev> <meaning> — !time add <HH:MM> <meaning>" end