diff --git a/lib/trmnl/api/models/palette.rb b/lib/trmnl/api/models/palette.rb index eb8969a..51ee2d8 100644 --- a/lib/trmnl/api/models/palette.rb +++ b/lib/trmnl/api/models/palette.rb @@ -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 diff --git a/spec/lib/trmnl/api/models/palette_spec.rb b/spec/lib/trmnl/api/models/palette_spec.rb index b56daa7..a920236 100644 --- a/spec/lib/trmnl/api/models/palette_spec.rb +++ b/spec/lib/trmnl/api/models/palette_spec.rb @@ -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