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
-
.attach_tags(node, data) ⇒ Object
private
Recursively attach YAML tags to the loaded data structure.
-
.load_with_attributes(path, attrs = {}) ⇒ Hash
Load a YAML file and resolve AsciiDoc attribute references like ‘attribute_name`.
-
.load_with_tags(path) ⇒ Hash
Load a YAML file, preserving any custom tags (e.g., ‘!foo`).
-
.parse_with_attributes(yaml, attrs = {}) ⇒ Hash
Parse a YAML string and resolve AsciiDoc attribute references like ‘attribute_name`.
-
.parse_with_tags(yaml) ⇒ Hash
Parse a YAML string, preserving any custom tags (e.g., ‘!foo`).
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.
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. 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) (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`.
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.
49 50 51 52 53 |
# File 'lib/sourcerer/yaml.rb', line 49 def self. path return {} if File.empty?(path) (File.read(path)) end |
.parse_with_attributes(yaml, attrs = {}) ⇒ Hash
Parse a YAML string and resolve AsciiDoc attribute references like ‘attribute_name`.
15 16 17 18 19 |
# File 'lib/sourcerer/yaml.rb', line 15 def self.parse_with_attributes yaml, attrs = {} raw_data = (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.
26 27 28 29 30 31 32 33 |
# File 'lib/sourcerer/yaml.rb', line 26 def self. yaml return {} if yaml.nil? || yaml.strip.empty? data = Psych.load(yaml, aliases: true, permitted_classes: [Date, Time]) ast = Psych.parse(yaml) (ast.root, data) data end |