lib/simple_navigation.rb in simple-navigation-3.14.0 vs lib/simple_navigation.rb in simple-navigation-4.0.0

- old
+ new

@@ -2,13 +2,19 @@ require 'active_support/core_ext/array' require 'active_support/core_ext/hash' require 'active_support/core_ext/module/attribute_accessors' require 'simple_navigation/version' -require 'simple_navigation/core' -require 'simple_navigation/rendering' +require 'simple_navigation/configuration' +require 'simple_navigation/item_adapter' +require 'simple_navigation/item' +require 'simple_navigation/item_container' +require 'simple_navigation/items_provider' +require 'simple_navigation/renderer' require 'simple_navigation/adapters' +require 'simple_navigation/config_file_finder' +require 'simple_navigation/railtie' if defined?(::Rails) require 'forwardable' # A plugin for generating a simple navigation. See README for resources on # usage instructions. @@ -53,11 +59,11 @@ # Sets the root path and current environment as specified. Also sets the # default config_file_path. 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. def framework return :rails if defined?(Rails) @@ -87,56 +93,22 @@ 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] end # 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 # Returns the singleton instance of the SimpleNavigation::Configuration def config @@ -184,12 +156,23 @@ registered_renderers.merge!(renderer_hash) end private - def apply_defaults(options) - options[:level] = options.delete(:levels) if options[:levels] - { context: :default, level: :all }.merge(options) + 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 SimpleNavigation.load_adapter