Skip to content

Commit

Permalink
Adds better nil value handling for theme
Browse files Browse the repository at this point in the history
  • Loading branch information
nolantait committed Apr 9, 2024
1 parent de1e1b3 commit d894d1b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/protos/theme.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def key?(key)
end

def add(key, value)
return if value.nil?

@theme[key].add(value)
end

Expand All @@ -49,6 +51,8 @@ def remove(key, value)
end

def set(key, value)
return if value.nil?

@theme[key].clear.add(value)
end

Expand Down
4 changes: 4 additions & 0 deletions spec/protos/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
attrs = described_class.new(foo: "bar")
expect(attrs[:foo]).to eq("bar")
end

it "handles non-existent attributes" do
expect(subject[:foo]).to be_nil
end
end

describe "#merge" do
Expand Down
16 changes: 14 additions & 2 deletions spec/protos/theme_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,26 @@

expect(subject[:foo]).to eq("foo bar")
end

it "handles nil values" do
subject.add(:foo, nil)

expect(subject[:foo]).to be(nil)
end
end

describe "#remove" do
it "removes a class from the theme" do
theme = described_class.new(foo: "bar")
theme.remove(:foo, "bar")

expect(theme[:foo]).to eq(nil)
expect(theme[:foo]).to be(nil)
end

it "handles removing a non existing key" do
subject.remove(:foo, "bar")

expect(subject[:foo]).to eq(nil)
expect(subject[:foo]).to be(nil)
end
end

Expand All @@ -76,6 +82,12 @@

expect(subject[:foo]).to eq("baz")
end

it "handles nil values" do
subject.set(:foo, nil)

expect(subject[:foo]).to be_nil
end
end

describe "#[]" do
Expand Down

0 comments on commit d894d1b

Please sign in to comment.