Module: RapidYAML

Defined in:
lib/rapidyaml/version.rb,
lib/rapidyaml.rb

Overview

Version constant definition for rapidyaml gem

This file defines the VERSION constant for the RapidYAML module, which is used for gem versioning, dependency resolution, and runtime version checks. The version follows semantic versioning (SemVer) format and is referenced by the gemspec, bundler, and other parts of the gem infrastructure.

The version is kept in a separate file to allow easy programmatic access and updates during the release process without modifying the main module files.

Constant Summary collapse

ParseError =

Error and SyntaxError are defined in the C extension (Init_rapidyaml). ParseError is kept as an alias for backward compatibility.

SyntaxError
DATE_RE =

Regex patterns for scalar coercion during load. Only applied when the corresponding class is in permitted_classes.

/\A(\d{4})-(\d{2})-(\d{2})\z/
DATETIME_RE =
/\A\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?: ?(?:Z|[+-]\d{2}:?\d{2}))?\z/
VERSION =
'0.1.3'

Class Method Summary collapse

Class Method Details

.dump(obj) ⇒ String

Serializes a Ruby object to a YAML string.

Parameters:

  • obj (Object)

    Ruby object to serialize

Returns:

  • (String)

    YAML representation

Raises:

  • (RapidYAML::Error)

    if the object cannot be serialized



74
75
76
# File 'lib/rapidyaml.rb', line 74

def dump(obj)
  Ext.emit(obj)
end

.dump_file(obj, path) ⇒ void

This method returns an undefined value.

Serializes a Ruby object to a YAML file.

Parameters:

  • obj (Object)

    Ruby object to serialize

  • path (String)

    destination file path

Raises:

  • (RapidYAML::Error)

    if the object cannot be serialized



174
175
176
# File 'lib/rapidyaml.rb', line 174

def dump_file(obj, path)
  File.write(path, dump(obj))
end

.dump_stream(*objects) ⇒ String

Serializes multiple Ruby objects into a multi-document YAML stream.

Parameters:

  • objects (Array<Object>)

    Ruby objects to serialize

Returns:

  • (String)

    YAML stream with one document per object

Raises:

  • (RapidYAML::Error)

    if any object cannot be serialized



83
84
85
86
87
88
# File 'lib/rapidyaml.rb', line 83

def dump_stream(*objects)
  objects.map do |obj|
    s = dump(obj)
    obj.is_a?(Hash) || obj.is_a?(Array) ? "---\n#{s}" : "--- #{s}"
  end.join
end

.libyaml_versionString

Returns the version string of the bundled rapidyaml C++ library. Psych exposes libyaml_version; this is the equivalent for rapidyaml.

Returns:

  • (String)

    rapidyaml version, e.g. "0.13.0"



103
104
105
# File 'lib/rapidyaml.rb', line 103

def libyaml_version
  Ext.ryml_version
end

.load(yaml, symbolize_names: false, permitted_classes: []) ⇒ Object

Parses a YAML string and returns the corresponding Ruby object.

Parameters:

  • yaml (String)

    YAML-formatted string

  • symbolize_names (Boolean) (defaults to: false)

    if true, hash keys are returned as symbols

  • permitted_classes (Array<Class>) (defaults to: [])

    classes allowed for type coercion (supports Symbol, Date, Time)

Returns:

  • (Object)

    parsed Ruby object

Raises:

  • (RapidYAML::SyntaxError)

    if the YAML is invalid



51
52
53
54
55
# File 'lib/rapidyaml.rb', line 51

def load(yaml, symbolize_names: false, permitted_classes: [])
  result = Ext.parse(yaml)
  result = coerce_scalars(result, permitted_classes) unless permitted_classes.empty?
  symbolize_names ? deep_symbolize_keys(result) : result
end

.load_file(path, symbolize_names: false, permitted_classes: []) ⇒ Object

Parses YAML from a file and returns the corresponding Ruby object.

Parameters:

  • path (String)

    path to the YAML file

  • symbolize_names (Boolean) (defaults to: false)

    if true, hash keys are returned as symbols

  • permitted_classes (Array<Class>) (defaults to: [])

    classes allowed for type coercion

Returns:

  • (Object)

    parsed Ruby object

Raises:

  • (RapidYAML::SyntaxError)

    if the YAML is invalid

  • (Errno::ENOENT)

    if the file does not exist



163
164
165
166
# File 'lib/rapidyaml.rb', line 163

def load_file(path, symbolize_names: false, permitted_classes: [])
  load(File.read(path), symbolize_names: symbolize_names,
                        permitted_classes: permitted_classes)
end

.load_stream(yaml, symbolize_names: false, permitted_classes: []) {|Object| ... } ⇒ Array?

Parses all YAML documents in a string and yields each as a Ruby object. If no block given, returns an array of all documents.

Parameters:

  • yaml (String)

    YAML-formatted string (may contain multiple documents)

  • symbolize_names (Boolean) (defaults to: false)

    if true, hash keys are returned as symbols

  • permitted_classes (Array<Class>) (defaults to: [])

    classes allowed for type coercion

Yields:

  • (Object)

    each parsed document

Returns:

  • (Array, nil)

    array of documents if no block given



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rapidyaml.rb', line 115

def load_stream(yaml, symbolize_names: false, permitted_classes: [], &block)
  docs = Ext.parse_stream(yaml)
  docs = docs.map { |d| coerce_scalars(d, permitted_classes) } unless permitted_classes.empty?
  docs = docs.map { |d| deep_symbolize_keys(d) } if symbolize_names
  if block
    docs.each(&block)
    nil
  else
    docs
  end
end

.safe_dump(obj) ⇒ String

Equivalent to dump; rapidyaml does not serialize arbitrary Ruby objects, so there is no unsafe surface to restrict.

Parameters:

  • obj (Object)

    Ruby object to serialize

Returns:

  • (String)

    YAML representation



95
96
97
# File 'lib/rapidyaml.rb', line 95

def safe_dump(obj)
  dump(obj)
end

.safe_load(yaml, symbolize_names: false, permitted_classes: []) ⇒ Object

Alias matching Psych.safe_load; rapidyaml does not execute arbitrary code, so this is equivalent to load for all practical purposes.

Parameters:

  • yaml (String)

    YAML-formatted string

  • symbolize_names (Boolean) (defaults to: false)

    if true, hash keys are returned as symbols

  • permitted_classes (Array<Class>) (defaults to: [])

    classes allowed for type coercion

Returns:

  • (Object)

    parsed Ruby object

Raises:

  • (RapidYAML::SyntaxError)

    if the YAML is invalid



65
66
67
# File 'lib/rapidyaml.rb', line 65

def safe_load(yaml, symbolize_names: false, permitted_classes: [])
  load(yaml, symbolize_names: symbolize_names, permitted_classes: permitted_classes)
end

.safe_load_stream(yaml, symbolize_names: false, permitted_classes: [], &block) ⇒ Object

See Also:



128
129
130
131
# File 'lib/rapidyaml.rb', line 128

def safe_load_stream(yaml, symbolize_names: false, permitted_classes: [], &block)
  load_stream(yaml, symbolize_names: symbolize_names,
                    permitted_classes: permitted_classes, &block)
end

.unsafe_load(yaml, symbolize_names: false, permitted_classes: []) ⇒ Object

Alias matching Psych.unsafe_load; rapidyaml never executes arbitrary Ruby object tags, so this is equivalent to load.

Parameters:

  • yaml (String)

    YAML-formatted string

  • symbolize_names (Boolean) (defaults to: false)

    if true, hash keys are returned as symbols

  • permitted_classes (Array<Class>) (defaults to: [])

    classes allowed for type coercion

Returns:

  • (Object)

    parsed Ruby object



140
141
142
# File 'lib/rapidyaml.rb', line 140

def unsafe_load(yaml, symbolize_names: false, permitted_classes: [])
  load(yaml, symbolize_names: symbolize_names, permitted_classes: permitted_classes)
end

.unsafe_load_file(path, symbolize_names: false, permitted_classes: []) ⇒ Object

Alias matching Psych.unsafe_load_file; rapidyaml never executes arbitrary Ruby object tags, so this is equivalent to load_file.

Parameters:

  • path (String)

    path to the YAML file

  • symbolize_names (Boolean) (defaults to: false)

    if true, hash keys are returned as symbols

  • permitted_classes (Array<Class>) (defaults to: [])

    classes allowed for type coercion

Returns:

  • (Object)

    parsed Ruby object



151
152
153
# File 'lib/rapidyaml.rb', line 151

def unsafe_load_file(path, symbolize_names: false, permitted_classes: [])
  load_file(path, symbolize_names: symbolize_names, permitted_classes: permitted_classes)
end