2

I recently replaced a home-grown configuration module with Configatron, but I'm unable to get one use case working.

When I attempt to use a configatron value as an argument to Object.const_get like this:

def formatter_class
  Object.const_get(configatron.formatter)
end

I get the following error:

file.rb:10:in `const_get': can't convert Configatron::Store to String 
  (Configatron::Store#to_str gives Configatron::Store) (TypeError)

The configatron assignment looks like this (simplified):

configatron.formatter = case
                          when condition?
                            'ExportFormat'
                          else
                            'ScreenFormat'
                        end

Even if I do configatron.formatter = 'ScreenFormat', I get the same error.

I've tried variations on the formatter_class method too. This fails:

def formatter_class
  Object.const_get(configatron['formatter'])
end

Of course, this succeeds, but won't fulfill my use case:

def formatter_class
  Object.const_get('ScreenFormat')
end

What am I doing wrong?

neontapir
  • 4,698
  • 3
  • 37
  • 52

2 Answers2

3

I solved my issue. Turns out you can call configatron.whatever and it will return a Configatron::Store if it's not initialized.

I inserted a call to configatron.has_key? 'formatter' before accessing the value. When it returned false, I figured out that the error was occurring in a code path where the value hadn't been initialized yet. Once I initialized the value, the error no longer occurs.

neontapir
  • 4,698
  • 3
  • 37
  • 52
1

Happens when .yml config file is missing. Or the key that you are looking for is not there.

Location: /config/NAME.yml

Sandip Subedi
  • 1,039
  • 1
  • 14
  • 34