Class: RubocopChallenger::Rubocop::ConfigEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop_challenger/rubocop/config_editor.rb

Overview

To edit rubocop_challenger config file

Constant Summary collapse

DEFAULT_FILE_PATH =
'.rubocop_challenge.yml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path: DEFAULT_FILE_PATH) ⇒ ConfigEditor

Returns a new instance of ConfigEditor.



13
14
15
16
# File 'lib/rubocop_challenger/rubocop/config_editor.rb', line 13

def initialize(file_path: DEFAULT_FILE_PATH)
  @file_path = file_path
  @data = FileTest.exist?(file_path) ? YAML.load_file(file_path) : {}
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



11
12
13
# File 'lib/rubocop_challenger/rubocop/config_editor.rb', line 11

def data
  @data
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



11
12
13
# File 'lib/rubocop_challenger/rubocop/config_editor.rb', line 11

def file_path
  @file_path
end

Instance Method Details

#add_ignore(*rules) ⇒ Object

Add ignore rule to the config data

Parameters:



28
29
30
31
32
# File 'lib/rubocop_challenger/rubocop/config_editor.rb', line 28

def add_ignore(*rules)
  data['Ignore'] ||= []
  rules.each { |rule| data['Ignore'] << rule.title }
  data['Ignore'].sort!.uniq!
end

#ignored_rulesArray<String>

Get ignored rules

Returns:

  • (Array<String>)

    Ignored rules



21
22
23
# File 'lib/rubocop_challenger/rubocop/config_editor.rb', line 21

def ignored_rules
  data['Ignore'] || []
end

#saveObject

Save setting to the config file as YAML



35
36
37
38
39
# File 'lib/rubocop_challenger/rubocop/config_editor.rb', line 35

def save
  File.open(file_path, 'w') do |file|
    YAML.dump(data, file)
  end
end