Class: Sanctum::GetConfig::ConfigMerge

Inherits:
Object
  • Object
show all
Defined in:
lib/sanctum/get_config/config_merge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file:, targets:, force:) ⇒ ConfigMerge

Returns a new instance of ConfigMerge.



14
15
16
17
18
# File 'lib/sanctum/get_config/config_merge.rb', line 14

def initialize(config_file: , targets: , force: )
  @config_file = config_file
  @targets = targets.split(/\,/).map(&:strip) unless targets.nil?
  @force = force
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



12
13
14
# File 'lib/sanctum/get_config/config_merge.rb', line 12

def config_file
  @config_file
end

#forceObject (readonly)

Returns the value of attribute force.



12
13
14
# File 'lib/sanctum/get_config/config_merge.rb', line 12

def force
  @force
end

#targetsObject (readonly)

Returns the value of attribute targets.



12
13
14
# File 'lib/sanctum/get_config/config_merge.rb', line 12

def targets
  @targets
end

Instance Method Details

#check_targets(targets, config_options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sanctum/get_config/config_merge.rb', line 38

def check_targets(targets, config_options)
  tmp_array = Array.new
  sync = config_options[:sync]

  targets.each do |t|
    sync.each do |h|
      tmp_array << h if h[:name] == t
    end
  end

  if tmp_array.empty?
    valid_targets = sync.map{|h| h[:name]}
    raise "Please specify at least one valid target\n Valid targets are #{valid_targets}"
  end

  config_options[:sync] = tmp_array
  config_options
end

#final_optionsObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sanctum/get_config/config_merge.rb', line 20

def final_options
  # default_options will search for config_file or take the path specified via cli
  default_options = DefaultOptions.new(config_file).run
  config_options = ConfigFile.new(default_options[:config_file]).run
  env_options = Env.new.run
  cli_options = {cli: {targets: targets, force: force }}

  # Check that targets specified via commandline actually exist in config_file and update config_options[:sync] array
  config_options = check_targets(targets, config_options) unless targets.nil?

  merge_options(default_options, config_options, env_options, cli_options)
end

#merge_options(default_options, config_options, env_options, cli_options) ⇒ Object



34
35
36
# File 'lib/sanctum/get_config/config_merge.rb', line 34

def merge_options(default_options, config_options, env_options, cli_options)
  default_options.deep_merge(config_options).deep_merge(env_options).deep_merge(cli_options)
end