Sha256: e0f36d75e1c28d7d451102dc0021cae34e3c5e47883169b144add5fbbbd122bb

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module Grenache
  class BaseConfiguration
    def values
      @values ||= {}
    end

    def method_missing(name, *args, &block)
      if name[-1] == "="
        values[name[0,name.size-1]] = args.first
      else
        values[name.to_s]
      end
    end
  end

  class Configuration < BaseConfiguration

    def initialize(params = {})
      @values = self.class.default.values

      params.each do |k, v|
        @values[k.to_s] = v
      end

      # sanitize urls
      if not @values["grape_address"].end_with?("/")
        @values["grape_address"] = @values["grape_address"] + "/"
      end
    end

    def self.set_default &block
      yield default
    end

    def self.default
      @defaults ||= BaseConfiguration.new
    end

  end

  # Configuration helpers
  module Configurable
    def self.included(base)
      base.extend(ClassMethods)
    end

    # Instance configuration, can be altered indipendently
    def config
      @configuration ||= Configuration.new
    end

    module ClassMethods
      def configure
        yield config
      end

      # Class configuration
      def config
        @configuration ||= Configuration.new
      end

      def default_conf &block
        Grenache::Configuration.set_default &block
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grenache-ruby-base-0.2.17 lib/grenache/configurable.rb