Module: YPS

Defined in:
lib/yps.rb,
lib/yps/value.rb,
lib/yps/parser.rb,
lib/yps/version.rb,
lib/yps/visitor.rb,
lib/yps/node_extension.rb

Overview

:nodoc: all

Defined Under Namespace

Modules: NodeExtension, Visitor Classes: Handler, Parser, Position, Value

Constant Summary collapse

VERSION =

The version of YPS

'1.0.0'

Class Method Summary collapse

Class Method Details

.load(yaml, permitted_classes: [Symbol], **kwargs) ⇒ Object

Similar to YPS.safe_load, but Symbol is allowed to be loaded by default.

See also Psych.load.



83
84
85
# File 'lib/yps.rb', line 83

def load(yaml, permitted_classes: [Symbol], **kwargs)
  safe_load(yaml, permitted_classes:, **kwargs)
end

.load_file(filename, **kwargs) ⇒ Object

Similar to YPS.load, but the YAML string is read from the file specified by the filename argument.

See also YPS.load



99
100
101
# File 'lib/yps.rb', line 99

def load_file(filename, **kwargs)
  open_file(filename) { |f| load(f, filename:, **kwargs) }
end

.load_stream(yaml, permitted_classes: [Symbol], **kwargs) ⇒ Object

Similar to YPS.safe_load_sream, but Symbol is allowed to be loaded by default.

See also YPS.safe_load_stream



135
136
137
# File 'lib/yps.rb', line 135

def load_stream(yaml, permitted_classes: [Symbol], **kwargs)
  safe_load_stream(yaml, permitted_classes:, **kwargs)
end

.load_stream_file(filename, **kwargs) ⇒ Object

Similar to YPS.load_stream, but the YAML string is read from the file specified by the filename argument.

See also YPS.load_stream



153
154
155
# File 'lib/yps.rb', line 153

def load_stream_file(filename, **kwargs)
  open_file(filename) { |f| load_stream(f, filename:, **kwargs) }
end

.safe_load(yaml, permitted_classes: [], permitted_symbols: [], unwrapped_classes: [], aliases: false, filename: nil, fallback: nil, symbolize_names: false, freeze: false, strict_integer: false, value_class: Value) ⇒ Object

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::Value is used.

See also Psych.safe_load.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/yps.rb', line 61

def safe_load( # rubocop:disable Metrics/ParameterLists
  yaml,
  permitted_classes: [], permitted_symbols: [], unwrapped_classes: [],
  aliases: false, filename: nil, fallback: nil, symbolize_names: false,
  freeze: false, strict_integer: false, value_class: Value
)
  Parser.parse(yaml, filename) do |node|
    visitor =
      Visitor.create(
        permitted_classes, permitted_symbols, unwrapped_classes,
        aliases, symbolize_names, freeze, strict_integer, value_class
      )
    return visitor.accept(node)
  end

  fallback
end

.safe_load_file(filename, **kwargs) ⇒ Object

Similar to YPS.safe_load, but the YAML string is read from the file specified by the filename argument.

See also YPS.safe_load



91
92
93
# File 'lib/yps.rb', line 91

def safe_load_file(filename, **kwargs)
  open_file(filename) { |f| safe_load(f, filename:, **kwargs) }
end

.safe_load_stream(yaml, permitted_classes: [], permitted_symbols: [], unwrapped_classes: [], aliases: false, filename: nil, fallback: DEFAULT_VALUE, symbolize_names: false, freeze: false, strict_integer: false, value_class: Value) ⇒ Object

Similar to YPS.safe_load, but load all documents given in yaml and return them as a list.

See also YPS.safe_load



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/yps.rb', line 110

def safe_load_stream( # rubocop:disable Metrics/ParameterLists
  yaml,
  permitted_classes: [], permitted_symbols: [], unwrapped_classes: [],
  aliases: false, filename: nil, fallback: DEFAULT_VALUE, symbolize_names: false,
  freeze: false, strict_integer: false, value_class: Value
)
  visitor = nil
  results = []
  Parser.parse(yaml, filename) do |node|
    visitor ||=
      Visitor.create(
        permitted_classes, permitted_symbols, unwrapped_classes,
        aliases, symbolize_names, freeze, strict_integer, value_class
      )
    results << visitor.accept(node)
  end
  return fallback if results.empty? && !fallback.equal?(DEFAULT_VALUE)

  results
end

.safe_load_stream_file(filename, **kwargs) ⇒ Object

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



144
145
146
# File 'lib/yps.rb', line 144

def safe_load_stream_file(filename, **kwargs)
  open_file(filename) { |f| safe_load_stream(f, filename:, **kwargs) }
end