Class: PreCommit::Checks::Json

Inherits:
Plugin
  • Object
show all
Defined in:
lib/plugins/pre_commit/checks/json.rb

Instance Attribute Summary

Attributes inherited from Plugin

#config, #pluginator

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

#initialize, #name

Constructor Details

This class inherits a constructor from PreCommit::Checks::Plugin

Class Method Details

.descriptionObject



23
24
25
# File 'lib/plugins/pre_commit/checks/json.rb', line 23

def self.description
  'Runs json to detect errors.'
end

Instance Method Details

#call(staged_files) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/plugins/pre_commit/checks/json.rb', line 7

def call(staged_files)
  staged_files = staged_files.grep(/\.json$/)
  return if staged_files.empty?

  errors = staged_files.map {|file| load_file(file)}.compact

  errors.join("\n") + "\n" unless errors.empty?
end

#load_file(file) ⇒ Object



16
17
18
19
20
21
# File 'lib/plugins/pre_commit/checks/json.rb', line 16

def load_file(file)
  File.open(file) {|io| JSON.load(io)}
  nil
rescue JSON::ParserError => e
  "#{e.message} parsing #{file}"
end