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



174
175
176
# File 'lib/rfmt.rb', line 174

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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rfmt.rb', line 140

def self.find
  current_dir = Dir.pwd

  loop do
    # Same search order as the Rust side (config/mod.rs CONFIG_FILE_NAMES)
    ['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



131
132
133
134
135
136
# File 'lib/rfmt.rb', line 131

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



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rfmt.rb', line 181

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