Module: ReleaseHx::SgymlHelpers

Defined in:
lib/releasehx/sgyml/helpers.rb

Class Method Summary collapse

Class Method Details

.deep_stringify_safe(obj) ⇒ Object

Recursively converts all keys in a Hash or Array to strings, safely handling non-stringifiable objects


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/releasehx/sgyml/helpers.rb', line 27

def self.deep_stringify_safe obj
  case obj
  when Hash
    obj.each_with_object({}) do |(k, v), h|
      h[k.to_s] = deep_stringify_safe(v)
    end
  when Array
    obj.map { |v| deep_stringify_safe(v) }
  else
    begin
      obj.to_yaml
      obj
    rescue TypeError
      obj.to_s
    end
  end
end

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

Precompiles a schema into a set of templates, using the provided data and schema.


8
9
10
# File 'lib/releasehx/sgyml/helpers.rb', line 8

def self.precompile_from_schema! data, schema, base_path = '', scope: {}
  SchemaGraphy::Templating.precompile_from_schema!(data, schema, base_path, scope: scope)
end

.render_stage_fields!(data, stage) ⇒ Object

Renders all templated fields in a given Hash if they've previously been parsed


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/releasehx/sgyml/helpers.rb', line 13

def self.render_stage_fields! data, stage
  data.each do |key, value|
    next unless value.is_a?(Sourcerer::Templating::TemplatedField)

    tmpl_context = value.context
    next unless tmpl_context.respond_to?(:stage)
    next unless tmpl_context.stage.to_sym == stage.to_sym

    data[key] = value.render
  end
end