Added palette model gray and color defaults

Necessary to provide typed defaults when attributes are missing. This is especially important when the API answers `nil` values since the API doesn't provide safe defaults.

Milestone: minor
This commit is contained in:
Brooke Kuhlmann
2026-03-18 12:02:50 -06:00
parent aeb68b32b3
commit 39a753f215
2 changed files with 35 additions and 10 deletions
+7
View File
@@ -10,6 +10,13 @@ module TRMNL
using Refinements::Hash
def self.for(attributes) = new(**attributes.transform_keys(name: :label, id: :name))
def initialize(**)
super
self[:grays] ||= 0
self[:colors] ||= []
freeze
end
end
end
end
+28 -10
View File
@@ -3,9 +3,21 @@
require "spec_helper"
RSpec.describe TRMNL::API::Models::Palette do
subject(:model) { described_class.new }
let :attributes do
{
name: "test",
label: "Test",
grays: 2,
colors: %w[#000000 #FFFFFF],
framework_class: "screen--1bit"
}
end
describe ".for" do
it "answers record for attributes" do
attributes = {
api_attributes = {
id: "test",
name: "Test",
grays: 2,
@@ -13,15 +25,21 @@ RSpec.describe TRMNL::API::Models::Palette do
framework_class: "screen--1bit"
}
expect(described_class.for(attributes)).to eq(
described_class[
name: "test",
label: "Test",
grays: 2,
colors: %w[#000000 #FFFFFF],
framework_class: "screen--1bit"
]
)
expect(described_class.for(api_attributes)).to eq(described_class[**attributes])
end
end
describe "#initalize" do
it "answers default attributes" do
expect(model).to have_attributes(grays: 0, colors: [])
end
it "answers custom attributes" do
expect(described_class[**attributes]).to have_attributes(attributes)
end
it "is frozen" do
expect(model.frozen?).to be(true)
end
end
end