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 80 def load(yaml, permitted_classes: [Symbol], **kwargs) 81 safe_load(yaml, permitted_classes:, **kwargs) 82 end
Similar to YPS.safe_load
, but Symbol is allowed to be loaded by default.
See also Psych.load.
Source
Source
# File lib/yps.rb 132 def load_stream(yaml, permitted_classes: [Symbol], **kwargs) 133 safe_load_stream(yaml, permitted_classes:, **kwargs) 134 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 150 def load_stream_file(filename, **kwargs) 151 open_file(filename) { |f| load_stream(f, filename:, **kwargs) } 152 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 58 def safe_load( # rubocop:disable Metrics/ParameterLists 59 yaml, 60 permitted_classes: [], permitted_symbols: [], aliases: false, 61 filename: nil, fallback: nil, symbolize_names: false, freeze: false, 62 strict_integer: false, value_class: Value 63 ) 64 Parser.parse(yaml, filename) do |node| 65 visitor = 66 Visitor.create( 67 permitted_classes, permitted_symbols, aliases, 68 symbolize_names, freeze, strict_integer, value_class 69 ) 70 return visitor.accept(node) 71 end 72 73 fallback 74 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.
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::Value
is used.
See also Psych.safe_load.
Source
# File lib/yps.rb 88 def safe_load_file(filename, **kwargs) 89 open_file(filename) { |f| safe_load(f, filename:, **kwargs) } 90 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 107 def safe_load_stream( # rubocop:disable Metrics/ParameterLists 108 yaml, 109 permitted_classes: [], permitted_symbols: [], aliases: false, 110 filename: nil, fallback: DEFAULT_VALUE, symbolize_names: false, 111 freeze: false, strict_integer: false, value_class: Value 112 ) 113 visitor = nil 114 results = [] 115 Parser.parse(yaml, filename) do |node| 116 visitor ||= 117 Visitor.create( 118 permitted_classes, permitted_symbols, aliases, 119 symbolize_names, freeze, strict_integer, value_class 120 ) 121 results << visitor.accept(node) 122 end 123 return fallback if results.empty? && !fallback.equal?(DEFAULT_VALUE) 124 125 results 126 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 141 def safe_load_stream_file(filename, **kwargs) 142 open_file(filename) { |f| safe_load_stream(f, filename:, **kwargs) } 143 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