Class: EacRubyUtils::Console::Configs::EntryReader

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.entry_key_to_envvar_name(entry_key) ⇒ Object


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

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

#readObject


28
29
30
31
32
33
34
35
36
# File 'lib/eac_ruby_utils/console/configs/entry_reader.rb', line 28

def read
  %w[envvars storage console].each do |suffix|
    value = send("read_from_#{suffix}")
    return value if value.present?
  end
  return nil unless options[:required]

  raise "No value found for entry \"#{entry_key}\""
end

#read_from_consoleObject


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

def read_from_console
  return if options[:noinput]

  options[:before_input].if_present(&:call)
  entry_value = looped_entry_value_from_input
  console_configs.configs.write_entry(entry_key, entry_value) if options[:store]
  entry_value
end

#read_from_envvarsObject


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

def read_from_envvars
  return if options[:noenv]

  env_entry_key = self.class.entry_key_to_envvar_name(entry_key)
  return unless ENV.key?(env_entry_key)

  ENV.fetch(env_entry_key).if_present(::EacRubyUtils::BlankNotBlank.instance)
end

#read_from_storageObject


38
39
40
# File 'lib/eac_ruby_utils/console/configs/entry_reader.rb', line 38

def read_from_storage
  console_configs.configs.read_entry(entry_key)
end