Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Calendar::Event time validation for protocol 7 #322

Merged
merged 3 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 or equal to 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.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 UTC must be greater or equal to device time!"
)
end
end
end
end