Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension option to support Undefined as default value #141

Merged
merged 4 commits into from
Sep 26, 2022

Conversation

timriley
Copy link
Member

@timriley timriley commented Sep 22, 2022

If you include or extend dry-configurable like so:

extend Dry::Configurable(default_undefined: true)

Then Undefined will become the default value for all settings instead of nil (when no setting-specific default is provided).

When a setting has no value and Undefined is configured as the default, then the constructor will not be called.

See my initial spec to demonstrate all of this in a nutshell:

it "support Undefined as a default" do
  klass = Class.new do
    extend Dry::Configurable(default_undefined: true)

    setting :foo
    setting :bar, constructor: -> v { v.upcase }
    setting :baz, default: "baz", constructor: -> v { v.upcase }
  end

  expect(klass.config.foo).to be(Dry::Configurable::Undefined)
  expect(klass.config.bar).to be(Dry::Configurable::Undefined)
  expect(klass.config.baz).to eq "BAZ"
end

TBH, I think this PR is basically ready, apart from fleshing out that spec a little bit. I would love feedback at this point!

Resolves #108.

Copy link
Member

@solnic solnic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 ❤️

options = options.merge(constructor: block)
block = nil
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh boy I am so happy to see this go away

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, you and me both

Copy link
Member

@flash-gordon flash-gordon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@timriley timriley force-pushed the nil-or-undefined-default-value branch from ca4e6f7 to a925ee9 Compare September 26, 2022 20:15
@timriley timriley merged commit 22ab1fa into main Sep 26, 2022
@timriley timriley deleted the nil-or-undefined-default-value branch September 26, 2022 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Leave setting values as Undefined rather than coercing to nil for un-set settings
3 participants