Class: PlatformosCheck::YamlFile

Inherits:
AppFile
  • Object
show all
Defined in:
lib/platformos_check/yaml_file.rb

Instance Attribute Summary

Attributes inherited from AppFile

#storage, #version

Instance Method Summary collapse

Methods inherited from AppFile

#==, #build_name, #dir_names, #dir_prefix, #graphql?, #liquid?, #module_name, #module_original_file?, #module_original_file_path, #module_overwrite_file?, #module_overwrite_file_path, #module_prefix, #name, #page?, #partial?, #path, #relative_path, #remove_extension, #source, #translation?

Constructor Details

#initialize(relative_path, storage) ⇒ YamlFile

Returns a new instance of YamlFile.



7
8
9
10
11
12
# File 'lib/platformos_check/yaml_file.rb', line 7

def initialize(relative_path, storage)
  super
  @loaded = false
  @content = nil
  @parser_error = nil
end

Instance Method Details

#contentObject



14
15
16
17
# File 'lib/platformos_check/yaml_file.rb', line 14

def content
  load!
  @content
end

#content_to_stringObject



36
37
38
39
40
41
42
43
44
# File 'lib/platformos_check/yaml_file.rb', line 36

def content_to_string
  pretty = YAML.dump(@content)
  return unless source.rstrip != pretty.rstrip

  # Most editors add a trailing \n at the end of files. Here we
  # try to maintain the convention.
  eof = source.end_with?("\n") ? "\n" : ""
  pretty.gsub("\n", @eol) + eof
end

#parse_errorObject



19
20
21
22
# File 'lib/platformos_check/yaml_file.rb', line 19

def parse_error
  load!
  @parser_error
end

#update_contents(new_content = {}) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
# File 'lib/platformos_check/yaml_file.rb', line 24

def update_contents(new_content = {})
  raise ArgumentError if new_content.is_a?(String)

  @content = new_content
end

#writeObject



30
31
32
33
34
# File 'lib/platformos_check/yaml_file.rb', line 30

def write
  pretty = content_to_string
  @storage.write(@relative_path, pretty)
  @source = pretty
end

#yaml?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/platformos_check/yaml_file.rb', line 46

def yaml?
  true
end