Class: Pod::YAMLHelper

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

Overview

TODO:

Remove any code required solely for Ruby 1.8.x.

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.

Array Sorting collapse

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.



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

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

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



40
41
42
43
# File 'lib/cocoapods-core/yaml_helper.rb', line 40

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



74
75
76
# File 'lib/cocoapods-core/yaml_helper.rb', line 74

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



56
57
58
59
60
61
62
63
64
# File 'lib/cocoapods-core/yaml_helper.rb', line 56

def load_string(yaml_string, file_path = nil)
  YAML.safe_load(yaml_string, :permitted_classes => [Date, Time, Symbol])
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

.sorted_array(array) ⇒ Array

TODO:

This stuff is here only because the Lockfile intermixes strings and hashes for the ‘PODS` key. The Lockfile should be more consistent.

Note:

If the value contained in the array is another Array or a Hash the first value of the collection is used for sorting, as this method is more useful, for arrays which contains a collection composed by one object.

Sorts an array according to the string representation of it values. This method allows to sort arrays which contains strings or hashes.

Returns:

  • (Array)

    The sorted array.



256
257
258
259
260
# File 'lib/cocoapods-core/yaml_helper.rb', line 256

def sorted_array(array)
  array.each_with_index.sort_by do |element, index|
    [sorting_string(element), index]
  end.map(&:first)
end