Skip to content

Commit

Permalink
Merge be83bb4 into 452a8d4
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Courtois committed Mar 8, 2014
2 parents 452a8d4 + be83bb4 commit ce53353
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 180 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
*4.0.0

* `SN.config_file_name`, `SN.config_file` and `SN.config_file?` have been removed
* `ConfigFileFinder` and `ConfigFile` handle the configuration logic

*3.12.2

* Fixing issue #154. Thanks to Simon Curtois.
Expand Down
57 changes: 20 additions & 37 deletions lib/simple_navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'simple_navigation/core'
require 'simple_navigation/rendering'
require 'simple_navigation/adapters'
require 'simple_navigation/config_file_finder'
require 'simple_navigation/railtie' if defined?(::Rails)

require 'forwardable'
Expand Down Expand Up @@ -56,7 +57,7 @@ class << self
def set_env(root, environment)
self.root = root
self.environment = environment
config_file_paths << SimpleNavigation.default_config_file_path
config_file_paths << default_config_file_path
end

# Returns the current framework in which the plugin is running.
Expand Down Expand Up @@ -90,30 +91,6 @@ def default_config_file_path
File.join(root, 'config')
end

# Returns true if the config_file for specified context does exist.
def config_file?(navigation_context = :default)
!!config_file(navigation_context)
end

# Returns the path to the config file for the given navigation context or
# nil if no matching config file can be found.
# If multiple config_paths are set, it returns the first matching path.
def config_file(navigation_context = :default)
config_file_paths
.map { |path| File.join(path, config_file_name(navigation_context)) }
.find { |full_path| File.exist?(full_path) }
end

# Returns the name of the config file for the given navigation_context
def config_file_name(navigation_context = :default)
prefix = if navigation_context == :default
''
else
"#{navigation_context.to_s.underscore}_"
end
"#{prefix}navigation.rb"
end

# Resets the list of config_file_paths to the specified path
def config_file_path=(path)
self.config_file_paths = [path]
Expand All @@ -122,20 +99,10 @@ def config_file_path=(path)
# Reads the config_file for the specified navigation_context and stores it
# for later evaluation.
def load_config(navigation_context = :default)
unless config_file?(navigation_context)
fail "Config file '#{config_file_name(navigation_context)}' not " \
"found in path(s) #{config_file_paths.join(', ')}!"
end

# FIXME: what about update_config and update_config! methods ?
if environment == 'production'
config_files[navigation_context] ||= begin
IO.read(config_file(navigation_context))
end
update_config(navigation_context)
else
config_files[navigation_context] = begin
IO.read(config_file(navigation_context))
end
update_config!(navigation_context)
end
end

Expand Down Expand Up @@ -191,6 +158,22 @@ def apply_defaults(options)
options[:level] = options.delete(:levels) if options[:levels]
{ context: :default, level: :all }.merge(options)
end

def config_file(navigation_context)
ConfigFileFinder.new(config_file_paths).find(navigation_context)
end

def read_config(navigation_context)
File.read config_file(navigation_context)
end

def update_config(navigation_context)
config_files[navigation_context] ||= read_config(navigation_context)
end

def update_config!(navigation_context)
config_files[navigation_context] = read_config(navigation_context)
end
end
end

Expand Down
36 changes: 36 additions & 0 deletions lib/simple_navigation/config_file.rb
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
42 changes: 42 additions & 0 deletions lib/simple_navigation/config_file_finder.rb
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
1 change: 1 addition & 0 deletions simple-navigation.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'coveralls', '~> 0.7'
spec.add_development_dependency 'guard-rspec', '~> 4.2'
spec.add_development_dependency 'json_spec', '~> 1.1'
spec.add_development_dependency 'memfs', '~> 0.2'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rdoc'
spec.add_development_dependency 'rspec', '~> 2.0'
Expand Down
7 changes: 7 additions & 0 deletions spec/initializers/memfs.rb
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
51 changes: 51 additions & 0 deletions spec/lib/simple_navigation/config_file_finder_spec.rb
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
25 changes: 25 additions & 0 deletions spec/lib/simple_navigation/config_file_spec.rb
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

0 comments on commit ce53353

Please sign in to comment.