Skip to content

Commit

Permalink
Merge pull request #13 from Kaligo/maintenance/option-transform-value…
Browse files Browse the repository at this point in the history
…-guard

[Feature] Allow transform declaration with optional value
  • Loading branch information
Drenmi authored Jun 21, 2021
2 parents 5f6f1f7 + 21d41a8 commit 1fcc5a2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.6.1

### New features

- Allow transform declaration with optional value to `OptionsDeclaration`.

## 0.6.0

### New features
Expand Down
2 changes: 1 addition & 1 deletion lib/stimpack/options_declaration/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def default_value
end

def transformed_value(value)
transform? ? transform.to_proc.(value) : value
transform? && value ? transform.to_proc.(value) : value
end

def required?
Expand Down
2 changes: 1 addition & 1 deletion lib/stimpack/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Stimpack
VERSION = "0.6.0"
VERSION = "0.6.1"
end
21 changes: 17 additions & 4 deletions spec/stimpack/options_declaration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,33 @@
option :corge, transform: ->(value) { value.upcase }
option :grault, default: "bar", transform: ->(value) { value.upcase }
option :garply, default: "baz", transform: :upcase
option :waldo, required: false, transform: :to_sym
end
end

describe ".option" do
it { expect(service.options_configuration.size).to eq(9) }
it { expect(service.options_configuration.size).to eq(10) }
it { expect(service.options_configuration.values).to all(be_a(described_class::Option)) }

describe "private_reader (option)" do
let(:public_instance_methods) { service.public_instance_methods(false) }
let(:private_instance_methods) { service.private_instance_methods(false) }

it { expect(public_instance_methods).to contain_exactly(:baz) }
it { expect(private_instance_methods).to contain_exactly(:foo, :bar, :qux, :quux, :quuz, :corge, :grault, :garply) } # rubocop:disable Layout/LineLength
it { expect(private_instance_methods).to contain_exactly(:foo, :bar, :qux, :quux, :quuz, :corge, :grault, :garply, :waldo) } # rubocop:disable Layout/LineLength
end
end

describe ".options" do
it { expect(service.options).to contain_exactly(:foo, :bar, :baz, :qux, :quux, :quuz, :corge, :grault, :garply) }
it { expect(service.options).to contain_exactly(:foo, :bar, :baz, :qux, :quux, :quuz, :corge, :grault, :garply, :waldo) } # rubocop:disable Layout/LineLength
end

describe ".required_options" do
it { expect(service.required_options).to contain_exactly(:foo, :baz, :corge) }
end

describe ".optional_options" do
it { expect(service.optional_options).to contain_exactly(:bar, :qux, :quux, :quuz, :grault, :garply) }
it { expect(service.optional_options).to contain_exactly(:bar, :qux, :quux, :quuz, :grault, :garply, :waldo) }
end

describe ".default_options" do
Expand Down Expand Up @@ -110,6 +111,18 @@
context "when using a method name for transform" do
it { expect(instance.send(:garply)).to eq("BAZ") }
end

context "when transform is applied to optional" do
context "with no value provided" do
it { expect(instance.send(:waldo)).to eq(nil) }
end

context "with value provided" do
before { options.merge!(waldo: "fubaz") }

it { expect(instance.send(:waldo)).to eq(:fubaz) }
end
end
end
end

Expand Down

0 comments on commit 1fcc5a2

Please sign in to comment.