Class: Lumin::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/lumin/config.rb,
lib/lumin/config/loader.rb,
lib/lumin/config/validator.rb

Defined Under Namespace

Classes: ConfigError, Loader, Validator

Constant Summary collapse

DEFAULTS =
{
  "target_ruby" => "3.3",
  "include" => ["**/*.rb"],
  "exclude" => ["vendor/**/*"],
  "rules" => {}
}.freeze
TOP_LEVEL_KEYS =
(DEFAULTS.keys + ["inherit_from"]).freeze
RULE_CONTROL_KEYS =
%w[enabled autocorrect].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, path:, registry: Registry) ⇒ Config

Returns a new instance of Config.



47
48
49
50
51
52
53
# File 'lib/lumin/config.rb', line 47

def initialize(data, path:, registry: Registry)
  @data = self.class.deep_merge(DEFAULTS, self.class.stringify_keys(data))
  @path = path
  Validator.new(@data, registry).validate!
  @data = deep_freeze(@data)
  @digest = Digest::SHA256.hexdigest(JSON.generate(canonical(@data)))
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#digestObject (readonly)

Returns the value of attribute digest.



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

def digest
  @digest
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.deep_merge(base, override) ⇒ Object



33
34
35
36
37
# File 'lib/lumin/config.rb', line 33

def self.deep_merge(base, override)
  base.merge(override) do |_key, left, right|
    left.is_a?(Hash) && right.is_a?(Hash) ? deep_merge(left, right) : right
  end
end

.from_hash(data = nil, registry: Registry, **values) ⇒ Object



28
29
30
31
# File 'lib/lumin/config.rb', line 28

def self.from_hash(data = nil, registry: Registry, **values)
  data ||= values
  new(deep_merge(DEFAULTS, stringify_keys(data)), path: nil, registry: registry)
end

.load(path = ".lumin.yml", registry: Registry) ⇒ Object



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

def self.load(path = ".lumin.yml", registry: Registry)
  data = File.file?(path) ? Loader.new.load_file(File.expand_path(path)) : DEFAULTS
  new(data, path: File.expand_path(path), registry: registry)
end

.stringify_keys(value) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/lumin/config.rb', line 39

def self.stringify_keys(value)
  case value
  when Hash then value.to_h { |key, child| [key.to_s, stringify_keys(child)] }
  when Array then value.map { |child| stringify_keys(child) }
  else value
  end
end

Instance Method Details

#enabled_rules(registry = Registry) ⇒ Object



71
72
73
74
75
76
# File 'lib/lumin/config.rb', line 71

def enabled_rules(registry = Registry)
  registry.all.filter_map do |name, rule_class|
    settings = rule_config(name)
    [name, rule_class, settings] unless settings[:enabled] == false
  end
end

#exclude_patternsObject



63
64
65
# File 'lib/lumin/config.rb', line 63

def exclude_patterns
  data.fetch("exclude")
end

#include_patternsObject



59
60
61
# File 'lib/lumin/config.rb', line 59

def include_patterns
  data.fetch("include")
end

#rule_config(name) ⇒ Object



67
68
69
# File 'lib/lumin/config.rb', line 67

def rule_config(name)
  data.fetch("rules").fetch(name, {}).transform_keys(&:to_sym)
end

#target_rubyObject



55
56
57
# File 'lib/lumin/config.rb', line 55

def target_ruby
  data.fetch("target_ruby")
end