lib/fulmar/domain/service/configuration_service.rb in fulmar-1.0.0 vs lib/fulmar/domain/service/configuration_service.rb in fulmar-1.1.0
- old
+ new
@@ -31,11 +31,11 @@
if ready?
configuration[:environments][@environment][@target][id] = value
else
fail 'Environment or target not set. Please set both variables via configuration.environment = \'xxx\' / '\
'configuration.target = \'yyy\''
- end
+ end
end
def to_hash
ready? ? configuration[:environments][@environment][@target] : configuration
end
@@ -59,14 +59,17 @@
def target=(target)
@target = target ? target.to_sym : nil
end
def ready?
- !@environment.nil? && !@target.nil?
+ return false if @environment.nil? || @target.nil?
+ fail 'Environment is invalid' if configuration[:environments][@environment].nil?
+ fail 'Target is invalid' if configuration[:environments][@environment][@target].nil?
+ true
end
- def has_feature?(feature)
+ def feature?(feature)
return configuration[:features][feature] unless configuration[:features][feature].nil?
case feature
when :database
configuration[:features][:database] = any? { |data| data[:type] == 'maria' }
else
@@ -82,11 +85,11 @@
end
end
def any?
if block_given?
- each{ |_env, _target, data| return true if yield(data) }
+ each { |_env, _target, data| return true if yield(data) }
false
else
configuration[:environments].any?
end
end
@@ -94,13 +97,12 @@
# Merge another configuration into the currently active one
# Useful for supplying a default configuration, as values are not overwritten.
# Hashes are merged.
# @param [Hash] other
def merge(other)
- if @environment && @target
- configuration[:environments][@environment][@target] = other.deep_merge(configuration[:environments][@environment][@target])
- end
+ return unless @environment && @target
+ configuration[:environments][@environment][@target] = other.deep_merge(configuration[:environments][@environment][@target])
end
def config_files
Dir.glob(File.join(base_path, FULMAR_CONFIGURATION_DIR, '*.config.yml')).sort
end
@@ -119,29 +121,29 @@
end
# Fills a target with all globally set variables so all necessary information
# is found within each target
def fill_target(env, target)
- @config[:environments][env][target] = @config[:environments][:all].deep_merge(@config[:environments][env][target])
+ @config[:environments][env][target] = @config[:environments][:all].deep_merge(@config[:environments][env][target]) if @config[:environments][:all]
- unless @config[:environments][env][target][:host].blank?
- host = @config[:environments][env][target][:host].to_sym
- if @config[:hosts] && @config[:hosts][host]
- @config[:hosts][host].each do
- @config[:environments][env][target] = @config[:hosts][host].deep_merge(@config[:environments][env][target])
- end
- else
- fail "Host #{host} is not configured."
+ return if @config[:environments][env][target][:host].blank?
+
+ host = @config[:environments][env][target][:host].to_sym
+ if @config[:hosts] && @config[:hosts][host]
+ @config[:hosts][host].each do
+ @config[:environments][env][target] = @config[:hosts][host].deep_merge(@config[:environments][env][target])
end
+ else
+ fail "Host #{host} is not configured."
end
end
# Loads the configuration from the YAML file and populates all targets
def load_configuration
- @config = { environments: {}, features: {} }
+ @config = { environments: {}, features: {}, hosts: {} }
config_files.each do |config_file|
- @config = @config.deep_merge(YAML.load_file(config_file).symbolize)
+ @config = @config.deep_merge((YAML.load_file(config_file) || {}).symbolize)
end
# Iterate over all environments and targets to prepare them
@config[:environments].each_key do |env|
next if env == :all
@@ -153,12 +155,11 @@
@config[:environments].delete(:all)
@config
end
def check_path(env, target)
- unless @config[:environments][env][target][:local_path].blank?
- @config[:environments][env][target][:local_path] = File.expand_path(@config[:environments][env][target][:local_path])
- end
+ return if @config[:environments][env][target][:local_path].blank?
+ @config[:environments][env][target][:local_path] = File.expand_path(@config[:environments][env][target][:local_path])
end
end
end
end
end