Module: Rfmt::Config

Defined in:
lib/rfmt.rb

Overview

Configuration management

Constant Summary collapse

DEFAULT_CONFIG =

Default configuration template

<<~YAML
  # rfmt Configuration File
  # This file controls how rfmt formats your Ruby code.
  # See https://github.com/fs0414/rfmt for full documentation.

  version: "1.0"

  # Formatting options
  formatting:
    # Maximum line length before wrapping (40-500)
    line_length: 100

    # Number of spaces or tabs per indentation level (1-8)
    indent_width: 2

    # Use "spaces" or "tabs" for indentation
    indent_style: "spaces"

    # Quote style for strings: "double", "single", or "consistent"
    quote_style: "double"

  # Files to include in formatting (glob patterns)
  include:
    - "**/*.rb"
    - "**/*.rake"
    - "**/Rakefile"
    - "**/Gemfile"

  # Files to exclude from formatting (glob patterns)
  exclude:
    - "vendor/**/*"
    - "tmp/**/*"
    - "node_modules/**/*"
    - "db/schema.rb"
YAML

Class Method Summary collapse

Class Method Details

.exists?Boolean

Check if configuration file exists

Returns:

  • (Boolean)

    true if config file exists



146
147
148
# File 'lib/rfmt.rb', line 146

def self.exists?
  !find.nil?
end

.findString?

Find configuration file in current or parent directories

Returns:

  • (String, nil)

    Path to config file or nil if not found



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/rfmt.rb', line 113

def self.find
  current_dir = Dir.pwd

  loop do
    ['.rfmt.yml', '.rfmt.yaml', 'rfmt.yml', 'rfmt.yaml'].each do |filename|
      config_path = File.join(current_dir, filename)
      return config_path if File.exist?(config_path)
    end

    parent = File.dirname(current_dir)
    break if parent == current_dir # Reached root

    current_dir = parent
  end

  # Check user home directory
  home_dir = begin
    Dir.home
  rescue StandardError
    nil
  end
  if home_dir
    ['.rfmt.yml', '.rfmt.yaml', 'rfmt.yml', 'rfmt.yaml'].each do |filename|
      config_path = File.join(home_dir, filename)
      return config_path if File.exist?(config_path)
    end
  end

  nil
end

.init(path = '.rfmt.yml', force: false) ⇒ Boolean

Generate a default configuration file

Parameters:

  • path (String) (defaults to: '.rfmt.yml')

    Path where to create the config file (default: .rfmt.yml)

  • force (Boolean) (defaults to: false)

    Overwrite existing file if true

Returns:

  • (Boolean)

    true if file was created, false if already exists



104
105
106
107
108
109
# File 'lib/rfmt.rb', line 104

def self.init(path = '.rfmt.yml', force: false)
  return false if File.exist?(path) && !force

  File.write(path, DEFAULT_CONFIG)
  true
end

.load(path = nil) ⇒ Hash

Load and validate configuration file

Parameters:

  • path (String, nil) (defaults to: nil)

    Path to config file (default: auto-detect)

Returns:

  • (Hash)

    Loaded configuration



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rfmt.rb', line 153

def self.load(path = nil)
  require 'yaml'

  config_path = path || find

  unless config_path
    warn 'No configuration file found, using defaults'
    return {}
  end

  YAML.load_file(config_path)
rescue Errno::ENOENT
  raise Error, "Configuration file not found: #{config_path}"
rescue Psych::SyntaxError => e
  raise Error, "Invalid YAML in configuration file: #{e.message}"
end