Class: Conjur::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/conjur/pubkeys-api.rb,
lib/conjur/configuration.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.accepted_optionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



36
37
38
39
# File 'lib/conjur/configuration.rb', line 36

def accepted_options
  require 'set'
  @options ||= Set.new
end

.add_option(name, options = {}, &def_proc) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • name (Symbol)
  • options (Hash) (defaults to: {})
  • def_proc (Proc)

    block to provide default values

Options Hash (options):

  • :boolean (Boolean) — default: false

    whether this option should have a ‘?’ accessor

  • :env (Boolean, String)

    Environment variable for this option. Set to false to disallow environment based configuration. Default is CONJUR_<OPTION_NAME>.

  • :default (Proc, *)

    Default value or proc to provide it

  • :required (Boolean) — default: false

    when true, raise an exception if the option is not set

  • :sticky (Boolean) — default: true

    when false, default proc will be called every time, otherwise the proc’s result will be cached

  • :convert (Proc, #to_proc)

    proc-ish to convert environment values to appropriate types



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/conjur/configuration.rb', line 55

def add_option name, options = {}, &def_proc
  accepted_options << name
  allow_env = options[:env].nil? or options[:env]
  sticky = options.member?(:sticky) ? options[:sticky] : true
  env_var = options[:env] || "CONJUR_#{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 }
  # Allow a Symbol, for example
  convert = convert.to_proc if convert.respond_to?(:to_proc) 

  define_method("#{name}=") do |value|
    set name, value
  end
  
  define_method(name) do
    if supplied.member?(name)
      supplied[name]
    elsif allow_env && ENV.member?(env_var)
      instance_exec(ENV[env_var], &convert)
    else 
      value = instance_eval(&def_proc)
      supplied[name] = value if sticky
      value
    end
  end
  alias_method("#{name}?", name) if options[:boolean]
end

Instance Method Details

#set(key, value) ⇒ Object



94
95
96
97
98
# File 'lib/conjur/configuration.rb', line 94

def set(key, value)
  if self.class.accepted_options.include?(key.to_sym)
    supplied[key.to_sym] = value
  end
end