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.



51
52
53
54
55
56
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
# File 'lib/generator/globals.rb', line 51

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] = [].merge(hash) unless hash.empty?
      condensed[env] = env_global[env]
    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.merge(condensed[env])
    end
  end
  condensed
end

#get_account_globalsObject



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

def 
  data = {}
  @accounts.each do ||
    next unless Dir.exists?("#{@project_path}/globals/accounts/#{}")
     = "#{@project_path}/globals/accounts/#{}/#{}.yml"
    data[] = YAML.load_file() if File.exists?()
  end
  data
end

#get_environment_globalsObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/generator/globals.rb', line 36

def get_environment_globals
  data = {}
  @accounts.each do ||
    next unless Dir.exists?("#{@project_path}/globals/accounts/#{}/environments")
    data[] = {}
    @environments.each do |env|
      next unless File.exists?("#{@project_path}/globals/accounts/#{}/environments/#{env}.yml")
      data[][env] = YAML.load_file("#{@project_path}/globals/accounts/#{}/environments/#{env}.yml")
    end
  end
  data
end

#get_main_globalObject



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

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