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
-
.resolve_attribute_reference(value, attrs) ⇒ String
Replace ‘attribute_name` patterns with corresponding values from attrs.
-
.resolve_attributes!(schema, attrs) ⇒ Hash
Recursively walk a schema Hash and resolve ‘attribute_name` references in `dflt` values.
Class Method Details
.resolve_attribute_reference(value, attrs) ⇒ String
Replace ‘attribute_name` patterns with corresponding values from attrs.
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.
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 |