Class: Appydave::Tools::Configuration::Config

Inherits:
Object
  • Object
show all
Extended by:
KLog::Logging
Defined in:
lib/appydave/tools/configuration/config.rb

Overview

Configuration class for handling multiple configurations

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.config_pathObject

Returns the value of attribute config_path.



11
12
13
# File 'lib/appydave/tools/configuration/config.rb', line 11

def config_path
  @config_path
end

.configurationsObject (readonly)

Returns the value of attribute configurations.



12
13
14
# File 'lib/appydave/tools/configuration/config.rb', line 12

def configurations
  @configurations
end

.default_blockObject (readonly)

Returns the value of attribute default_block.



13
14
15
# File 'lib/appydave/tools/configuration/config.rb', line 13

def default_block
  @default_block
end

Class Method Details

.configureObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/appydave/tools/configuration/config.rb', line 15

def configure
  if block_given?
    yield self
  elsif default_block
    default_block.call(self)
  else
    raise Appydave::Tools::Error, 'No configuration block provided'
  end
  ensure_config_directory
end

.debugObject



61
62
63
64
# File 'lib/appydave/tools/configuration/config.rb', line 61

def debug
  log.kv 'Configuration Path', config_path
  configurations.each_value(&:debug)
end

.editObject



54
55
56
57
58
59
# File 'lib/appydave/tools/configuration/config.rb', line 54

def edit
  ensure_config_directory
  puts "Edit configuration: #{config_path}"
  open_vscode = "code  --folder-uri '#{config_path}'" # --new-window
  Open3.capture3(open_vscode)
end

.loadObject



50
51
52
# File 'lib/appydave/tools/configuration/config.rb', line 50

def load
  configurations.each_value(&:load)
end

.method_missing(method_name, *_args) ⇒ Object



31
32
33
34
35
36
# File 'lib/appydave/tools/configuration/config.rb', line 31

def method_missing(method_name, *_args)
  raise Appydave::Tools::Error, 'Configuration has never been registered' if @configurations.nil?
  raise Appydave::Tools::Error, "Configuration not available: #{method_name}" unless @configurations.key?(method_name)

  @configurations[method_name]
end

def print

log.kv 'Configuration Path', config_path
configurations.each_value(&:print)

end



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/appydave/tools/configuration/config.rb', line 71

def print(*keys)
  if keys.empty?
    keys = configurations.keys
  else
    keys.map!(&:to_sym)
  end

  keys.each do |key|
    if configurations[key]
      configurations[key].print
    else
      log.error "Configuration not available: #{key}"
    end
  end
end

.register(key, klass) ⇒ Object



26
27
28
29
# File 'lib/appydave/tools/configuration/config.rb', line 26

def register(key, klass)
  @configurations ||= {}
  @configurations[key] = klass.new
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/appydave/tools/configuration/config.rb', line 38

def respond_to_missing?(method_name, include_private = false)
  @configurations.key?(method_name) || super
end

.saveObject



42
43
44
# File 'lib/appydave/tools/configuration/config.rb', line 42

def save
  configurations.each_value(&:save)
end

.set_default(&block) ⇒ Object



46
47
48
# File 'lib/appydave/tools/configuration/config.rb', line 46

def set_default(&block)
  @default_block = block
end