Class: OKF::Bundle::Folder

Inherits:
Object
  • Object
show all
Defined in:
lib/okf/bundle/folder.rb

Overview

A bundle on disk — the directory-level handle. Reads a directory into a pure OKF::Bundle once, exposes the analyzers over it, and can materialize an in-memory bundle back to disk. Part of the shell.

folder = OKF::Bundle::Folder.load("docs")
folder.bundle             # => OKF::Bundle (pure)
folder.validate; folder.lint; folder.graph
folder.concept("tables/orders")   # => OKF::Concept::File (or nil)

# build in memory (the Rails / snapshot-publisher path) and write it out:
OKF::Bundle::Folder.new(bundle: pure_bundle, root: "out/dir").save

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bundle:, root:) ⇒ Folder

Returns a new instance of Folder.



24
25
26
27
# File 'lib/okf/bundle/folder.rb', line 24

def initialize(bundle:, root:)
  @bundle = bundle
  @root = File.expand_path(root.to_s)
end

Instance Attribute Details

#bundleObject (readonly)

Returns the value of attribute bundle.



17
18
19
# File 'lib/okf/bundle/folder.rb', line 17

def bundle
  @bundle
end

#rootObject (readonly)

Returns the value of attribute root.



17
18
19
# File 'lib/okf/bundle/folder.rb', line 17

def root
  @root
end

Class Method Details

.load(dir) ⇒ Object



19
20
21
22
# File 'lib/okf/bundle/folder.rb', line 19

def self.load(dir)
  root = File.expand_path(dir.to_s)
  new(bundle: Reader.read(root), root: root)
end

Instance Method Details

#catalogObject



45
46
47
# File 'lib/okf/bundle/folder.rb', line 45

def catalog
  @bundle.catalog
end

#concept(id) ⇒ Object

A single-file handle for one concept id (read live from disk), or nil when no concept in the loaded bundle has that id. The id may be a frontmatter id, so it is resolved to a path through the bundle rather than assumed to be "id.md".



62
63
64
65
# File 'lib/okf/bundle/folder.rb', line 62

def concept(id)
  path = @bundle.paths_by_id[id] or return nil
  Concept::File.read(root: @root, path: path)
end

#conceptsObject



29
30
31
# File 'lib/okf/bundle/folder.rb', line 29

def concepts
  @bundle.concepts
end

#directory_indexObject



49
50
51
# File 'lib/okf/bundle/folder.rb', line 49

def directory_index
  @bundle.directory_index
end

#graph(minimal: false, body: true) ⇒ Object



41
42
43
# File 'lib/okf/bundle/folder.rb', line 41

def graph(minimal: false, body: true)
  @bundle.graph(minimal: minimal, body: body)
end

#lint(**options) ⇒ Object



37
38
39
# File 'lib/okf/bundle/folder.rb', line 37

def lint(**options)
  @bundle.lint(**options)
end

#nameObject

Human-readable "parent/dir" name — the default HTML title.



54
55
56
57
# File 'lib/okf/bundle/folder.rb', line 54

def name
  pathname = Pathname.new(@root)
  "#{pathname.parent.basename}/#{pathname.basename}"
end

#reloadObject



80
81
82
83
# File 'lib/okf/bundle/folder.rb', line 80

def reload
  @bundle = Reader.read(@root)
  self
end

#save(overwrite: false) ⇒ Object

Materialize the in-memory bundle to disk (Writer validates §9 before publishing, so a malformed bundle is never written).



69
70
71
72
73
74
75
76
77
78
# File 'lib/okf/bundle/folder.rb', line 69

def save(overwrite: false)
  Writer.call(
    bundle_path: @root,
    concepts: @bundle.concepts,
    index_files: reserved_hash("index.md"),
    log_files: reserved_hash("log.md"),
    overwrite: overwrite
  )
  self
end

#validateObject



33
34
35
# File 'lib/okf/bundle/folder.rb', line 33

def validate
  @bundle.validate
end