Class: PropertyGenerator::Globals
- Inherits:
-
Object
- Object
- PropertyGenerator::Globals
- Defined in:
- lib/generator/globals.rb
Instance Attribute Summary collapse
-
#globals ⇒ Object
Returns the value of attribute globals.
Instance Method Summary collapse
-
#condense_globals ⇒ Object
merge environment globals with account globals.
- #get_account_globals ⇒ Object
- #get_environment_globals ⇒ Object
- #get_main_global ⇒ Object
-
#initialize(project_path, config) ⇒ Globals
constructor
A new instance of Globals.
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
#globals ⇒ Object
Returns the value of attribute globals.
4 5 6 |
# File 'lib/generator/globals.rb', line 4 def globals @globals end |
Instance Method Details
#condense_globals ⇒ Object
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 account_globals = get_account_globals main_global = get_main_global # nothing to do here if everything is empty return condensed if environment_globals.empty? && account_globals.empty? && main_global.empty? environment_globals.each do |account, env_global| # get the env and the values env_global.each do |env, hash| account_globals[account] ||= {} # set the environment globals to be the account global merged with the env globals env_global[env] = account_globals[account].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? && !account_globals.empty? # assemble the account to env mapping mapping = {} @environment_configs.each do |env, vals| account = vals["account"] next if account.nil? mapping[account] = Array(mapping[account]).push(env) end account_globals.each do |account, env_account| mapping[account].each do |env| condensed[env] = env_account 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_globals ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/generator/globals.rb', line 25 def get_account_globals data = {} @accounts.each do |account| next unless Dir.exist?("#{@project_path}/globals/accounts/#{account}") account_default_file = "#{@project_path}/globals/accounts/#{account}/#{account}.yml" data[account] = YAML.load_file(account_default_file) if File.exist?(account_default_file) end data end |
#get_environment_globals ⇒ Object
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 |account| next unless Dir.exist?("#{@project_path}/globals/accounts/#{account}/environments") data[account] = {} @environments.each do |env| next unless File.exist?("#{@project_path}/globals/accounts/#{account}/environments/#{env}.yml") data[account][env] = YAML.load_file("#{@project_path}/globals/accounts/#{account}/environments/#{env}.yml") unless data[account][env]['encrypted'].nil? encrypted = data[account][env]['encrypted'].dup not_encrypted = data[account][env].reject { |k, _| k == 'encrypted' } data[account][env] = not_encrypted.deep_merge(encrypted) end end end data end |
#get_main_global ⇒ Object
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 |