Class: Marcdouane::FileChecker
- Inherits:
-
Object
- Object
- Marcdouane::FileChecker
- Defined in:
- lib/marcdouane/file_checker.rb
Overview
FileChecker grabs all rules available and then calls them on a file, transmitting any options fed to the CLI. and then calls all the rules
Class Attribute Summary collapse
-
.exit_code ⇒ Object
readonly
Returns the value of attribute exit_code.
Class Method Summary collapse
- .call(file, options) ⇒ Object
- .parse_config!(path) ⇒ Object
- .print_error(file, rule, line_number, msg) ⇒ Object
- .rules ⇒ Object
- .run_rule(file, klass, options) ⇒ Object
Class Attribute Details
.exit_code ⇒ Object (readonly)
Returns the value of attribute exit_code.
14 15 16 |
# File 'lib/marcdouane/file_checker.rb', line 14 def exit_code @exit_code end |
Class Method Details
.call(file, options) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/marcdouane/file_checker.rb', line 16 def call(file, ) verbose = .fetch(:verbose) parse_config!([:config]) unless [:config].nil? puts "Checking `#{file}'..." if verbose @exit_code = 0 rules .map { |klass| run_rule(file, klass, ) } .tap { |_codes| puts "Done." if verbose } .then { @exit_code } end |
.parse_config!(path) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/marcdouane/file_checker.rb', line 55 def parse_config!(path) config = YAML.load_file(path) config.each do |klass, hash| hash.each do |key, value| rule_class = "Marcdouane::Rules::#{klass}" const_get(rule_class).class_eval do self.config[key.to_sym] = value end end end end |
.print_error(file, rule, line_number, msg) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/marcdouane/file_checker.rb', line 43 def print_error(file, rule, line_number, msg) warn( format( "%<file>s:%<line_number>s: [%<rule_class>s] %<message>s", file:, line_number:, rule_class: rule.identifier, message: msg ) ) end |
.rules ⇒ Object
69 70 71 |
# File 'lib/marcdouane/file_checker.rb', line 69 def rules Marcdouane::Rules::Rule.subclasses end |
.run_rule(file, klass, options) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/marcdouane/file_checker.rb', line 31 def run_rule(file, klass, ) rule = klass.new(file, ) rule.subscribe("rule.error") do |event| print_error(file, rule, event[:line_number], event[:msg]) @exit_code = 1 end rule.check! end |