palkan/anyway_config

Automatic predicate methods for coerced booleans

jhirn opened this issue · 4 comments

jhirn commented

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

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

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:

new_defaults.each do |key, val|
next unless val.is_a?(TrueClass) || val.is_a?(FalseClass)
alias_method :"#{key}?", :"#{key}"
end

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

jhirn commented

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

Thank you!