Module: Sourcerer::Yaml

Defined in:
lib/sourcerer/yaml.rb

Overview

Lightweight YAML parsing helpers with optional AsciiDoc attribute resolution.

Defined Under Namespace

Modules: AttributeResolver, TagUtils

Class Method Summary collapse

Class Method Details

.attach_tags(node, data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Recursively attach YAML tags to the loaded data structure.

Parameters:

  • node (Psych::Nodes::Node)

    The current AST node.

  • data (Object)

    The data corresponding to the current node.



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

def self.attach_tags node, data
  return unless node.is_a?(Psych::Nodes::Mapping)

  node.children.each_slice(2) do |key_node, val_node|
    key = key_node.value

    if val_node.respond_to?(:tag) && val_node.tag && data[key].is_a?(String)
      normalized_tag = val_node.tag.sub(/^!+/, '').sub(/^.*:/, '')
      data[key] = {
        'value' => data[key],
        '__tag__' => normalized_tag
      }
    elsif data[key].is_a?(Hash)
      attach_tags(val_node, data[key])
    end
  end
end

.load_with_attributes(path, attrs = {}) ⇒ Hash

Load a YAML file and resolve AsciiDoc attribute references like ‘attribute_name`.

Parameters:

  • path (String)

    The path to the YAML file.

  • attrs (Hash) (defaults to: {})

    The AsciiDoc attributes to use for resolution.

Returns:

  • (Hash)

    The loaded YAML data with attributes resolved.



40
41
42
# File 'lib/sourcerer/yaml.rb', line 40

def self.load_with_attributes path, attrs = {}
  parse_with_attributes(File.read(path), attrs)
end

.load_with_tags(path) ⇒ Hash

Load a YAML file, preserving any custom tags (e.g., ‘!foo`). Custom tags are attached to the data structure.

Parameters:

  • path (String)

    The path to the YAML file.

Returns:

  • (Hash)

    The loaded YAML data with custom tags attached.



49
50
51
52
53
# File 'lib/sourcerer/yaml.rb', line 49

def self.load_with_tags path
  return {} if File.empty?(path)

  parse_with_tags(File.read(path))
end

.parse_with_attributes(yaml, attrs = {}) ⇒ Hash

Parse a YAML string and resolve AsciiDoc attribute references like ‘attribute_name`.

Parameters:

  • yaml (String)

    The YAML string to parse.

  • attrs (Hash) (defaults to: {})

    The AsciiDoc attributes to use for resolution.

Returns:

  • (Hash)

    The loaded YAML data with attributes resolved.



15
16
17
18
19
# File 'lib/sourcerer/yaml.rb', line 15

def self.parse_with_attributes yaml, attrs = {}
  raw_data = parse_with_tags(yaml)
  AttributeResolver.resolve_attributes!(raw_data, attrs)
  raw_data
end

.parse_with_tags(yaml) ⇒ Hash

Parse a YAML string, preserving any custom tags (e.g., ‘!foo`). Custom tags are attached to the data structure.

Parameters:

  • yaml (String)

    The YAML string to parse.

Returns:

  • (Hash)

    The loaded YAML data with custom tags attached.



26
27
28
29
30
31
32
33
# File 'lib/sourcerer/yaml.rb', line 26

def self.parse_with_tags yaml
  return {} if yaml.nil? || yaml.strip.empty?

  data = Psych.load(yaml, aliases: true, permitted_classes: [Date, Time])
  ast  = Psych.parse(yaml)
  attach_tags(ast.root, data)
  data
end