class Dagger::Vertex

Vertex class for Dagger, representing a filesystem directory

dir/
  file.yaml         => keytree
  prefix@file.yaml  => prefix.keytree

+forest+ = [ +meta+       = {},
             +local+        [ { file_keys }, ... ],
                            [ { Default } ],
             +inherited+  = [ [ parent ], ... ]
           ]

Public Class Methods

new(name, cached: false) click to toggle source
# File lib/dagger/vertex.rb, line 23
def initialize(name, cached: false)
  @forest = initialize_forest(cached)
  @meta['_meta.name'] = name
  @meta['_meta.basename'] = File.basename(name)
  @meta['_meta.dirname'] = File.dirname(name)
end

Public Instance Methods

<<(keytree) click to toggle source
# File lib/dagger/vertex.rb, line 56
def <<(keytree)
  @local << keytree
end
[](key) click to toggle source
# File lib/dagger/vertex.rb, line 40
def [](key)
  key = key.to_key_path
  return @inherited[key.drop('^')] if key.prefix?('^')

  @forest[key]
end
added_to_graph(graph) click to toggle source
# File lib/dagger/vertex.rb, line 72
def added_to_graph(graph)
  raise %(belongs another graph) if @graph&.!= graph

  @graph = graph
end
edge_added(edge) click to toggle source
# File lib/dagger/vertex.rb, line 60
def edge_added(edge)
  return unless edge.head?(self)

  @inherited << edge.tail.to_key_wood
end
edge_removed(edge) click to toggle source
# File lib/dagger/vertex.rb, line 66
def edge_removed(edge)
  return unless edge.head?(self)

  @inherited.reject! { |tree| tree.equal?(edge.tail.to_key_wood) }
end
fetch(key, *default, &block) click to toggle source
# File lib/dagger/vertex.rb, line 47
def fetch(key, *default, &block)
  key = key.to_key_path
  if key.prefix?('^')
    @inherited.fetch(key.drop('^'), *default, &block)
  else
    @forest.fetch(key, *default, &block)
  end
end
flatten(cleanup: true) click to toggle source
# File lib/dagger/vertex.rb, line 84
def flatten(cleanup: true)
  forest = initialize_forest(true)

  forest.key_paths.select { |key| key.prefix?('_default') }.each do |key|
    forest[key.drop('_default')]
  end

  flattened = forest.flatten
  return flattened unless cleanup

  flattened.to_h.delete_if { |key| key.to_s.start_with?('_') }
  flattened
end
name() click to toggle source
# File lib/dagger/vertex.rb, line 35
def name
  @forest['_meta.name']
end
Also aliased as: to_s
removed_from_graph(graph) click to toggle source
# File lib/dagger/vertex.rb, line 78
def removed_from_graph(graph)
  raise %(not part of graph) if @graph&.!= graph

  @graph = nil
end
to_h() click to toggle source
# File lib/dagger/vertex.rb, line 98
def to_h
  flatten(cleanup: true).to_h
end
to_json(*args) click to toggle source
# File lib/dagger/vertex.rb, line 106
def to_json(*args)
  flatten(cleanup: true).to_json(*args)
end
to_key_forest() click to toggle source
# File lib/dagger/vertex.rb, line 30
def to_key_forest
  @forest
end
Also aliased as: to_key_wood
to_key_wood()
Alias for: to_key_forest
to_s()
Alias for: name
to_yaml() click to toggle source
# File lib/dagger/vertex.rb, line 102
def to_yaml
  flatten(cleanup: true).to_yaml
end

Private Instance Methods

initialize_default_tree(cached) click to toggle source
# File lib/dagger/vertex.rb, line 122
def initialize_default_tree(cached)
  default_args = cached ? { cached: true, fallback: @inherited } : {}
  default_proc = Default.proc(self, **default_args)
  KeyTree::Tree.new(&default_proc)
end
initialize_forest(cached) click to toggle source
# File lib/dagger/vertex.rb, line 112
def initialize_forest(cached)
  forest = KeyTree::Forest.new
  forest << @meta ||= KeyTree::Tree.new
  forest << @local ||= KeyTree::Forest.new
  forest << default = KeyTree::Forest.new
  forest << @inherited ||= KeyTree::Forest.new
  default << initialize_default_tree(cached)
  forest
end