Class: MiniYaml::Linter

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_yaml/linter.rb

Defined Under Namespace

Classes: State

Instance Method Summary collapse

Constructor Details

#initialize(yaml, paranoid: true, columns: 80) ⇒ Linter

Returns a new instance of Linter.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mini_yaml/linter.rb', line 6

def initialize(yaml, paranoid: true, columns: 80)
  @comment_map = nil
  @parsed = parse(yaml)
  @columns = columns

  if paranoid
    before, before_e, after, after_e = nil

    begin
      before = YAML.load(yaml)
    rescue => e
      before_e = e
    end

    begin
      after = YAML.load(dump)
    rescue => e
      after_e = e
    end

    if before != after
      STDERR.puts "ERROR YAML mismatch:"
      STDERR.puts
      STDERR.puts "BEFORE TEXT"
      STDERR.puts yaml
      STDERR.puts "AFTER"
      STDERR.puts dump

      STDERR.puts "BEFORE OBJ"
      STDERR.puts before.inspect
      if before_e
        STDERR.puts before_e
      end
      STDERR.puts "AFTER OBJ"
      STDERR.puts after.inspect
      if after_e
        STDERR.puts after_e
      end

      raise "Failed to correctly generate YAML"
    end
  end
end

Instance Method Details

#contentsObject

malliable array/hash structure



51
52
53
# File 'lib/mini_yaml/linter.rb', line 51

def contents
  @parsed
end

#dumpObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mini_yaml/linter.rb', line 55

def dump
  buffer = +"---\n"

  if leading = @comment_map[:leading]
    buffer << leading
    buffer << "\n"
  end

  _dump(node: @parsed, buffer: buffer, indent: 0, path: "", prev_indent: 0)

  if trailing = @comment_map[:trailing]
    buffer << trailing
  end

  buffer
end