Class: SchemaGraphy::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/schemagraphy/loader.rb

Overview

The Loader class provides methods for loading YAML files while preserving custom tags and resolving attribute references.

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 for template processing.

Parameters:

  • node (Psych::Nodes::Node)
    The current AST node.
  • data (Object)
    The data corresponding to the current node.


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/schemagraphy/loader.rb', line 41

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_yaml_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.


16
17
18
19
20
# File 'lib/schemagraphy/loader.rb', line 16

def self.load_yaml_with_attributes path, attrs = {}
  raw_data = load_yaml_with_tags(path)
  AttributeResolver.resolve_attributes!(raw_data, attrs)
  raw_data
end

.load_yaml_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.


27
28
29
30
31
32
33
34
# File 'lib/schemagraphy/loader.rb', line 27

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

  data = Psych.load_file(path, aliases: true, permitted_classes: [Date, Time])
  ast  = Psych.parse_file(path)
  attach_tags(ast.root, data)
  data
end