Class: Suma::ManifestTraverser

Inherits:
Object
  • Object
show all
Defined in:
lib/suma/manifest_traverser.rb

Overview

Tree-walking service over a CollectionManifest.

ManifestTraverser owns all imperative operations that walk or mutate a manifest tree: finding schemas-only entries, expanding them into compiled-doc sub-trees, exporting a unified Expressir::SchemaManifest, and removing schemas-only sources after compilation.

Each method takes the manifest passed at construction as its root; recursion instantiates a new traverser per child node so the surface stays instance-based and the data model (CollectionManifest) stays pure.

Schema-config loading and doc-entry construction are delegated to SchemaDiscovery so this class owns traversal, not schema I/O.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manifest) ⇒ ManifestTraverser

Returns a new instance of ManifestTraverser.



22
23
24
# File 'lib/suma/manifest_traverser.rb', line 22

def initialize(manifest)
  @manifest = manifest
end

Instance Attribute Details

#manifestObject (readonly)

Returns the value of attribute manifest.



20
21
22
# File 'lib/suma/manifest_traverser.rb', line 20

def manifest
  @manifest
end

Instance Method Details

#expand_schemas_only(schema_output_path) ⇒ Object

Expand schemas-only entries into compiled-doc sub-trees. If the manifest has no file, walks children via process_entry. Otherwise loads the schema_config (SchemaDiscovery), and if this entry is schemas-only, hides it from the output index and appends a new sub-collection that hosts the compiled docs.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/suma/manifest_traverser.rb', line 54

def expand_schemas_only(schema_output_path)
  return process_entry(schema_output_path) unless manifest.file

  SchemaDiscovery.new(manifest).load_config

  return process_entry(schema_output_path) unless manifest.schemas_only

  manifest.index = false

  added = SchemaDiscovery.new(manifest).build_added_manifest(schema_output_path)
  [manifest, added]
end

#export_schema_config(path) ⇒ Object

Recursively concatenate every nested schema_config into a single Expressir::SchemaManifest. The manifest's own schema_config (if set) seeds the result; otherwise an empty SchemaManifest is returned.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/suma/manifest_traverser.rb', line 37

def export_schema_config(path)
  export_config = manifest.schema_config || Expressir::SchemaManifest.new
  return export_config unless manifest.entry

  manifest.entry.each do |child|
    child_config = self.class.new(child).export_schema_config(path)
    export_config.concat(child_config) if child_config
  end

  export_config
end

#find_schemas_onlyObject

Returns every entry (anywhere in the tree) whose schemas_only flag is truthy, plus manifest itself if it is schemas-only.



28
29
30
31
32
# File 'lib/suma/manifest_traverser.rb', line 28

def find_schemas_only
  results = (manifest.entry || []).select(&:schemas_only)
  results << manifest if manifest.schemas_only
  results
end

#remove_schemas_only_sourcesObject

Drop every direct child whose schemas_only is truthy. Called after compilation so the schemas-only manifest file no longer shows up in the rendered collection.



70
71
72
73
74
75
# File 'lib/suma/manifest_traverser.rb', line 70

def remove_schemas_only_sources
  return unless manifest.entry

  kept = manifest.entry.reject(&:schemas_only)
  manifest.entry = kept
end