Module: Textus::Manifest::Entry::Parser

Defined in:
lib/textus/manifest/entry/parser.rb

Constant Summary collapse

COMPUTE_KINDS =
%w[projection external].freeze

Class Method Summary collapse

Class Method Details

.call(manifest, raw) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/textus/manifest/entry/parser.rb', line 7

def self.call(manifest, raw)
  key = raw["key"] or raise UsageError.new("manifest entry missing key")
  path = raw["path"] or raise UsageError.new("manifest entry '#{key}' missing path")
  zone = raw["zone"] or raise UsageError.new("manifest entry '#{key}' missing zone")

  raw_kind = raw["kind"] or raise BadManifest.new("entry '#{key}' missing required `kind:` (#{Entry::REGISTRY.keys.join("|")})")
  kind = raw_kind.to_sym
  format = resolve_format(raw, path)

  common = {
    manifest: manifest, raw: raw,
    key: key, path: path, zone: zone,
    schema: raw["schema"], owner: raw["owner"],
    format: format,
    publish_to: raw["publish_to"]
  }

  klass = Entry::REGISTRY[kind] or
    raise BadManifest.new("entry '#{key}': unknown kind: #{kind.inspect} (known: #{Entry::REGISTRY.keys.join(", ")})")
  klass.from_raw(common, raw)
end

.parse_source(raw, key) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/textus/manifest/entry/parser.rb', line 29

def self.parse_source(raw, key)
  compute = raw["compute"]
  raise BadManifest.new("derived entry '#{key}' requires compute: { kind: projection|external } or template:") if compute.nil?

  unless COMPUTE_KINDS.include?(compute["kind"])
    raise BadManifest.new(
      "entry '#{key}': compute.kind must be one of #{COMPUTE_KINDS.join(", ")} (got #{compute["kind"].inspect})",
    )
  end

  if compute["kind"] == "projection"
    Entry::Derived::Projection.new(
      select: compute["select"],
      pluck: compute["pluck"],
      sort_by: compute["sort_by"],
      transform: compute["transform"],
    )
  else
    Entry::Derived::External.new(sources: compute["sources"], runner: compute["runner"])
  end
end

.resolve_format(raw, path) ⇒ Object

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/textus/manifest/entry/parser.rb', line 51

def self.resolve_format(raw, path)
  declared = raw["format"]
  ext = File.extname(path)
  inferred = Textus::Entry.infer_from_extension(ext)

  if declared.nil?
    return inferred if inferred

    return "markdown"
  end

  raise UsageError.new("entry '#{raw["key"]}': unknown format #{declared.inspect}") unless Textus::Entry.formats.include?(declared)

  if ext != "" && inferred && inferred != declared
    raise UsageError.new(
      "entry '#{raw["key"]}': path extension #{ext.inspect} does not match declared format #{declared.inspect}",
    )
  end

  declared
end