module Configurations::Configurable

Module configurable provides the API of configurations

Public Instance Methods

included(base) click to toggle source

Once included, Configurations installs three methods in the host module: configure, configuration_defaults and configurable

# File lib/configurations/configurable.rb, line 12
def included(base)
  install_configure_in(base)
  base.instance_eval do
    extend ClassMethods

    # call configuration_mutex once to initialize the value
    #
    initialize_configuration!
  end
end
install_configure_in(base) click to toggle source

Installs configure in base, and makes sure that it will instantiate configuration as a subclass of the host module

# File lib/configurations/configurable.rb, line 34
    def install_configure_in(base)
      base.instance_eval <<-EOF
        # Configuration class for host module
        #
        #{base.name}::Configuration = Class.new(Configurations::Configuration)

        # The central configure method
        # @params [Proc] block the block to configure host module with
        # @raise [ArgumentError] error when not given a block
        # @example Configure a configuration
        #   MyGem.configure do |c|
        #     c.foo = :bar
        #   end
        #
        def self.configure(&block)
          semaphore.synchronize do
            fail ArgumentError, "configure needs a block" unless block_given?
            include_configuration_type!(#{base.name}::Configuration)

            set_configuration!(&block)
          end
        end

        # A reader for Configuration
        #
        def configuration
          semaphore.synchronize do
            return @configuration if @configuration

            if @configuration_defaults
              include_configuration_type!(#{base.name}::Configuration)
              set_configuration! { }
            end
          end
        end


        private

        # Sets the configuration instance variable
        #
        def self.set_configuration!(&block)
          @configuration = #{base.name}::Configuration.__new__(
                                                      configuration_options,
                                                      &block
                                                    )
        end

        @semaphore = Mutex.new
        def self.semaphore
          @semaphore
        end

      EOF
    end
underscore_camelized(string) click to toggle source
# File lib/configurations/configurable.rb, line 23
def underscore_camelized(string)
  string.gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr('-', '_')
    .downcase
end