Class: Rfmt::Configuration

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

Overview

Configuration management for rfmt

Defined Under Namespace

Classes: ConfigError

Constant Summary collapse

DEFAULT_CONFIG =
{
  'version' => '1.0',
  'formatting' => {
    'line_length' => 100,
    'indent_width' => 2,
    'indent_style' => 'spaces'
  },
  '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.



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

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



23
24
25
# File 'lib/rfmt/configuration.rb', line 23

def config
  @config
end

Class Method Details

.discoverObject

Discover configuration file in current directory



30
31
32
33
# File 'lib/rfmt/configuration.rb', line 30

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



36
37
38
39
40
41
42
43
44
# File 'lib/rfmt/configuration.rb', line 36

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

#formatting_configObject

Get formatting configuration



47
48
49
# File 'lib/rfmt/configuration.rb', line 47

def formatting_config
  @config['formatting']
end