Class: Txray::Config

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

Constant Summary collapse

FILENAME =
".txray.yml"
DEFAULTS =
{
  "include" => [ "app", "lib", "db/migrate" ],
  "exclude" => %w[spec test vendor node_modules tmp log .git],
  "max_depth" => 3,
  "fail_level" => "low",
  "disabled_rules" => [],
  "external_clients" => [],
  "severities" => {},
  "runtime" => {
    "enabled" => false,
    "threshold_ms" => 250,
    "on_violation" => "log",
    "log_path" => "tmp/txray.ndjson",
    "ignore" => [],
    "guard_http" => true,
    "guard_jobs" => true,
    "guard_mail" => true
  }
}.freeze
ARRAY_KEYS =
%w[include exclude disabled_rules external_clients].freeze
HASH_KEYS =
%w[severities runtime].freeze
FAIL_LEVELS =
%w[low medium high none].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Config

Returns a new instance of Config.



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

def initialize(data = {})
  validate!(data)
  @data = DEFAULTS.merge(data) { |_key, default, given| default.is_a?(Hash) ? default.merge(given) : given }
end

Class Method Details

.discover(from = Dir.pwd) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/txray/config.rb', line 30

def self.discover(from = Dir.pwd)
  Pathname.new(from).ascend do |directory|
    candidate = directory.join(FILENAME)
    return candidate.to_s if candidate.file?
  end
  nil
end

.load(path = nil) ⇒ Object

Raises:



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

def self.load(path = nil)
  raise Error, "no such config file: #{path}" if path && !File.exist?(path)

  path ||= discover
  data = path ? YAML.safe_load_file(path) : {}
  new(data.is_a?(Hash) ? data : {})
end

Instance Method Details

#disabled_rulesObject



59
# File 'lib/txray/config.rb', line 59

def disabled_rules = Array(@data["disabled_rules"]).map(&:to_s)

#excludesObject



56
# File 'lib/txray/config.rb', line 56

def excludes = Array(@data["exclude"])

#external_clientsObject



58
# File 'lib/txray/config.rb', line 58

def external_clients = Array(@data["external_clients"]).map(&:to_s)

#fail_levelObject



62
# File 'lib/txray/config.rb', line 62

def fail_level = @data["fail_level"].to_s.to_sym

#includesObject



55
# File 'lib/txray/config.rb', line 55

def includes = Array(@data["include"])

#max_depthObject



57
# File 'lib/txray/config.rb', line 57

def max_depth = Integer(@data["max_depth"])

#merge(overrides) ⇒ Object



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

def merge(overrides)
  self.class.new(@data.merge(overrides.compact.transform_keys(&:to_s)))
end

#rule(id) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/txray/config.rb', line 86

def rule(id)
  severity = @data["severities"].to_h[id.to_s]
  base = Rules[id]
  return base unless severity

  base.dup.tap { |copy| copy.severity = severity.to_sym }
end

#rule_enabled?(id) ⇒ Boolean

Returns:

  • (Boolean)


64
# File 'lib/txray/config.rb', line 64

def rule_enabled?(id) = !disabled_rules.include?(id.to_s)

#runtimeObject



60
# File 'lib/txray/config.rb', line 60

def runtime = @data["runtime"].transform_keys(&:to_s)

#to_hObject



100
# File 'lib/txray/config.rb', line 100

def to_h = @data