Module: Sourcerer::Yaml::AttributeResolver

Defined in:
lib/sourcerer/yaml.rb

Overview

Resolves AsciiDoc attribute references like ‘attribute_name` in YAML values.

Class Method Summary collapse

Class Method Details

.resolve_attribute_reference(value, attrs) ⇒ String

Replace ‘attribute_name` patterns with corresponding values from attrs.

Parameters:

  • value (String)

    The string to process.

  • attrs (Hash)

    The attributes to use for resolution.

Returns:

  • (String)

    The processed string with attribute references replaced.



108
109
110
111
112
113
114
115
# File 'lib/sourcerer/yaml.rb', line 108

def self.resolve_attribute_reference value, attrs
  return value unless value.match?(/\{[^}]+\}/)

  value.gsub(/\{([^}]+)\}/) do |match|
    attr_name = ::Regexp.last_match(1)
    attrs[attr_name] || match
  end
end

.resolve_attributes!(schema, attrs) ⇒ Hash

Recursively walk a schema Hash and resolve ‘attribute_name` references in `dflt` values.

Parameters:

  • schema (Hash)

    The schema or definition hash to process.

  • attrs (Hash)

    The key-value pairs from AsciiDoc attributes to use for resolution.

Returns:

  • (Hash)

    The schema with resolved attributes.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sourcerer/yaml.rb', line 86

def self.resolve_attributes! schema, attrs
  case schema
  when Hash
    schema.transform_values! do |value|
      if value.is_a?(Hash)
        if value.key?('dflt') && value['dflt'].is_a?(String)
          value['dflt'] = resolve_attribute_reference(value['dflt'], attrs)
        end
        resolve_attributes!(value, attrs)
      else
        value
      end
    end
  end
  schema
end