Class: Collie::Config

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

Overview

Configuration management

Defined Under Namespace

Modules: Schema

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
PROFILE_OVERRIDES =
{
  "default" => {},
  "lrama" => {
    "rules" => {
      "LeftRecursion" => { "enabled" => false },
      "FactorizableRules" => { "enabled" => false },
      "RightRecursion" => { "severity" => "warning" }
    }
  },
  "bison" => {
    "rules" => {
      "LeftRecursion" => { "enabled" => false },
      "FactorizableRules" => { "enabled" => false },
      "RightRecursion" => { "severity" => "warning" }
    }
  },
  "strict" => {
    "formatter" => {
      "max_line_length" => 100
    },
    "rules" => {
      "AmbiguousPrecedence" => { "severity" => "warning" },
      "ConsistentTagNaming" => { "severity" => "warning" },
      "EmptyAction" => { "severity" => "warning" },
      "FactorizableRules" => { "severity" => "warning" },
      "LongRule" => { "severity" => "warning" },
      "NonterminalNaming" => { "severity" => "warning" },
      "PrecImprovement" => { "severity" => "warning" },
      "RedundantEpsilon" => { "severity" => "warning" },
      "TokenNaming" => { "severity" => "warning" },
      "TrailingWhitespace" => { "severity" => "warning" }
    }
  },
  "minimal" => {
    "rules" => {
      "AmbiguousPrecedence" => { "enabled" => false },
      "ConsistentTagNaming" => { "enabled" => false },
      "EmptyAction" => { "enabled" => false },
      "FactorizableRules" => { "enabled" => false },
      "LeftRecursion" => { "enabled" => false },
      "LongRule" => { "enabled" => false },
      "NonterminalNaming" => { "enabled" => false },
      "PrecImprovement" => { "enabled" => false },
      "RedundantEpsilon" => { "enabled" => false },
      "RightRecursion" => { "enabled" => false },
      "TokenNaming" => { "enabled" => false },
      "TrailingWhitespace" => { "enabled" => false },
      "UnreachableRule" => { "enabled" => false },
      "UnusedNonterminal" => { "enabled" => false },
      "UnusedToken" => { "enabled" => false }
    }
  }
}.freeze
PROFILE_NAMES =
PROFILE_OVERRIDES.keys.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.



79
80
81
# File 'lib/collie/config.rb', line 79

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



77
78
79
# File 'lib/collie/config.rb', line 77

def config
  @config
end

Class Method Details

.deep_merge(hash1, hash2) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/collie/config.rb', line 125

def self.deep_merge(hash1, hash2)
  hash1.merge(hash2) do |_key, old_val, new_val|
    if old_val.is_a?(Hash) && new_val.is_a?(Hash)
      deep_merge(old_val, new_val)
    else
      new_val
    end
  end
end

.defaultObject



106
107
108
# File 'lib/collie/config.rb', line 106

def self.default
  new
end

.generate_default(path = ".collie.yml", profile: "default") ⇒ Object



110
111
112
# File 'lib/collie/config.rb', line 110

def self.generate_default(path = ".collie.yml", profile: "default")
  File.write(path, profile_config(profile).to_yaml)
end

.profile_config(profile) ⇒ Object

Raises:



114
115
116
117
118
119
# File 'lib/collie/config.rb', line 114

def self.profile_config(profile)
  profile_name = profile.to_s
  raise Error, "Unknown config profile: #{profile}" unless PROFILE_OVERRIDES.key?(profile_name)

  deep_merge(DEFAULT_CONFIG, PROFILE_OVERRIDES.fetch(profile_name))
end

.schemaObject



121
122
123
# File 'lib/collie/config.rb', line 121

def self.schema
  Schema.to_h
end

Instance Method Details

#excluded_patternsObject



102
103
104
# File 'lib/collie/config.rb', line 102

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

#formatter_optionsObject



94
95
96
# File 'lib/collie/config.rb', line 94

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

#included_patternsObject



98
99
100
# File 'lib/collie/config.rb', line 98

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

#rule_config(rule_name) ⇒ Object



90
91
92
# File 'lib/collie/config.rb', line 90

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

#rule_enabled?(rule_name) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
# File 'lib/collie/config.rb', line 83

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