Question

I'm using configatron to store my config values. I can access the config values without a problem, except when the scope is within a class's method.

I'm using configatron 3.0.0-rc1 with ruby 2.0.0

Here is the source I'm using in a single file called 'tc_tron.rb'

require 'configatron'

class TcTron  
  def simple(url)
    puts "-------entering simple-------"
    p url
    p configatron
    p configatron.url
    p configatron.database.server
    puts "-------finishing simple-------"
  end
end

# setup the configatron.  I assume this is a singleton
configatron.url = "this is a url string"
configatron.database.server = "this is a database server name"

# this should print out all the stuff in the configatron
p configatron
p configatron.url
p configatron.database.server

# create the object and call the simple method.
a = TcTron.new
a.simple("called URL")

# this should print out all the stuff in the configatron
p configatron
p configatron.url
p configatron.database.server

When I run the code, I get

{:url=>"this is a url string", :database=>{:server=>"this is a database server name"}}
"this is a url string"
"this is a database server name"
-------entering simple-------
"called URL"
{}
{}
{}
-------finishing simple-------
{:url=>"this is a url string", :database=>{:server=>"this is a database server name"}}
"this is a url string"
"this is a database server name"

Between the 'entering simple' and 'finishing simple' output I don't know why I'm not getting the configatron singleton.

What am I missing?

Was it helpful?

Solution

The current implementation of configatron is

module Kernel
  def configatron
    @__configatron ||= Configatron::Store.new
  end
end

from here

Since Kernel is included in Object that makes the method available in every object. However, b/c the method simply sets an instance variable, that store will only be available to each instance. Weird choice for a gem whose whole job is providing a globally accessible store.

In v2.4 they used a similar method to access a singleton, which probably worked much better

module Kernel
  # Provides access to the Configatron storage system.
  def configatron
    Configatron.instance
  end
end

from here

Looks like you could solve this yourself using require 'configatron/core' instead to avoid the monkey-patch, and provide your own singleton wrapper.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top