lib/fulmar/domain/service/configuration_service.rb in fulmar-0.6.5 vs lib/fulmar/domain/service/configuration_service.rb in fulmar-1.0.0

- old
+ new

@@ -3,13 +3,15 @@ module Fulmar module Domain module Service # Loads and prepares the configuration from the yaml file # TODO: Clone target configuration when used as a parameter to another service so an environment change won't affect it + # Not Sure if that is actually a god idea class ConfigurationService FULMAR_FILE = 'Fulmarfile' FULMAR_CONFIGURATION = 'FulmarConfiguration' + FULMAR_CONFIGURATION_DIR = 'Fulmar' DEPLOYMENT_CONFIG_FILE = 'deployment.yml' include Singleton attr_reader :environment, :target @@ -60,20 +62,51 @@ def ready? !@environment.nil? && !@target.nil? end + def has_feature?(feature) + return configuration[:features][feature] unless configuration[:features][feature].nil? + case feature + when :database + configuration[:features][:database] = any? { |data| data[:type] == 'maria' } + else + false + end + end + + def each + configuration[:environments].each_key do |env| + configuration[:environments][env].each_pair do |target, data| + yield(env, target, data) + end + end + end + + def any? + if block_given? + each{ |_env, _target, data| return true if yield(data) } + false + else + configuration[:environments].any? + end + end + # 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 end + def config_files + Dir.glob(File.join(base_path, FULMAR_CONFIGURATION_DIR, '*.config.yml')).sort + end + protected def lookup_base_path fulmar_file = Fulmar::Service::HelperService.reverse_file_lookup(Dir.pwd, FULMAR_FILE) @@ -102,19 +135,23 @@ end end # Loads the configuration from the YAML file and populates all targets def load_configuration - @config = YAML.load_file(base_path + '/' + DEPLOYMENT_CONFIG_FILE).symbolize + @config = { environments: {}, features: {} } + config_files.each do |config_file| + @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 @config[:environments][env].each_key do |target| fill_target(env, target) check_path(env, target) end end + @config[:environments].delete(:all) @config end def check_path(env, target) unless @config[:environments][env][target][:local_path].blank?