lib/slim_lint/configuration.rb in slim_lint-0.2.0 vs lib/slim_lint/configuration.rb in slim_lint-0.3.0
- old
+ new
@@ -1,18 +1,26 @@
module SlimLint
# Stores runtime configuration for the application.
+ #
+ # The purpose of this class is to validate and ensure all configurations
+ # satisfy some basic pre-conditions so other parts of the application don't
+ # have to check the configuration for errors. It should have no knowledge of
+ # how these configuration values are ultimately used.
class Configuration
+ # Internal hash storing the configuration.
attr_reader :hash
# Creates a configuration from the given options hash.
#
# @param options [Hash]
def initialize(options)
@hash = options
validate
end
+ # Access the configuration as if it were a hash.
+ #
# @param key [String]
# @return [Array,Hash,Number,String]
def [](key)
@hash[key]
end
@@ -34,25 +42,15 @@
case linter
when Class
linter.name.split('::').last
when SlimLint::Linter
linter.name
- else
- linter.to_s
end
- smart_merge(@hash['linters']['ALL'],
- @hash['linters'].fetch(linter_name, {})).freeze
+ @hash['linters'].fetch(linter_name, {}).dup.freeze
end
- # Returns whether the specified linter is enabled by this configuration.
- #
- # @param linter [SlimLint::Linter,String]
- def linter_enabled?(linter)
- for_linter(linter)['enabled'] != false
- end
-
# Merges the given configuration with this one, returning a new
# {Configuration}. The provided configuration will either add to or replace
# any options defined in this configuration.
#
# @param config [SlimLint::Configuration]
@@ -60,17 +58,15 @@
self.class.new(smart_merge(@hash, config.hash))
end
private
- # Validates the configuration for any invalid options, normalizing it where
- # possible.
- def validate
- @hash = convert_nils_to_empty_hashes(@hash)
- ensure_linter_section_exists(@hash)
- end
-
+ # Merge two hashes such that nested hashes are merged rather than replaced.
+ #
+ # @param parent [Hash]
+ # @param child [Hash]
+ # @return [Hash]
def smart_merge(parent, child)
parent.merge(child) do |_key, old, new|
case old
when Hash
smart_merge(old, new)
@@ -78,24 +74,35 @@
new
end
end
end
- def ensure_linter_section_exists(hash)
- hash['linters'] ||= {}
- hash['linters']['ALL'] ||= {}
+ # Validates the configuration for any invalid options, normalizing it where
+ # possible.
+ def validate
+ ensure_exclude_option_array_exists
+ ensure_linter_section_exists
+ ensure_linter_include_exclude_arrays_exist
end
- def convert_nils_to_empty_hashes(hash)
- hash.each_with_object({}) do |(key, value), h|
- h[key] =
- case value
- when nil then {}
- when Hash then convert_nils_to_empty_hashes(value)
- else
- value
- end
- h
+ # Ensures the `exclude` global option is an array.
+ def ensure_exclude_option_array_exists
+ @hash['exclude'] = Array(@hash['exclude'])
+ end
+
+ # Ensures the `linters` configuration section exists.
+ def ensure_linter_section_exists
+ @hash['linters'] ||= {}
+ end
+
+ # Ensure `include` and `exclude` options for linters are arrays
+ # (since users can specify a single string glob pattern for convenience)
+ def ensure_linter_include_exclude_arrays_exist
+ @hash['linters'].keys.each do |linter_name|
+ %w[include exclude].each do |option|
+ linter_config = @hash['linters'][linter_name]
+ linter_config[option] = Array(linter_config[option])
+ end
end
end
end
end