palkan / anyway_config

Configuration library for Ruby gems and applications
MIT License
778 stars 52 forks source link

Automatic predicate methods for coerced booleans #114

Closed jhirn closed 1 year ago

jhirn commented 1 year ago

Given a config with a coerced boolean, it would be really nice to have predicate methods generated automatically. I'm thinking this is something that you might want to disable rather than opt-in to, but I'd be fine with either way.

class MyConfig < Anyway::Config
  attr_config enabled: "true"
  coerce_boolean enabled: :boolean
end

MyConfig.new.enabled? #=> true

# config
config.anyway_config.predicates_for_coerced_booleans = [false | true]
jhirn commented 1 year ago

If it's wanted. I can try to put up a PR for this.

palkan commented 1 year ago

Hey!

We already create predicate methods when the default value is true or false (so, in you case enabled: true will ad the #enabled?) method: https://github.com/palkan/anyway_config/blob/83d79ccaf5619889c07c8ecdf8d66dcb22c9dc05/lib/anyway/config.rb#L105-L108

Anyway, I think, taking defined corrections into account also makes sense, so I'm open for a PR

jhirn commented 1 year ago

Oh nice. Yeah I'm assuming that may not work for ENVs?

coerce_types is the final say in the type so adding it there would be a nice addition. As soon as I'm done converting this 6yr old legacy app to AnywayConfig I'll take a stab at a PR.

jhirn commented 1 year ago

Thank you!