Skip to content

Commit

Permalink
Add Calendar::Event time validation for protocol 7.
Browse files Browse the repository at this point in the history
  • Loading branch information
synthead committed Aug 13, 2023
1 parent 0e21173 commit 9f6ebb2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/timex_datalink_client/protocol_7/eeprom/calendar/event.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
# frozen_string_literal: true

require "active_model"

require "timex_datalink_client/helpers/lsb_msb_formatter"

class TimexDatalinkClient
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<Integer>] Phrase for event.
# @raise [ActiveModel::ValidationError] One or more model values are invalid.
# @return [Event] Event instance.
def initialize(time:, phrase:)
@time = time
@phrase = 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9f6ebb2

Please sign in to comment.