Skip to content

Commit

Permalink
Merge pull request #486 from BBC-News/imports
Browse files Browse the repository at this point in the history
Adds 'Imports' functionality
  • Loading branch information
ChrisBAshton committed Nov 25, 2016
2 parents 87c8de2 + e8944bf commit 6300cc4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 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
29 changes: 25 additions & 4 deletions lib/wraith/wraith.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +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']
@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 @@ -22,13 +32,24 @@ def open_config_file(config_name)

possible_filenames.each do |filepath|
if File.exist?(filepath)
config = File.open filepath
return YAML.load config
@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)
path_to_config = "#{@config_dir}/#{config_to_import}"
if File.exist?(path_to_config)
yaml = YAML.load_file path_to_config
return yaml.merge(config)
end

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

def directory
# Legacy support for those using array configs
@config["directory"].is_a?(Array) ? @config["directory"].first : @config["directory"]
Expand Down
10 changes: 10 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
it "contains shot options" do
expect(wraith.config).to include "directory" => "shots"
end

it "should be able to import other configs" do
config_name = get_path_relative_to __FILE__, "./configs/test_config--imports.yaml"
wraith = Wraith::Wraith.new(config_name)

# retain the imported config settings
expect(wraith.paths).to eq("home" => "/", "uk_index" => "/uk")
# ...but override the imported config in places
expect(wraith.widths).to eq [1337]
end
end

describe "When creating a wraith worker" do
Expand Down
4 changes: 4 additions & 0 deletions spec/configs/test_config--imports.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
imports: 'test_config--phantom.yaml'

screen_widths:
- 1337

0 comments on commit 6300cc4

Please sign in to comment.