Class: PropertyGenerator::Globals

Inherits:
Object
  • Object
show all
Defined in:
lib/generator/globals.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_path, config) ⇒ Globals

Returns a new instance of Globals.



6
7
8
9
10
11
# File 'lib/generator/globals.rb', line 6

def initialize(project_path, config)
  @project_path = project_path
  @environments = config.environments
  @environment_configs = config.environment_configs
  @accounts = config.accounts
end

Instance Attribute Details

#globalsObject

Returns the value of attribute globals.



4
5
6
# File 'lib/generator/globals.rb', line 4

def globals
  @globals
end

Instance Method Details

#condense_globalsObject

merge environment globals with account globals.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
# File 'lib/generator/globals.rb', line 57

def condense_globals
  condensed = {}
  # get account and the environmental hash's for said account
  environment_globals = get_environment_globals
   = 
  main_global = get_main_global
  # nothing to do here if everything is empty
  return condensed if environment_globals.empty? && .empty? && main_global.empty?

  environment_globals.each do |, env_global|
    # get the env and the values
    env_global.each do |env, hash|
      [] ||= {}
      # set the environment globals to be the account global merged with the env globals
      env_global[env] = [].deep_merge(hash) unless hash.empty?
      condensed[env] = env_global[env]
    end
  end

  # If there are no environmental globals and there are account globals we have to merge them in
  if condensed.empty? && !.empty?
    # assemble the account to env mapping
    mapping = {}

    @environment_configs.each do |env, vals|
       = vals["account"]
      next if .nil?

      mapping[] = Array(mapping[]).push(env)
    end

    .each do |, |
      mapping[].each do |env|
        condensed[env] = 
      end
    end
  end

  unless main_global.empty?
    # All environments need the main global definitions
    @environments.each do |env|
      # If a key/value pair for a environment has not been defined set one so we can merge
      condensed[env] ||= {}
      # We need to merge into the globals so any env configs overwrite main global configs.
      # Dup so we dont modify the original object
      main_global_dup = main_global.dup
      condensed[env] = main_global_dup.deep_merge(condensed[env])
    end
  end
  condensed
end

#get_account_globalsObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/generator/globals.rb', line 25

def 
  data = {}
  @accounts.each do ||
    next unless Dir.exist?("#{@project_path}/globals/accounts/#{}")

     = "#{@project_path}/globals/accounts/#{}/#{}.yml"
    data[] = YAML.load_file() if File.exist?()
  end
  data
end

#get_environment_globalsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/generator/globals.rb', line 36

def get_environment_globals
  data = {}
  @accounts.each do ||
    next unless Dir.exist?("#{@project_path}/globals/accounts/#{}/environments")

    data[] = {}
    @environments.each do |env|
      next unless File.exist?("#{@project_path}/globals/accounts/#{}/environments/#{env}.yml")

      data[][env] = YAML.load_file("#{@project_path}/globals/accounts/#{}/environments/#{env}.yml")
      unless data[][env]['encrypted'].nil?
        encrypted = data[][env]['encrypted'].dup
        not_encrypted = data[][env].reject { |k, _| k == 'encrypted' }
        data[][env] = not_encrypted.deep_merge(encrypted)
      end
    end
  end
  data
end

#get_main_globalObject



17
18
19
20
21
22
23
# File 'lib/generator/globals.rb', line 17

def get_main_global
  top_level = {}
  if File.exist?("#{@project_path}/globals/globals.yml")
    top_level = YAML.load_file("#{@project_path}/globals/globals.yml")
  end
  top_level
end