Class: PropertyGenerator::Service
- Inherits:
-
Object
- Object
- PropertyGenerator::Service
- Defined in:
- lib/generator/service.rb
Instance Attribute Summary collapse
-
#service ⇒ Object
Returns the value of attribute service.
Instance Method Summary collapse
-
#initialize(service_data, config, globals) ⇒ Service
constructor
A new instance of Service.
- #interpolate ⇒ Object
- #merge_env_default(data, environments) ⇒ Object
- #merge_service_with_globals(globals_data, service_data, environments) ⇒ Object
- #set_service ⇒ Object
Constructor Details
#initialize(service_data, config, globals) ⇒ Service
Returns a new instance of Service.
4 5 6 7 8 9 10 |
# File 'lib/generator/service.rb', line 4 def initialize(service_data, config, globals) @service_data = service_data @environments = config.environments @globals = globals @environment_configs = config.environment_configs set_service end |
Instance Attribute Details
#service ⇒ Object
Returns the value of attribute service.
3 4 5 |
# File 'lib/generator/service.rb', line 3 def service @service end |
Instance Method Details
#interpolate ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/generator/service.rb', line 21 def interpolate environments = @environments #read in config #interate through environment and substitute config for values for that environment environments.each do | env| #get the map of config for a env interpolations = @environment_configs[env]['interpolations'] #interate through the properties for an environment and gsub the config @service[env].each do | property_key, property_value| property_value_dup = property_value.dup interpolations.each do |matcher_key, matcher_value| if property_value.class == String && property_value_dup.include?("{#{matcher_key}}") @service[env][property_key] = property_value_dup.gsub!("{#{matcher_key}}", matcher_value) end end end end service end |
#merge_env_default(data, environments) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/generator/service.rb', line 42 def merge_env_default(data, environments) #creates a hash of the enviornments merged with the defaults # {service => {env1 => {properties}, # env2 => {properties} # } # } output = {} default = data['default'] environments.each do |env| default_clone = default.dup #if nil, use set to environments as nothing to merge env with data['environments'] ||= {} data['environments'][env] ||= {} environment_data = data['environments'][env].dup if data['encrypted'] encrypted = data['encrypted'][env].dup unless data['encrypted'][env].nil? environment_data = data['environments'][env].merge(encrypted) unless encrypted.nil? end if default_clone.nil? merged = environment_data else merged = default_clone.merge(environment_data) end output[env] = merged end output end |
#merge_service_with_globals(globals_data, service_data, environments) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/generator/service.rb', line 71 def merge_service_with_globals(globals_data, service_data, environments) #service will now overwrite globals, merging will be done for each environment output = {} envs = environments envs.each do |env| globals_clone = globals_data.dup if globals_clone[env].nil? || globals_clone[env] == false merged = service_data[env] else merged = globals_clone[env].merge(service_data[env]) end output[env] = merged end output end |
#set_service ⇒ Object
12 13 14 15 |
# File 'lib/generator/service.rb', line 12 def set_service service_data = merge_env_default(@service_data, @environments) @service = merge_service_with_globals(@globals, service_data, @environments) end |