module YPS
YPS: YAML Positioning Sysmte
YPS is a gem to parse YAML and add position information (file name, line and column) to each parsed elements. This is useful for error reporting and debugging, allowing developers to precisely locate an issue within the original YAML file.
Constants
- VERSION
-
The version of
YPS
Public Class Methods
Source
# File lib/yps.rb 83 def load(yaml, permitted_classes: [Symbol], **kwargs) 84 safe_load(yaml, permitted_classes:, **kwargs) 85 end
Similar to YPS.safe_load, but Symbol is allowed to be loaded by default.
See also Psych.load.
Source
# File lib/yps.rb 99 def load_file(filename, **kwargs) 100 open_file(filename) { |f| load(f, filename:, **kwargs) } 101 end
Source
# File lib/yps.rb 135 def load_stream(yaml, permitted_classes: [Symbol], **kwargs) 136 safe_load_stream(yaml, permitted_classes:, **kwargs) 137 end
Similar to YPS.safe_load_sream, but Symbol is allowed to be loaded by default.
See also YPS.safe_load_stream
Source
# File lib/yps.rb 153 def load_stream_file(filename, **kwargs) 154 open_file(filename) { |f| load_stream(f, filename:, **kwargs) } 155 end
Similar to YPS.load_stream, but the YAML string is read from the file specified by the filename argument.
See also YPS.load_stream
Source
# File lib/yps.rb 61 def safe_load( # rubocop:disable Metrics/ParameterLists 62 yaml, 63 permitted_classes: [], permitted_symbols: [], unwrapped_classes: [], 64 aliases: false, filename: nil, fallback: nil, symbolize_names: false, 65 freeze: false, strict_integer: false, value_class: Value 66 ) 67 Parser.parse(yaml, filename) do |node| 68 visitor = 69 Visitor.create( 70 permitted_classes, permitted_symbols, unwrapped_classes, 71 aliases, symbolize_names, freeze, strict_integer, value_class 72 ) 73 return visitor.accept(node) 74 end 75 76 fallback 77 end
Safely load the YAML string in yaml and add position information (file name line and column) to each parsed objects except for hash keys.
Load the 1st documetns only if the given YAML contains multiple documents.
Parsed objects will be wrapped by YPS::Value class to add the accessor returning the position information. You can use the value_class to specify your own wrapper class.
Classes which are allowed to be loaded by default are same as the Psych.safe_load method.
Arguments:
yaml-
String or IO object containing the YAML string to be parsed.
permitted_classes-
Array containing additional classes allowed to be loaded.
permitted_symbols-
Array containing Symbols allowed to be loaded. By default, any symbol can be loaded.
unwrapped_classes-
Array containing classes whose objects are not wrapped with the wrapper class. By default, all objects are wrapped.
aliases-
Aliases can be used if set to true. By default, aliases are not allowed.
filename-
File name string which will be added to the position information of each parsed object.
fallback-
An object which will be returned when an empty YAML string is given.
symbolize_names-
All hash keys will be symbolized if set to true.
freeze-
All parsed objects will be frozen if set to true.
strict_integer-
Integer literals are not allowed to include commas ‘,’ if set to true. Such literals will be parsed as String objects. For Ruby 3.1, this option is ignored.
value_class-
Specify a class wrapping parsed objects. By default,
YPS::Valueis used.
See also Psych.safe_load.
Source
# File lib/yps.rb 91 def safe_load_file(filename, **kwargs) 92 open_file(filename) { |f| safe_load(f, filename:, **kwargs) } 93 end
Similar to YPS.safe_load, but the YAML string is read from the file specified by the filename argument.
See also YPS.safe_load
Source
# File lib/yps.rb 110 def safe_load_stream( # rubocop:disable Metrics/ParameterLists 111 yaml, 112 permitted_classes: [], permitted_symbols: [], unwrapped_classes: [], 113 aliases: false, filename: nil, fallback: DEFAULT_VALUE, symbolize_names: false, 114 freeze: false, strict_integer: false, value_class: Value 115 ) 116 visitor = nil 117 results = [] 118 Parser.parse(yaml, filename) do |node| 119 visitor ||= 120 Visitor.create( 121 permitted_classes, permitted_symbols, unwrapped_classes, 122 aliases, symbolize_names, freeze, strict_integer, value_class 123 ) 124 results << visitor.accept(node) 125 end 126 return fallback if results.empty? && !fallback.equal?(DEFAULT_VALUE) 127 128 results 129 end
Similar to YPS.safe_load, but load all documents given in yaml and return them as a list.
See also YPS.safe_load
Source
# File lib/yps.rb 144 def safe_load_stream_file(filename, **kwargs) 145 open_file(filename) { |f| safe_load_stream(f, filename:, **kwargs) } 146 end
Similar to YPS.safe_load_stream, but the YAML string is read from the file specified by the filename argument.
See also YPS.safe_load_stream