Class: Pod::YAMLHelper
- Inherits:
-
Object
- Object
- Pod::YAMLHelper
- Defined in:
- lib/cocoapods-core/yaml_helper.rb
Overview
Remove any code required solely for Ruby 1.8.x.
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
-
.sorted_array(array) ⇒ Array
Sorts an array according to the string representation of it values.
Class Method Summary collapse
-
.convert(value) ⇒ String
Returns the YAML representation of the given object.
- .convert_hash(value, hash_keys_hint, line_separator = "\n") ⇒ Object
-
.load_file(file_path) ⇒ Hash, Array
Loads a YAML file and leans on the #load_string imp to do error detection.
-
.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.
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.
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
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.
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
This stuff is here only because the Lockfile intermixes strings and hashes for the ‘PODS` key. The Lockfile should be more consistent.
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.
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 |