Wednesday 27 November 2013

Configuration in ruby gem

This article tells about How to set default configuration in your ruby gem and how to override default configuration.

Suppose you want to set default parameters for gem like environment, version, access_token, etc which will be set inside gem. At the same time You have created initialize in rails application from which you  want to achieve custom configuration. Custom configuration may or mayn't overcome default configuration as per our requirement.

Lets consider, your gem have following structure



gem_name
    |-- Gemfile
    |-- lib
    |   |-- generators
    |   |   `-- gem_name
    |   |       |-- install_generator.rb
    |   |       `-- templates
    |   |           `-- initializer.rb
    |   |-- gem_name
    |   |   |-- configuration.rb
    |   |   |-- test_file1.rb
    |   |   |-- test_file2.rb
    |   |   `-- version.rb
    |   `-- gem_name.rb
    |-- LICENSE.txt
    |-- gem_name-1.0.0.gem
    |-- gem_name.gemspec
    |-- Rakefile
    `-- README.md

1) Set Default Configuration

create  file with name configuration.rb inside lib/gem_name. So the gem structure will be 

gem_name
    |-- lib
    |   |-- gem_name
    |   |   |-- configuration.rb
    |   |   |-- test_file1.rb
    |   |   |-- test_file2.rb
    |   |   `-- version.rb
    |   `-- gem_name.rb
    |

Copy following content to configuration.rb file



    module GemName
      class Configuration

        attr_accessor :access_token
        attr_accessor :endpoint

        DEFAULT_ENDPOINT = 'http://example.com'

        def initialize
          @endpoint = DEFAULT_ENDPOINT
        end
      end
    end



Above code will create one configuration class with setting getter, setter method for our configuration parameters.

We can also define constant as DEFAULT_ENDPOINT
Initialize method will initialize class object with default parameter value. This object of configuration class will act as building block of configuration of gem.

2) Create configuration object

Open gem_name.rb. It will have content something like below


    class GemName

      def method1
      end
  
      def method2
      end
    end


Now create configuration method which will create configuration object



    class GemName

      def configuration
          @configuration ||= Configuration.new
      end

      def method1
      end
  
      def method2
      end
    end



Well you have created the configuration object with all the default parameter.
So, access parameter inside gem_name.rb  Just use

@configuration.endpoint

It will give default value of parameter endpoint written in configuration class initialize method.
Here it is 'http://example.com'

3) Set Custom configuration from initializer inside rails application

Now we will allow application to write inside our configuration object which we created above.
So we will use
 
  attr_writer :configuration

Then create anoter action configure method in GemName class which will write data in configuration object

So, GemName class looks like



    class GemName

       attr_writer :configuration

        def configure
           yield(configuration)
         end

       def configuration
           @configuration ||= Configuration.new
       end

       def method1
       end
  
       def method2
       end
    end



That's over from Gem.

Now inside initializer in rails application just past below code


GemName.configure do |config|
  config.access_token = "1234567896"
end



Well everything is fine.

Now access custom configuration in gem_name.rb with

configuration.access_token

which will return value of access_token from initializer.

That's it...

No comments:

Post a Comment