Sha256: e82586263fc9084d54bc0a50a2a9c0cebf467652b21ec6409fdc4652df4e0750

Contents?: true

Size: 1.01 KB

Versions: 7

Compression:

Stored size: 1.01 KB

Contents

module Mina
  class Configuration
    include Singleton
    include Helpers::Internal

    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)
      ENV.has_key?(key.to_s) || !variables.fetch(key, nil).nil?
    end

    def ensure!(key, message: nil)
      return if set?(key)
      message ||= "#{key} must be defined!"
      error! message
    end

    def reset!
      @variables = {}
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mina-1.2.4 lib/mina/configuration.rb
mina-1.2.3 lib/mina/configuration.rb
mina-1.2.2 lib/mina/configuration.rb
mina-1.2.1 lib/mina/configuration.rb
mina-1.2.0 lib/mina/configuration.rb
mina-1.1.1 lib/mina/configuration.rb
mina-1.1.0 lib/mina/configuration.rb