Class: Synthra::Types::Template

Inherits:
Base
  • Object
show all
Defined in:
lib/synthra/types/template.rb

Overview

Template type generator.

Substitutes +$field+ (and legacy +field+) placeholders with the already-generated value of that field in the same record. Field values come from the generation +context+, which holds every field generated before this one (fields are generated in definition order).

first: name greeting: template("Hello $first") # => "Hello Ada Lovelace"

A referenced field that is not (yet) available is left as the literal placeholder text rather than blanking the whole string.

Constant Summary collapse

PLACEHOLDER =

Matches $field or field; the field name is captured.

/\$\{([^}]+)\}|\{([^}]+)\}/

Instance Method Summary collapse

Instance Method Details

#context_values(context) ⇒ Hash{String=>Object} (private)

Flatten the context into a String-keyed lookup of field => value. Accepts a Context, a Hash, or anything responding to #each.

Parameters:

  • context (Object)

Returns:

  • (Hash{String=>Object})


52
53
54
55
56
57
58
59
60
# File 'lib/synthra/types/template.rb', line 52

def context_values(context)
  return {} unless context.respond_to?(:each)

  values = {}
  context.each do |key, value|
    values[key.to_s] = value
  end
  values
end

#generate_edge(rng, context, args) ⇒ Object



37
38
39
# File 'lib/synthra/types/template.rb', line 37

def generate_edge(rng, context, args)
  rng.sample(["", "${missing}", "template"])
end

#generate_invalid(rng, context, args) ⇒ Object



41
42
43
# File 'lib/synthra/types/template.rb', line 41

def generate_invalid(rng, context, args)
  rng.sample([nil, 123, [], {}])
end

#generate_random(rng, context, args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/synthra/types/template.rb', line 21

def generate_random(rng, context, args)
  pattern = args[:pattern] || args[:template] || ""
  return "" if pattern.to_s.empty?

  values = context_values(context)

  pattern.to_s.gsub(PLACEHOLDER) do
    key = (Regexp.last_match(1) || Regexp.last_match(2)).strip
    if values.key?(key)
      values[key].to_s
    else
      Regexp.last_match(0) # leave the literal placeholder untouched
    end
  end
end

#get_context_value(context, key) ⇒ Object (private)

Look up a single value from a context by key.

Retained as a public-by-#send helper for callers/tests that resolve one key at a time (e.g. lazy interpolation). The primary generate_random path uses context_values for bulk substitution; this mirrors that resolution order (hash-style [] first, then a same-named method) for a single key.

Parameters:

  • context (Object)

    context object (Hash, Context, or any object)

  • key (String)

    key to retrieve

Returns:

  • (Object)

    value (stringified) or "" if not found



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/synthra/types/template.rb', line 72

def get_context_value(context, key)
  return "" if context.nil?

  if context.respond_to?(:[])
    value = context[key] || context[key.to_sym]
    return value.to_s if value
  end

  if context.respond_to?(key)
    begin
      return context.public_send(key).to_s
    rescue NoMethodError
      return "" # empty result
    end
  end

  ""
end