From 7f9245933d2ce62c76618df97e878cdefe93d45c Mon Sep 17 00:00:00 2001 From: stephann <3025661+stephannv@users.noreply.github.com> Date: Sun, 29 Sep 2024 14:43:12 -0300 Subject: [PATCH] feat: Allow only true/false for boolean variants (#10) --- lib/phlex/variants.rb | 5 +++-- spec/phlex/variants_spec.rb | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/phlex/variants.rb b/lib/phlex/variants.rb index 16f58fc..283dd42 100644 --- a/lib/phlex/variants.rb +++ b/lib/phlex/variants.rb @@ -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 @@ -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 diff --git a/spec/phlex/variants_spec.rb b/spec/phlex/variants_spec.rb index 78e78f4..859ff25 100644 --- a/spec/phlex/variants_spec.rb +++ b/spec/phlex/variants_spec.rb @@ -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 @@ -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