-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
223 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'active_support/core_ext/string' | ||
|
||
module SimpleNavigation | ||
# Internal: Encapsulates the config file naming knowledge. | ||
class ConfigFile | ||
# Internal: Initializes a ConfigFile. | ||
# | ||
# context - The navigation context for this ConfigFile. | ||
def initialize(context) | ||
@prefix = prefix_for_context(context) | ||
end | ||
|
||
# Internal: Returns the name of the configuration file on disk. | ||
# | ||
# Based on the the initialization context the outcome may differ. | ||
# | ||
# Examples | ||
# | ||
# ConfigFile.new.name # => "navigation.rb" | ||
# ConfigFile.new(:default).name # => "navigation.rb" | ||
# ConfigFile.new(:other).name # => "other_navigation.rb" | ||
# | ||
# Returns a String representing the name of the configuration file on disk. | ||
def name | ||
@name ||= "#{prefix}navigation.rb" | ||
end | ||
|
||
private | ||
|
||
attr_reader :prefix | ||
|
||
def prefix_for_context(context) | ||
context == :default ? '' : "#{context.to_s.underscore}_" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'simple_navigation/config_file' | ||
|
||
module SimpleNavigation | ||
# Internal: Encapsulates the configuration file finding logic. | ||
class ConfigFileFinder | ||
# Internal: Initializes a ConfigFileFinder. | ||
# | ||
# paths - an enumerable list of paths in which to look for configuration | ||
# files. | ||
def initialize(paths) | ||
@paths = paths | ||
end | ||
|
||
# Internal: Searches a configuration file for the given context in the | ||
# initialization paths. | ||
# | ||
# context - The navigation context for which to look the configuration file. | ||
# | ||
# Returns a String representing the full path of the configuation file. | ||
# Raises StandardError if no file is found. | ||
def find(context) | ||
config_file_name = config_file_name_for_context(context) | ||
|
||
find_config_file(config_file_name) || | ||
fail("Config file '#{config_file_name}' not found in " \ | ||
"path(s) #{paths.join(', ')}!") | ||
end | ||
|
||
private | ||
|
||
attr_reader :paths | ||
|
||
def config_file_name_for_context(context) | ||
ConfigFile.new(context).name | ||
end | ||
|
||
def find_config_file(config_file_name) | ||
paths.map { |path| File.join(path, config_file_name) } | ||
.find { |full_path| File.exist?(full_path) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'memfs' | ||
|
||
RSpec.configure do |config| | ||
config.around(memfs: true) do |example| | ||
MemFs.activate { example.run } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'spec_helper' | ||
require 'fileutils' | ||
require 'simple_navigation/config_file_finder' | ||
|
||
module SimpleNavigation | ||
describe ConfigFileFinder do | ||
subject(:finder) { ConfigFileFinder.new(paths) } | ||
|
||
let(:paths) { ['/path/one', '/path/two'] } | ||
|
||
describe '#find', memfs: true do | ||
before { FileUtils.mkdir_p(paths) } | ||
|
||
context 'when the context is :default' do | ||
let(:context) { :default } | ||
|
||
context 'and a navigation.rb file is found in one of the paths' do | ||
before { FileUtils.touch('/path/one/navigation.rb') } | ||
|
||
it 'returns its full path' do | ||
expect(finder.find(context)).to eq '/path/one/navigation.rb' | ||
end | ||
end | ||
|
||
context 'and no navigation.rb file is found in the paths' do | ||
it 'raises an exception' do | ||
expect{ finder.find(context) }.to raise_error | ||
end | ||
end | ||
end | ||
|
||
context 'when the context is :other' do | ||
let(:context) { :other } | ||
|
||
context 'and a other_navigation.rb file is found in one of the paths' do | ||
before { FileUtils.touch('/path/two/other_navigation.rb') } | ||
|
||
it 'returns its full path' do | ||
expect(finder.find(context)).to eq '/path/two/other_navigation.rb' | ||
end | ||
end | ||
|
||
context 'and no other_navigation.rb file is found in the paths' do | ||
it 'raise an exception' do | ||
expect{ finder.find(context) }.to raise_error | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'simple_navigation/config_file' | ||
|
||
module SimpleNavigation | ||
describe ConfigFile do | ||
subject(:config_file) { ConfigFile.new(context) } | ||
|
||
let(:context) { :default } | ||
|
||
describe '#name' do | ||
context 'when the context is :default' do | ||
it 'returns navigation.rb' do | ||
expect(config_file.name).to eq 'navigation.rb' | ||
end | ||
end | ||
|
||
context 'when the context is different from :default' do | ||
let(:context) { :HelloWorld } | ||
|
||
it 'returns UNDERSCORED_CONTEXT_navigation.rb' do | ||
expect(config_file.name).to eq 'hello_world_navigation.rb' | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.