Class: Conjur::Configuration

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(explicit = {}) ⇒ Configuration

Returns a new instance of Configuration.



51
52
53
54
# File 'lib/conjur/configuration.rb', line 51

def initialize explicit = {}
  @explicit = explicit.dup
  @supplied = explicit.dup
end

Instance Attribute Details

#explicitObject (readonly)

All explicit values.



46
47
48
# File 'lib/conjur/configuration.rb', line 46

def explicit
  @explicit
end

#suppliedObject (readonly)

All explicit and cached values.



49
50
51
# File 'lib/conjur/configuration.rb', line 49

def supplied
  @supplied
end

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.



58
59
60
61
# File 'lib/conjur/configuration.rb', line 58

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

  • :convert (Proc, #to_proc)

    proc-ish to convert environment values to appropriate types



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/conjur/configuration.rb', line 75

def add_option name, options = {}, &def_proc
  accepted_options << name
  allow_env = options[:env].nil? || options[:env]
  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}_env_var") do
    allow_env ? env_var : nil
  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 
      instance_eval(&def_proc)
    end
  end
  alias_method("#{name}?", name) if options[:boolean]
end

Instance Method Details

#clone(override_options) ⇒ Object

Copies the current configuration, except a set of overridden options.



116
117
118
# File 'lib/conjur/configuration.rb', line 116

def clone override_options
  self.class.new self.explicit.dup.merge(override_options)
end

#set(key, value) ⇒ Object



120
121
122
123
124
125
# File 'lib/conjur/configuration.rb', line 120

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