Class: Rfmt::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/rfmt/configuration.rb

Overview

File selection and cache concerns only: formatter settings (indent_width etc.) live in the Rust Config, reached via Rfmt.format(config_path:).

Defined Under Namespace

Classes: ConfigError

Constant Summary collapse

DEFAULT_CONFIG =
{
  'version' => '1.0',
  'include' => ['**/*.rb'],
  'exclude' => ['vendor/**/*', 'tmp/**/*', 'node_modules/**/*']
}.freeze
CONFIG_FILES =
['rfmt.yml', 'rfmt.yaml', '.rfmt.yml', '.rfmt.yaml'].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



21
22
23
# File 'lib/rfmt/configuration.rb', line 21

def initialize(options = {})
  @config = load_configuration(options)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



19
20
21
# File 'lib/rfmt/configuration.rb', line 19

def config
  @config
end

Class Method Details

.discoverObject

Discover configuration file in current directory



26
27
28
29
# File 'lib/rfmt/configuration.rb', line 26

def self.discover
  config_file = CONFIG_FILES.find { |file| File.exist?(file) }
  config_file ? new(file: config_file) : new
end

Instance Method Details

#files_to_format(base_path: '.') ⇒ Object

Get list of files to format based on include/exclude patterns



32
33
34
35
36
37
38
39
40
# File 'lib/rfmt/configuration.rb', line 32

def files_to_format(base_path: '.')
  include_patterns = @config['include']
  exclude_patterns = @config['exclude']

  included_files = include_patterns.flat_map { |pattern| Dir.glob(File.join(base_path, pattern)) }
  excluded_files = exclude_patterns.flat_map { |pattern| Dir.glob(File.join(base_path, pattern)) }

  (included_files - excluded_files).select { |f| File.file?(f) }
end