Sha256: 03838d0adee824ea723b65f5aabfb9b3c5bd381de8848baf636fdc11745847ef

Contents?: true

Size: 930 Bytes

Versions: 1

Compression:

Stored size: 930 Bytes

Contents

module Mina
  class Configuration
    include Singleton

    module DSL
      def self.included(base)
        [:set, :fetch, :remove, :set?, :ensure!, :reset!].each do |method|
          base.send :define_method, method do |*args,  &block|
            Configuration.instance.send(method, *args, &block)
          end
        end
      end
    end

    attr_reader :variables

    def initialize
      @variables ||= {}
    end

    def set(key, value = nil, &block)
      variables[key] = block || value
    end

    def fetch(key, default = nil)
      value = ENV[key.to_s] || variables.fetch(key, default)
      value.respond_to?(:call) ? value.call : value
    end

    def remove(key)
      variables.delete(key)
    end

    def set?(key)
      !variables.fetch(key, nil).nil?
    end

    def ensure!(key)
      fail "#{key} must be defined!" unless set?(key)
    end

    def reset!
      @variables = {}
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mina-1.0.0.beta2 lib/mina/configuration.rb