Sha256: d10f61a14308af14c606d60080aad5481fa0fd3daf3407d2654b8f58d487275f

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

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.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

1 entries across 1 versions & 1 rubygems

Version Path
mina-1.2.5 lib/mina/configuration.rb