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

.build_derived(common, raw, key) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/textus/manifest/entry/parser.rb', line 45

def self.build_derived(common, raw, key)
  source = parse_source(raw, key)
  Derived.new(
    source: source,
    template: raw["template"],
    inject_intro: raw["inject_intro"] == true,
    publish_to: raw["publish_to"],
    events: raw["events"] || {},
    **common,
  )
end

.build_intake(common, raw, key) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/textus/manifest/entry/parser.rb', line 57

def self.build_intake(common, raw, key)
  intake = raw["intake"] || {}
  handler = intake["handler"] || raw["intake_handler"] or
    raise UsageError.new("intake entry '#{key}' missing handler")
  config = intake["config"] || raw["intake_config"] || {}
  Intake.new(handler: handler, config: config, events: raw["events"] || {}, **common)
end

.build_leaf(common, raw) ⇒ Object



32
33
34
# File 'lib/textus/manifest/entry/parser.rb', line 32

def self.build_leaf(common, raw)
  Leaf.new(publish_to: raw["publish_to"], **common)
end

.build_nested(common, raw) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/textus/manifest/entry/parser.rb', line 36

def self.build_nested(common, raw)
  Nested.new(
    index_filename: raw["index_filename"],
    publish_each: raw["publish_each"],
    publish_to: raw["publish_to"],
    **common,
  )
end

.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
28
29
30
# 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:` (leaf|nested|derived|intake)")
  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
  }

  case kind
  when :leaf    then build_leaf(common, raw)
  when :nested  then build_nested(common, raw)
  when :derived then build_derived(common, raw, key)
  when :intake  then build_intake(common, raw, key)
  else raise BadManifest.new("entry '#{key}': unknown kind: #{kind.inspect}")
  end
end

.parse_source(raw, key) ⇒ Object

Raises:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/textus/manifest/entry/parser.rb', line 65

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"
    Derived::Projection.new(
      select: compute["select"],
      pluck: compute["pluck"],
      sort_by: compute["sort_by"],
      transform: compute["transform"],
    )
  else
    Derived::External.new(sources: compute["sources"], runner: compute["runner"])
  end
end

.resolve_format(raw, path) ⇒ Object

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/textus/manifest/entry/parser.rb', line 87

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