Module: LcpRuby::Generators::DslToYaml

Defined in:
lib/generators/lcp_ruby/dsl_to_yaml.rb

Overview

Evaluates a DSL template file and returns YAML string output. Used by generators to produce YAML from a single DSL source.

Defined Under Namespace

Classes: EvalContext

Class Method Summary collapse

Class Method Details

.convert(dsl_path) ⇒ Object



17
18
19
# File 'lib/generators/lcp_ruby/dsl_to_yaml.rb', line 17

def convert(dsl_path)
  convert_string(File.read(dsl_path), virtual_path: dsl_path)
end

.convert_string(content, virtual_path: "(string)") ⇒ Object

Convert pre-rendered DSL content (e.g. an ERB-rendered template) to YAML. ‘virtual_path` is used only for error messages / line numbers.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/generators/lcp_ruby/dsl_to_yaml.rb', line 23

def convert_string(content, virtual_path: "(string)")
  ctx = evaluate_dsl(content, virtual_path)
  # Strip i18n_check Phase 3a internal source-loc keys before
  # serializing — they're metadata for the lint, not part of the
  # public YAML schema. Stripping at the `to_hash` level (instead
  # of after `to_yaml`) avoids Psych alias-output entirely when
  # the same source_loc hash repeats across labeled elements.
  wrapper = WRAPPER_KEYS.fetch(ctx.builder.class) do
    raise "Unsupported builder #{ctx.builder.class}"
  end
  clean_hash = HashUtils.strip_source_locs(ctx.builder.to_hash)
  # ViewGroupBuilder.to_hash already wraps under "view_group" —
  # detect and don't double-wrap.
  yaml_hash = clean_hash.key?(wrapper) ? clean_hash : { wrapper => clean_hash }
  if ctx.inherits
    yaml_hash[wrapper]["inherits"] = ctx.inherits
  end
  yaml_hash.to_yaml
end

.evaluate_dsl(content, path) ⇒ Object



43
44
45
46
47
48
# File 'lib/generators/lcp_ruby/dsl_to_yaml.rb', line 43

def evaluate_dsl(content, path)
  ctx = EvalContext.new
  ctx.instance_eval(content, path)
  raise "No define_model/define_presenter/define_view_group found in #{path}" unless ctx.builder
  ctx
end