Class: Pod::YAMLHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-core/yaml_helper.rb

Overview

Note:

This class misses important features necessary for a correct YAML serialization and thus it is safe to use only for the Lockfile. The missing features include:

  • Strings are never quoted even when ambiguous.

Converts objects to their YAML representation.

This class was created for the need having control on how the YAML is representation is generated. In details it provides:

  • sorting for hashes in ruby 1.8.x

  • ability to hint the sorting of the keys of a dictionary when converting it. In this case the keys are also separated by an additional new line feed for readability.

Class Method Summary collapse

Class Method Details

.convert(value) ⇒ String

Returns the YAML representation of the given object. If the given object is a Hash, it accepts an optional hint for sorting the keys.

Parameters:

  • object (String, Symbol, Array, Hash)

    the object to convert

  • hash_keys_hint (Array)

    an array to use as a hint for sorting the keys of the object to convert if it is a hash.

Returns:

  • (String)

    the YAML representation of the given object.



31
32
33
34
# File 'lib/cocoapods-core/yaml_helper.rb', line 31

def convert(value)
  result = process_according_to_class(value)
  result << "\n"
end

.convert_hash(value, hash_keys_hint, line_separator = "\n") ⇒ Object



36
37
38
39
# File 'lib/cocoapods-core/yaml_helper.rb', line 36

def convert_hash(value, hash_keys_hint, line_separator = "\n")
  result = process_hash(value, hash_keys_hint, line_separator)
  result << "\n"
end

.load_file(file_path) ⇒ Hash, Array

Loads a YAML file and leans on the #load_string imp to do error detection

Parameters:

  • file_path (Pathname)

    The file path to be used for read for the YAML file

Returns:

  • (Hash, Array)

    the Ruby YAML representaton



70
71
72
# File 'lib/cocoapods-core/yaml_helper.rb', line 70

def load_file(file_path)
  load_string(File.read(file_path), file_path)
end

.load_string(yaml_string, file_path = nil) ⇒ Hash, Array

Loads a YAML string and provide more informative error messages in special cases like merge conflict.

Parameters:

  • yaml_string (String)

    The YAML String to be loaded

  • file_path (Pathname) (defaults to: nil)

    The (optional) file path to be used for read for the YAML file

Returns:

  • (Hash, Array)

    the Ruby YAML representaton



52
53
54
55
56
57
58
59
60
# File 'lib/cocoapods-core/yaml_helper.rb', line 52

def load_string(yaml_string, file_path = nil)
  YAML.load(yaml_string)
  rescue
    if yaml_has_merge_error?(yaml_string)
      raise Informative, yaml_merge_conflict_msg(yaml_string, file_path)
    else
      raise Informative, yaml_parsing_error_msg(yaml_string, file_path)
    end
end