title: Setting up configuration settings when writing a gem
body: <p>I'm writing a gem which I would like to work with and without the Rails environment.</p>
  
  <p>I have a <code>Configuration</code> class to allow configuration of the gem:</p>
  
  <pre><code>module NameChecker
    class Configuration
      attr_accessor :api_key, :log_level
  
      def initialize
        self.api_key = nil
        self.log_level = 'info'
      end
    end
  
    class &lt;&lt; self
      attr_accessor :configuration
    end
  
    def self.configure
      self.configuration ||= Configuration.new
      yield(configuration) if block_given?
    end
  end
  </code></pre>
  
  <p>This can now be used like so:</p>
  
  <pre><code>NameChecker.configure do |config|
    config.api_key = 'dfskljkf'
  end
  </code></pre>
  
  <p>However, I don't seem to be able to access my configuration variables from withing the other classes in my gem. For example, when I configure the gem in my <code>spec_helper.rb</code> like so:</p>
  
  <pre><code># spec/spec_helper.rb
  require "name_checker"
  
  NameChecker.configure do |config|
    config.api_key = 'dfskljkf'
  end
  </code></pre>
  
  <p>and reference the configuration from my code:</p>
  
  <pre><code># lib/name_checker/net_checker.rb
  module NameChecker
    class NetChecker
      p NameChecker.configuration.api_key
    end
  end
  </code></pre>
  
  <p>I get an undefined method error:</p>
  
  <pre><code>`&lt;class:NetChecker&gt;': undefined method `api_key' for nil:NilClass (NoMethodError)
  </code></pre>
  
  <p>What is wrong with my code?</p>