class CarthageCacheRes::Configurator
Constants
- CONFIG_FILE_NAME
Attributes
base_config[R]
config_file_path[R]
terminal[R]
Public Class Methods
new(terminal, project_path, base_config = {})
click to toggle source
# File lib/carthage_cache_res/configurator.rb, line 13 def initialize(terminal, project_path, base_config = {}) @config_file_path = File.join(project_path, CONFIG_FILE_NAME) @base_config = merge_config(base_config) @terminal = terminal end
Public Instance Methods
config()
click to toggle source
# File lib/carthage_cache_res/configurator.rb, line 19 def config @config ||= load_config! end
save_config(config)
click to toggle source
# File lib/carthage_cache_res/configurator.rb, line 23 def save_config(config) raise "Invalid configuration" unless config.valid? File.open(config_file_path, 'w') { |f| f.write config.to_yaml } end
Private Instance Methods
config_file_exist?()
click to toggle source
# File lib/carthage_cache_res/configurator.rb, line 30 def config_file_exist? File.exist?(config_file_path) end
load_config()
click to toggle source
# File lib/carthage_cache_res/configurator.rb, line 40 def load_config if config_file_exist? config = Configuration.parse(File.read(config_file_path)) config = base_config.merge(config) else base_config end end
load_config!()
click to toggle source
# File lib/carthage_cache_res/configurator.rb, line 34 def load_config! config = load_config validate!(config) config end
merge_config(config)
click to toggle source
# File lib/carthage_cache_res/configurator.rb, line 72 def merge_config(config) new_config = Configuration.default.hash_object.merge(config) Configuration.new(remove_nil_keys(new_config)) end
remove_nil_keys(hash)
click to toggle source
# File lib/carthage_cache_res/configurator.rb, line 58 def remove_nil_keys(hash) hash.inject({}) do |new_hash, (k,v)| unless v.nil? || (v.respond_to?(:empty?) && v.empty?) if v.class == Hash cleaned_hashed = remove_nil_keys(v) new_hash[k] = cleaned_hashed unless cleaned_hashed.empty? else new_hash[k] = v end end new_hash end end
validate!(config)
click to toggle source
# File lib/carthage_cache_res/configurator.rb, line 49 def validate!(config) validator = ConfigurationValidator.new(config) result = validator.validate unless result.valid? terminal.error "error: #{result.error.solution}" exit 1 end end