class RevbitsPamCicd::Configuration

Attributes

computed[R]
explicit[R]
supplied[R]

Public Class Methods

accepted_options() click to toggle source
# File lib/revbits_pam_cicd/configuration.rb, line 41
def accepted_options
  require 'set'
  @options ||= Set.new
end
add_option(name, options = {}, &def_proc) click to toggle source
# File lib/revbits_pam_cicd/configuration.rb, line 46
def add_option(name, options = {}, &def_proc)
  accepted_options << name
  allow_env = options[:env].nil? || options[:env]
  env_var = options[:env] || "REVBITS_#{name.to_s.upcase}"
  def_val = options[:default]
  opt_name = name

  def_proc ||= if def_val.respond_to?(:call)
                 def_val
               elsif options[:required]
                 proc { raise "Missing required option #{opt_name}" }
               else
                 proc { def_val }
               end

  convert = options[:convert] || ->(x) { x }
  convert = convert.to_proc if convert.respond_to?(:to_proc)

  define_method("#{name}=") do |value|
    set name, value
  end

  define_method("#{name}_env_var") do
    allow_env ? env_var : nil
  end

  define_method(name) do
    value = computed[name]
    return value unless value.nil?

    if supplied.member?(name)
      supplied[name]
    elsif allow_env && ENV.member?(env_var)
      instance_exec(ENV[env_var], &convert)
    else
      instance_eval(&def_proc)
    end.tap do |value|
      computed[name] = value
    end
  end

  alias_method("#{name}?", name) if options[:boolean]
end
new(options = {}) click to toggle source
# File lib/revbits_pam_cicd/configuration.rb, line 34
def initialize(options = {})
  @explicit = options.dup
  @supplied = options.dup
  @computed = Hash.new
end

Public Instance Methods

clone(override_options = {}) click to toggle source
# File lib/revbits_pam_cicd/configuration.rb, line 91
def clone(override_options = {})
  self.class.new self.explicit.dup.merge(override_options)
end
set(key, value) click to toggle source
# File lib/revbits_pam_cicd/configuration.rb, line 95
def set(key, value)
  if self.class.accepted_options.include?(key.to_sym)
    explicit[key.to_sym] = value
    supplied[key.to_sym] = value
    computed.clear
  end
end