Class: Yamlint::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/yamlint/formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Formatter

Returns a new instance of Formatter.



10
11
12
# File 'lib/yamlint/formatter.rb', line 10

def initialize(config = nil)
  @config = config || Config.load_default
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/yamlint/formatter.rb', line 8

def config
  @config
end

Instance Method Details

#format(content, dry_run: false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/yamlint/formatter.rb', line 14

def format(content, dry_run: false)
  original = content.dup
  result = content

  result = fix_trailing_spaces(result)
  result = fix_new_line_at_end(result)
  result = fix_new_lines(result)
  result = fix_empty_lines(result)

  return original if dry_run

  if safe_fix?(original, result)
    result
  else
    original
  end
end

#format_file(filepath, dry_run: false) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/yamlint/formatter.rb', line 32

def format_file(filepath, dry_run: false)
  content = File.read(filepath)
  formatted = format(content, dry_run:)

  File.write(filepath, formatted) unless dry_run || content == formatted

  formatted
end