Class: EacRubyUtils::Console::Configs

Inherits:
Object
  • Object
show all
Includes:
Speaker
Defined in:
lib/eac_ruby_utils/console/configs.rb

Constant Summary collapse

STORE_PASSWORDS_KEY =
'core.store_passwords'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Speaker

#fatal_error, #info, #infom, #infov, #out, #puts, #request_input, #success, #title, #warn

Constructor Details

#initialize(configs_key) ⇒ Configs

Returns a new instance of Configs.



27
28
29
# File 'lib/eac_ruby_utils/console/configs.rb', line 27

def initialize(configs_key)
  @configs = ::EacRubyUtils::Configs.new(configs_key, autosave: true)
end

Instance Attribute Details

#configsObject (readonly)

Returns the value of attribute configs.



25
26
27
# File 'lib/eac_ruby_utils/console/configs.rb', line 25

def configs
  @configs
end

Class Method Details

.entry_key_to_envvar_name(entry_key) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/eac_ruby_utils/console/configs.rb', line 12

def entry_key_to_envvar_name(entry_key)
  path = if entry_key.is_a?(::Array)
           entry_key
         else
           ::EacRubyUtils::PathsHash.parse_entry_key(entry_key)
         end
  path.join('_').gsub(/[^a-z0-9_]/i, '').gsub(/\A_+/, '').gsub(/_+\z/, '')
      .gsub(/_{2,}/, '_').upcase
end

Instance Method Details

#read_entry(entry_key, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/eac_ruby_utils/console/configs.rb', line 40

def read_entry(entry_key, options = {})
  unless options[:noenv]
    envvar_value = envvar_read_entry(entry_key)
    return envvar_value if envvar_value.present?
  end
  stored_value = configs.read_entry(entry_key)
  return stored_value if stored_value
  return read_entry_from_console(entry_key, options) unless options[:noinput]
  raise "No value found for entry \"#{entry_key}\""
end

#read_password(entry_key, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/eac_ruby_utils/console/configs.rb', line 31

def read_password(entry_key, options = {})
  options = options.merge(noecho: true)
  if store_passwords?
    read_entry(entry_key, options)
  else
    looped_entry_value_from_input(entry_key, options)
  end
end

#store_passwords?Boolean

Returns:

  • (Boolean)


51
52
53
54
55
56
57
# File 'lib/eac_ruby_utils/console/configs.rb', line 51

def store_passwords?
  'yes' == read_entry(
    STORE_PASSWORDS_KEY,
    before_input: -> { store_password_banner },
    validator: ->(entry_value) { %w[yes no].include?(entry_value) }
  )
end