Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisBAshton committed Nov 25, 2016
1 parent 9963662 commit 321ceb1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
6 changes: 6 additions & 0 deletions lib/wraith/helpers/utilities.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
require "wraith/helpers/custom_exceptions"

def absolute_path_of_dir(filepath)
path_parts = filepath.split('/')
path_to_dir = path_parts.first path_parts.size - 1
path_to_dir.join('/')
end

def convert_to_absolute(filepath)
if !filepath
"false"
Expand Down
28 changes: 18 additions & 10 deletions lib/wraith/wraith.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ class Wraith::Wraith
attr_accessor :config

def initialize(config, yaml_passed = false)
@config = yaml_passed ? config : open_config_file(config)
if yaml_passed
@config = config
else
filepath = determine_config_path config
@config = YAML.load_file filepath
end

if @config['imports']
abs_path = @absolute_path_to_config.split('/')
abs_path = abs_path.first abs_path.size - 1
abs_path = abs_path.join('/')
yaml = YAML.load_file("#{abs_path}/#{@config['imports']}")
combined = yaml.merge(@config)
@config = combined
@config = apply_imported_config(@config['imports'], @config)
end

logger.level = verbose ? Logger::DEBUG : Logger::INFO
end

def open_config_file(config_name)
def determine_config_path(config_name)
possible_filenames = [
config_name,
"#{config_name}.yml",
Expand All @@ -30,13 +32,19 @@ def open_config_file(config_name)

possible_filenames.each do |filepath|
if File.exist?(filepath)
@absolute_path_to_config = convert_to_absolute(filepath)
return YAML.load_file filepath
@config_dir = absolute_path_of_dir(convert_to_absolute filepath)
return convert_to_absolute filepath
end
end

fail ConfigFileDoesNotExistError, "unable to find config \"#{config_name}\""
end

def apply_imported_config(config_to_import, config)
yaml = YAML.load_file("#{@config_dir}/#{config_to_import}")
yaml.merge(config)
end

def directory
# Legacy support for those using array configs
@config["directory"].is_a?(Array) ? @config["directory"].first : @config["directory"]
Expand Down

0 comments on commit 321ceb1

Please sign in to comment.