Skip to content

Commit

Permalink
feat: Allow only true/false for boolean variants (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv authored Sep 29, 2024
1 parent 7e60cec commit 7f92459
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/phlex/variants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def build_variants_style(variants)

next value if value

# doesn't raise error when passing false for variant with only true/:yes option
# doesn't raise error when passing false for variant with only true option
next if option == false && options.has_key?(true)
end

Expand Down Expand Up @@ -138,12 +138,13 @@ def initialize(view_class, variant_name)

def method_missing(name, *args) # standard:disable Style/MissingRespondToMissing
option = name.to_sym
view_class::STYLE_VARIANTS[variant_name][option] = args

if option == :yes
view_class::STYLE_VARIANTS[variant_name][true] = args
elsif option == :no
view_class::STYLE_VARIANTS[variant_name][false] = args
else
view_class::STYLE_VARIANTS[variant_name][option] = args
end
end
end
Expand Down
15 changes: 13 additions & 2 deletions spec/phlex/variants_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,20 @@ def view_template

expect(example.build_style).to eq "btn-normal"
expect(example.build_style(outline: true, full: false)).to eq "btn-normal btn-outline btn-fit"
expect(example.build_style(loading: :yes, full: :no)).to eq "btn-loading btn-fit"

expect do
example.build_style(loading: :yes)
end.to raise_error("Option :yes for :loading variant doesn't exist. Valid options are: [true, false]")
end

it "doesn't raise error when passing false for a variant with only true/:yes options" do
it "doesn't raise error when passing false for a boolean variant without 'false' option" do
example = phlex_class do
style do
variants do
color do
red "btn-red"
end

outline do
yes "btn-outline"
end
Expand All @@ -211,6 +218,10 @@ def view_template
end

expect(example.build_style(outline: false)).to eq ""

expect do
example.build_style(color: false)
end.to raise_error("Option false for :color variant doesn't exist. Valid options are: [:red]")
end

it "ignores nil variants" do
Expand Down

0 comments on commit 7f92459

Please sign in to comment.