Sha256: 2f08c0379d575d9680fad5fecdf5839bf33889f0343e7c4e5545f2b46e41adb1

Contents?: true

Size: 1.52 KB

Versions: 16

Compression:

Stored size: 1.52 KB

Contents

require "yaml"
require "pathname"
require_relative "file_finder"
require_relative "parses_cli_option"

module Standard
  class LoadsYamlConfig
    def initialize
      @parses_cli_option = ParsesCliOption.new
    end

    def call(argv, search_path)
      yaml_path = @parses_cli_option.call(argv, "--config") ||
        FileFinder.new.call(".standard.yml", search_path)
      construct_config(yaml_path, load_standard_yaml(yaml_path))
    end

    private

    def load_standard_yaml(yaml_path)
      if yaml_path
        YAML.load_file(yaml_path) || {}
      else
        {}
      end
    end

    def construct_config(yaml_path, standard_yaml)
      {
        ruby_version: Gem::Version.new((standard_yaml["ruby_version"] || RUBY_VERSION)),
        fix: !!standard_yaml["fix"],
        format: standard_yaml["format"],
        parallel: !!standard_yaml["parallel"],
        ignore: expand_ignore_config(standard_yaml["ignore"]),
        default_ignores: standard_yaml.key?("default_ignores") ? !!standard_yaml["default_ignores"] : true,
        config_root: yaml_path ? Pathname.new(yaml_path).dirname.to_s : nil,
      }
    end

    def expand_ignore_config(ignore_config)
      arrayify(ignore_config).map { |rule|
        if rule.is_a?(String)
          [rule, ["AllCops"]]
        elsif rule.is_a?(Hash)
          rule.entries.first
        end
      }
    end

    def arrayify(object)
      if object.nil?
        []
      elsif object.respond_to?(:to_ary)
        object.to_ary || [object]
      else
        [object]
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
standard-0.1.10 lib/standard/loads_yaml_config.rb
standard-0.1.9 lib/standard/loads_yaml_config.rb
standard-0.1.8 lib/standard/loads_yaml_config.rb
standard-0.1.7 lib/standard/loads_yaml_config.rb
standard-0.1.6 lib/standard/loads_yaml_config.rb
standard-0.1.5 lib/standard/loads_yaml_config.rb
standard-0.1.4 lib/standard/loads_yaml_config.rb
standard-0.1.3 lib/standard/loads_yaml_config.rb
standard-0.1.2 lib/standard/loads_yaml_config.rb
standard-0.1.1 lib/standard/loads_yaml_config.rb
standard-0.1.0 lib/standard/loads_yaml_config.rb
standard-0.0.41 lib/standard/loads_yaml_config.rb
standard-0.0.40 lib/standard/loads_yaml_config.rb
standard-0.0.39 lib/standard/loads_yaml_config.rb
standard-0.0.38 lib/standard/loads_yaml_config.rb
standard-0.0.37 lib/standard/loads_yaml_config.rb