Class: Collie::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/collie/config.rb

Overview

Configuration management

Constant Summary collapse

DEFAULT_CONFIG =
{
  "rules" => {},
  "formatter" => {
    "indent_size" => 2,
    "align_tokens" => true,
    "align_alternatives" => true,
    "blank_lines_around_sections" => 1,
    "max_line_length" => 120
  },
  "include" => ["**/*.y"],
  "exclude" => ["vendor/**/*", "tmp/**/*"]
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path = nil) ⇒ Config

Returns a new instance of Config.



23
24
25
# File 'lib/collie/config.rb', line 23

def initialize(config_path = nil)
  @config = load_config(config_path)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/collie/config.rb', line 21

def config
  @config
end

Class Method Details

.defaultObject



50
51
52
# File 'lib/collie/config.rb', line 50

def self.default
  new
end

.generate_default(path = ".collie.yml") ⇒ Object



54
55
56
# File 'lib/collie/config.rb', line 54

def self.generate_default(path = ".collie.yml")
  File.write(path, DEFAULT_CONFIG.to_yaml)
end

Instance Method Details

#excluded_patternsObject



46
47
48
# File 'lib/collie/config.rb', line 46

def excluded_patterns
  @config["exclude"] || DEFAULT_CONFIG["exclude"]
end

#formatter_optionsObject



38
39
40
# File 'lib/collie/config.rb', line 38

def formatter_options
  @config["formatter"] || DEFAULT_CONFIG["formatter"]
end

#included_patternsObject



42
43
44
# File 'lib/collie/config.rb', line 42

def included_patterns
  @config["include"] || DEFAULT_CONFIG["include"]
end

#rule_config(rule_name) ⇒ Object



34
35
36
# File 'lib/collie/config.rb', line 34

def rule_config(rule_name)
  @config.dig("rules", rule_name) || {}
end

#rule_enabled?(rule_name) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
# File 'lib/collie/config.rb', line 27

def rule_enabled?(rule_name)
  rule_config = @config.dig("rules", rule_name)
  return true if rule_config.nil? # Enabled by default

  rule_config.is_a?(Hash) ? rule_config.fetch("enabled", true) : rule_config
end