mirror of
https://github.com/usetrmnl/trmnl-api.git
synced 2026-04-29 13:35:13 -07:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user