markbates / configatron

A super cool, simple, and feature rich configuration system for Ruby apps.

Home Page:http://www.metabates.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Non-existent options should return nil rather than Configatron::Store

TylerRick opened this issue · comments

The problem with returning an instance of Configatron::Store, as you probably already know, is that you can't do var = configatron.some_option || 'default', where some_option may or may not exist.

Because even if it does exist, a Configatron::Store object will be returned, which is truthy and therefore the || 'default' never has any effect.

Worse yet, when we try to use the value, we will inevitably get NoMethodErrors, because Configatron::Store does not act like a string or a Fixnum or any of the data types we are probably expecting our value to be.

I don't THINK there is any way to make objects of a certain class so they are not truthy (that would sure be handy though, but you can't override the logical operators like ||), so the only solution I can think of is to have configatron return (for options that are not found) a special nil object that had a method_missing added that proxied the message on to the Store instance but otherwise behaved like a normal nil. Do you think that could work?

That sounds good in theory, would have to play around with it to see if it would work in practice.

In the meantime have you tried the retrieve method?

configatron.i.do.exist = [:some, :array]
configatron.i.dont.retrieve(:exist, nil) # => nil
configatron.i.do.retrieve(:exist, :foo) # => [:some, :array]

It will set defaults if it doesnt exist.

Thanks for the reply. I had not tried the retrieve method. I will definitely give it a try.

It's good to know something like that exists, although I still think

configatron.i.dont.exist || :foo

would be nicer if we could get it to work :)

I agree that the Configatron::Store should never be returned. It is a huge source of mistake that's hard to track down.

Also, on the other hand, where the configatron is used, there should be no need to provide a default as it can/should be done during configuration (not when it is used) by setting nil to that particular option:

configatron.it.is_optional = nil

As a result I believe we only one thing - strict retrieval mode - this would allow configuration option to be mandatory. So if it is not present - error is raised.

And its implementation is very easy.

configatron.it.is.required! # "bang" MUST throw if the option is NOT present

this would ensure that the required option has been provided correctly (nil is also a correct value).

+1 This is a very odd behavior to return an unusable object when there is no configuration.

+1 any progress on this?

This really can't be done. This is how configatron works. To do this would mean some serious monkey patching of NilClass and that would have some very serious repercussions in almost every application.

if someone out there has a great solution for this, I'm all ears. For now I'm closing this ticket because it's just not feasible.

Sorry.

How about #44

Agree, #44 would cover many use cases for this issue.