Skip to content

Suggestion: Unset keys should be falsy #98

@DannyBen

Description

@DannyBen

I love configatron, but although it solves a lot of things, it makes it rather difficult to work with unassigned keys.

In a standard hash, or an OpenStruct, non existing values are falsy:

o = OpenStruct.new({ one: '1', two: '2' })
p o.three
# => nil

While in configatron they are truthy:

# configatron.allow = false   # unset
p configatron.allow ? "allowed" : "not allowed"
# => "allowed" (but expected "not allowed")

Even if I want to set defaults, I cannot work with the common ruby syntax:

configatron.allow ||= false

And instead, I can only do:

configatron.allow = false unless configatron.has_key? :allow

And in cases where I can't (or prefer to avoid) having defaults, in order to allow "open schema", the only way for me to handle unset keys in the same way as falsy keys, is:

allowed = configatron.has_key?(:allow) ? configatron.allow : false
p allowed ? "allowed" : "not allowed"

Am I missing some functionality in configatron that will make this easier?

If there is no way to alleviate the above issues, I would love to see a change that allows it - even if it will require me to opt-in to this behavior using some config directive (in case it is desired to avoid introducing a breaking change).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions