Module: SchemaGraphy::AttributeResolver

Defined in:
lib/schemagraphy/attribute_resolver.rb

Overview

The AttributeResolver module provides methods for resolving AsciiDoc attribute references within a schema hash. It is used to substitute placeholders like `{attribute_name}` with actual values.

Class Method Summary collapse

Class Method Details

.resolve_attribute_reference(value, attrs) ⇒ String

Replace `{attribute_name}` patterns with corresponding values from the attrs hash.

Parameters:

  • value (String)
    The string to process.
  • attrs (Hash)
    The attributes to use for resolution.

Returns:

  • (String)
    The processed string with attribute references replaced.


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/schemagraphy/attribute_resolver.rb', line 36

def self.resolve_attribute_reference value, attrs
  # Handle \{attribute_name} references
  if value.match?(/\{[^}]+\}/)
    value.gsub(/\{([^}]+)\}/) do |match|
      attr_name = ::Regexp.last_match(1)
      attrs[attr_name] || match # Keep original if no matching attribute
    end
  else
    value
  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.


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/schemagraphy/attribute_resolver.rb', line 14

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