From 9f6ebb2c2c276e477c37bbd05a2463a752a1a96e Mon Sep 17 00:00:00 2001 From: Maxwell Pray Date: Sat, 12 Aug 2023 17:21:35 -0700 Subject: [PATCH 1/3] Add Calendar::Event time validation for protocol 7. --- .../protocol_7/eeprom/calendar/event.rb | 15 ++++++++++++++- .../protocol_7/eeprom/calendar/event_spec.rb | 11 +++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/timex_datalink_client/protocol_7/eeprom/calendar/event.rb b/lib/timex_datalink_client/protocol_7/eeprom/calendar/event.rb index 6cc1a33..fcf2fda 100644 --- a/lib/timex_datalink_client/protocol_7/eeprom/calendar/event.rb +++ b/lib/timex_datalink_client/protocol_7/eeprom/calendar/event.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "active_model" + require "timex_datalink_client/helpers/lsb_msb_formatter" class TimexDatalinkClient @@ -7,16 +9,23 @@ class Protocol7 class Eeprom class Calendar class Event + include ActiveModel::Validations include Helpers::LsbMsbFormatter FIVE_MINUTES_SECONDS = 300 - attr_accessor :time, :phrase + validates :time, comparison: { + greater_than_or_equal_to: :device_time, + message: "%{value} must be greater than device time!" + } + + attr_accessor :time, :phrase, :device_time # Create an Event instance. # # @param time [::Time] Time of event. # @param phrase [Array] Phrase for event. + # @raise [ActiveModel::ValidationError] One or more model values are invalid. # @return [Event] Event instance. def initialize(time:, phrase:) @time = time @@ -24,6 +33,10 @@ def initialize(time:, phrase:) end def time_formatted(device_time) + @device_time = device_time + + validate! + device_time_midnight = Time.new(device_time.year, device_time.month, device_time.day) seconds = (time - device_time_midnight).to_i five_minutes = seconds / FIVE_MINUTES_SECONDS diff --git a/spec/lib/timex_datalink_client/protocol_7/eeprom/calendar/event_spec.rb b/spec/lib/timex_datalink_client/protocol_7/eeprom/calendar/event_spec.rb index d303214..1a29ae4 100644 --- a/spec/lib/timex_datalink_client/protocol_7/eeprom/calendar/event_spec.rb +++ b/spec/lib/timex_datalink_client/protocol_7/eeprom/calendar/event_spec.rb @@ -43,5 +43,16 @@ it { should eq([0x7e, 0x0f]) } end + + context "when time is 2021-12-10 15:28:15" do + let(:time) { Time.new(2021, 12, 23, 18, 30, 20) } + + it do + expect { phrase_value }.to raise_error( + ActiveModel::ValidationError, + "Validation failed: Time 2021-12-23 18:30:20 -0800 must be greater than device time!" + ) + end + end end end From 2e58e4867dfa09609974a0231d6c011c9bae5fc5 Mon Sep 17 00:00:00 2001 From: Maxwell Pray Date: Sat, 12 Aug 2023 17:53:08 -0700 Subject: [PATCH 2/3] Use UTC in protocol 7 calendar event validation spec. --- .../protocol_7/eeprom/calendar/event_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/lib/timex_datalink_client/protocol_7/eeprom/calendar/event_spec.rb b/spec/lib/timex_datalink_client/protocol_7/eeprom/calendar/event_spec.rb index 1a29ae4..a7d1f74 100644 --- a/spec/lib/timex_datalink_client/protocol_7/eeprom/calendar/event_spec.rb +++ b/spec/lib/timex_datalink_client/protocol_7/eeprom/calendar/event_spec.rb @@ -45,12 +45,12 @@ end context "when time is 2021-12-10 15:28:15" do - let(:time) { Time.new(2021, 12, 23, 18, 30, 20) } + let(:time) { Time.utc(2021, 12, 23, 18, 30, 20) } it do expect { phrase_value }.to raise_error( ActiveModel::ValidationError, - "Validation failed: Time 2021-12-23 18:30:20 -0800 must be greater than device time!" + "Validation failed: Time 2021-12-23 18:30:20 UTC must be greater than device time!" ) end end From 76fd686848b5e28e7b8866cf3c05b78ab0e97eb2 Mon Sep 17 00:00:00 2001 From: Maxwell Pray Date: Sat, 12 Aug 2023 17:55:20 -0700 Subject: [PATCH 3/3] Use "greater or equal to" in validation for protocol 7 event. --- lib/timex_datalink_client/protocol_7/eeprom/calendar/event.rb | 2 +- .../protocol_7/eeprom/calendar/event_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/timex_datalink_client/protocol_7/eeprom/calendar/event.rb b/lib/timex_datalink_client/protocol_7/eeprom/calendar/event.rb index fcf2fda..33285c5 100644 --- a/lib/timex_datalink_client/protocol_7/eeprom/calendar/event.rb +++ b/lib/timex_datalink_client/protocol_7/eeprom/calendar/event.rb @@ -16,7 +16,7 @@ class Event validates :time, comparison: { greater_than_or_equal_to: :device_time, - message: "%{value} must be greater than device time!" + message: "%{value} must be greater or equal to device time!" } attr_accessor :time, :phrase, :device_time diff --git a/spec/lib/timex_datalink_client/protocol_7/eeprom/calendar/event_spec.rb b/spec/lib/timex_datalink_client/protocol_7/eeprom/calendar/event_spec.rb index a7d1f74..aa5ff21 100644 --- a/spec/lib/timex_datalink_client/protocol_7/eeprom/calendar/event_spec.rb +++ b/spec/lib/timex_datalink_client/protocol_7/eeprom/calendar/event_spec.rb @@ -50,7 +50,7 @@ it do expect { phrase_value }.to raise_error( ActiveModel::ValidationError, - "Validation failed: Time 2021-12-23 18:30:20 UTC must be greater than device time!" + "Validation failed: Time 2021-12-23 18:30:20 UTC must be greater or equal to device time!" ) end end