Class: YamlLint

Inherits:
Object
  • Object
show all
Defined in:
lib/learn-tool/yaml-linter.rb

Class Method Summary collapse

Class Method Details

.check_attributes(file) ⇒ Object



35
36
37
38
# File 'lib/learn-tool/yaml-linter.rb', line 35

def self.check_attributes(file)
  file_string = file_lines(file)
  file_string.match(/languages/)
end

.file_lines(file) ⇒ Object



40
41
42
# File 'lib/learn-tool/yaml-linter.rb', line 40

def self.file_lines(file)
  f = File.read(file)
end

.parse_file(file, learn_error) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/learn-tool/yaml-linter.rb', line 6

def self.parse_file(file, learn_error)
  begin
    YAML.load_file(file)
  rescue Exception => err
    learn_error.valid_yaml = {message: "#{err}", color: :red}
  else
    learn_error.yaml_error[:valid_yaml] = true
    if check_attributes(file)
      learn_error.yaml_error[:attributes] = true
      learn_error.correct_yaml_content = {message: "valid attribute key names", color: :green}
    end
    if self.validate_whitespace_for_learn(file)
      learn_error.yaml_error[:valid_whitespace] = true
      learn_error.valid_yaml = {message: "valid yaml and valid whitespace.", color: :green}
    else 
      learn_error.valid_yaml = {message: "valid yaml but invalid whitespace", color: :red}
    end
  end
end

.validate_whitespace_for_learn(file) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/learn-tool/yaml-linter.rb', line 26

def self.validate_whitespace_for_learn(file)
  lines = file_lines(file).split("\n")
  attributes = lines.select { |line| line.include?("-") }
  attributes.each do |attribute| 
    return false unless attribute[0..3] == "  - "
  end
  true
end