Class: RuboCounsel::ConfigWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocounsel/config_writer.rb

Overview

Generates .rubocop.yml content from user choices.

Converts a hash of cop choices to YAML format and optionally writes to file. The choices hash maps cop names to their configuration settings:

{
"Style/StringLiterals" => { "EnforcedStyle" => "double_quotes" },
"Metrics/MethodLength" => { "Max" => 15 }
}

Constant Summary collapse

PRIORITY_KEYS =
["inherit_gem", "plugins", "inherit_from", "inherit_mode", "require", "AllCops"].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(choices) ⇒ ConfigWriter

Returns a new instance of ConfigWriter.



22
23
24
# File 'lib/rubocounsel/config_writer.rb', line 22

def initialize(choices)
  @choices = choices
end

Instance Attribute Details

#choicesObject (readonly)

Returns the value of attribute choices.



20
21
22
# File 'lib/rubocounsel/config_writer.rb', line 20

def choices
  @choices
end

Instance Method Details

#merge_and_write(path) ⇒ Object

Merges choices into an existing config file and writes it back.



38
39
40
41
42
# File 'lib/rubocounsel/config_writer.rb', line 38

def merge_and_write(path)
  existing = File.exist?(path) ? YAML.load_file(path) || {} : {}
  merged = sort_config(existing.deep_merge(choices))
  File.write(path, add_blank_lines(merged.to_yaml))
end

#to_yamlObject



26
27
28
29
30
# File 'lib/rubocounsel/config_writer.rb', line 26

def to_yaml
  return "" if choices.empty?

  add_blank_lines(sort_config(choices).to_yaml)
end

#write(path) ⇒ Object



32
33
34
35
# File 'lib/rubocounsel/config_writer.rb', line 32

def write(path)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, to_yaml)
end