Class: Goodcheck::ConfigLoader

Inherits:
Object
  • Object
show all
Includes:
ArrayHelper
Defined in:
lib/goodcheck/config_loader.rb

Defined Under Namespace

Classes: InvalidPattern

Constant Summary collapse

Schema =
StrongJSON.new do
  let :regexp_pattern, object(regexp: string, case_insensitive: boolean?, multiline: boolean?)
  let :literal_pattern, object(literal: string, case_insensitive: boolean?)
  let :token_pattern, object(token: string)
  let :pattern, enum(regexp_pattern, literal_pattern, token_pattern, string)

  let :encoding, enum(*Encoding.name_list.map {|name| literal(name) })
  let :glob, object(pattern: string, encoding: optional(encoding))

  let :rule, object(
    id: string,
    pattern: enum(array(pattern), pattern),
    message: string,
    justification: optional(enum(array(string), string)),
    glob: optional(enum(array(enum(glob, string)), glob, string)),
    pass: optional(enum(array(string), string)),
    fail: optional(enum(array(string), string))
  )

  let :rules, array(rule)

  let :config, object(rules: rules)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ArrayHelper

#array

Constructor Details

#initialize(path:, content:) ⇒ ConfigLoader

Returns a new instance of ConfigLoader.



34
35
36
37
# File 'lib/goodcheck/config_loader.rb', line 34

def initialize(path:, content:)
  @path = path
  @content = content
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



32
33
34
# File 'lib/goodcheck/config_loader.rb', line 32

def content
  @content
end

#pathObject (readonly)

Returns the value of attribute path.



31
32
33
# File 'lib/goodcheck/config_loader.rb', line 31

def path
  @path
end

Instance Method Details

#loadObject



39
40
41
42
43
# File 'lib/goodcheck/config_loader.rb', line 39

def load
  Schema.config.coerce(content)
  rules = content[:rules].map {|hash| load_rule(hash) }
  Config.new(rules: rules)
end

#load_globs(globs) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/goodcheck/config_loader.rb', line 57

def load_globs(globs)
  globs.map do |glob|
    case glob
    when String
      Glob.new(pattern: glob, encoding: nil)
    when Hash
      Glob.new(pattern: glob[:pattern], encoding: glob[:encoding])
    end
  end
end

#load_pattern(pattern) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/goodcheck/config_loader.rb', line 68

def load_pattern(pattern)
  case pattern
  when String
    Pattern.literal(pattern, case_insensitive: false)
  when Hash
    case
    when pattern[:literal]
      ci = pattern[:case_insensitive]
      literal = pattern[:literal]
      Pattern.literal(literal, case_insensitive: ci)
    when pattern[:regexp]
      regexp = pattern[:regexp]
      ci = pattern[:case_insensitive]
      multiline = pattern[:multiline]
      Pattern.regexp(regexp, case_insensitive: ci, multiline: multiline)
    when pattern[:token]
      tok = pattern[:token]
      Pattern.token(tok)
    end
  end
end

#load_rule(hash) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/goodcheck/config_loader.rb', line 45

def load_rule(hash)
  id = hash[:id]
  patterns = array(hash[:pattern]).map {|pat| load_pattern(pat) }
  justifications = array(hash[:justification])
  globs = load_globs(array(hash[:glob]))
  message = hash[:message].chomp
  passes = array(hash[:pass])
  fails = array(hash[:fail])

  Rule.new(id: id, patterns: patterns, justifications: justifications, globs: globs, message: message, passes: passes, fails: fails)
end