Module: SchemaGraphy::Templating

Extended by:
Sourcerer::Templating
Defined in:
lib/schemagraphy/templating.rb

Overview

A module for handling templated fields within a data structure based on a schema or definition. It provides methods for pre-compiling and rendering fields using various template engines.

Constant Summary collapse

TemplatedField =
An alias for the `Sourcerer::Templating::TemplatedField` class.
Sourcerer::Templating::TemplatedField

Class Method Summary collapse

Class Method Details

.compile_templated_fields!(data:, schema:, fields:, scope: {}) ⇒ Object

Compiles templated fields in the data.

Parameters:

  • data (Hash)
    The data containing the fields to compile.
  • schema (Hash)
    The schema definition.
  • fields (Array<Hash>)
    An array of fields to compile, each with a `:key` and `:path`.
  • scope (Hash) (defaults to: {})
    The scope to use for compilation.


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/schemagraphy/templating.rb', line 56

def self.compile_templated_fields! data:, schema:, fields:, scope: {}
  fields.each do |entry|
    key  = entry[:key]
    path = entry[:path]
    val  = data[key]

    next unless val.is_a?(String) || (val.is_a?(Hash) && val['__tag__'] && val['value'])

    raw     = val.is_a?(Hash) ? val['value'] : val
    tagged  = val.is_a?(Hash)
    config  = SchemaGraphy::SchemaUtils.templating_config_for(schema, path)
    engine  = tagged ? val['__tag__'] : (config['default'] || 'liquid')

    compiled = Sourcerer::Templating::Engines.compile(raw, engine)

    data[key] = if config['delay']
                  Sourcerer::Templating::TemplatedField.new(raw, compiled, engine, tagged, inferred: !tagged)
                else
                  Sourcerer::Templating::Engines.render(compiled, engine, scope)
                end
  end
end

.precompile_from_schema!(data, schema, base_path = '', scope: {}) ⇒ Object

Recursively pre-compiles templated fields in a data structure based on a schema.

Parameters:

  • data (Hash)
    The data to process.
  • schema (Hash)
    The schema defining which fields are templated.
  • base_path (String) (defaults to: '')
    The base path for the current data level.
  • scope (Hash) (defaults to: {})
    The scope to use for compilation.


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/schemagraphy/templating.rb', line 29

def self.precompile_from_schema! data, schema, base_path = '', scope: {}
  return unless data.is_a?(Hash)

  data.each do |key, value|
    path = [base_path, key].reject(&:empty?).join('.')

    precompile_from_schema!(value, schema, path, scope: scope) if value.is_a?(Hash)

    next unless SchemaGraphy::SchemaUtils.templated_field?(schema, path)

    compile_templated_fields!(
      data: data,
      schema: schema,
      fields: [{ key: key, path: path }],
      scope: scope)
  end
end

.render_all_templated_fields!(data, context = {}) ⇒ Object

Recursively renders all pre-compiled templated fields in a data structure.

Parameters:

  • data (Hash, Array)
    The data structure to process.
  • context (Hash) (defaults to: {})
    The context to use for rendering.


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/schemagraphy/templating.rb', line 83

def self.render_all_templated_fields! data, context = {}
  return unless data.is_a?(Hash)

  data.each do |key, value|
    case value
    when TemplatedField
      data[key] = value.render(context)
    when Hash
      render_all_templated_fields!(value, context)
    when Array
      value.each_with_index do |item, idx|
        if item.is_a?(TemplatedField)
          value[idx] = item.render(context)
        elsif item.is_a?(Hash)
          render_all_templated_fields!(item, context)
        end
      end
    end
  end
end

.resolve_field(field, context = {}) ⇒ Object

Renders a field if it is a template.

Parameters:

  • field (Object)
    The field to render.
  • context (Hash) (defaults to: {})
    The context to use for rendering.

Returns:

  • (Object)
    The rendered field, or the original field if it's not a template.


19
20
21
# File 'lib/schemagraphy/templating.rb', line 19

def self.resolve_field field, context = {}
  render_field_if_template(field, context)
end