Class: Yarrow::Content::Expansion::Traversal

Inherits:
Object
  • Object
show all
Defined in:
lib/yarrow/content/expansion/traversal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, policy) ⇒ Traversal

Returns a new instance of Traversal.



7
8
9
10
11
# File 'lib/yarrow/content/expansion/traversal.rb', line 7

def initialize(graph, policy)
  @graph = graph
  @policy = policy
  @aggregator = policy.aggregator_const.new(graph)
end

Instance Attribute Details

#aggregatorObject (readonly)

Returns the value of attribute aggregator.



5
6
7
# File 'lib/yarrow/content/expansion/traversal.rb', line 5

def aggregator
  @aggregator
end

#graphObject (readonly)

Returns the value of attribute graph.



5
6
7
# File 'lib/yarrow/content/expansion/traversal.rb', line 5

def graph
  @graph
end

#policyObject (readonly)

Returns the value of attribute policy.



5
6
7
# File 'lib/yarrow/content/expansion/traversal.rb', line 5

def policy
  @policy
end

Instance Method Details

#complete_traversalObject



43
44
45
# File 'lib/yarrow/content/expansion/traversal.rb', line 43

def complete_traversal
  aggregator.after_traversal(policy)
end

#expandObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/yarrow/content/expansion/traversal.rb', line 47

def expand
  start_traversal

  traversal = source_node.depth_first.each
  
  visit_source(traversal.next)

  loop do
    node = traversal.next
    case node.label
    when :directory then visit_directory(node)
    when :file then visit_file(node)
    end
  end

  complete_traversal
end

#source_nodeObject

If source path represents entire content dir, then include the entire content dir instead of scanning from a subfolder matching the name of the collection.



16
17
18
19
20
21
22
# File 'lib/yarrow/content/expansion/traversal.rb', line 16

def source_node
  if policy.source_path == "."
    graph.n(:root).out(:directory)
  else
    graph.n(name: policy.source_path)
  end
end

#start_traversalObject



39
40
41
# File 'lib/yarrow/content/expansion/traversal.rb', line 39

def start_traversal
  aggregator.before_traversal(policy)
end

#visit_directory(dir_node) ⇒ Object



28
29
30
31
# File 'lib/yarrow/content/expansion/traversal.rb', line 28

def visit_directory(dir_node)
  # TODO: match on potential directory extension/filter
  aggregator.expand_directory(dir_node, policy)
end

#visit_file(file_node) ⇒ Object



33
34
35
36
37
# File 'lib/yarrow/content/expansion/traversal.rb', line 33

def visit_file(file_node)
  # TODO: dispatch underscore prefix or index files separately
  # TODO: match on file extension
  aggregator.expand_file(file_node, policy)
end

#visit_source(root_node) ⇒ Object



24
25
26
# File 'lib/yarrow/content/expansion/traversal.rb', line 24

def visit_source(root_node)
  aggregator.expand_source(root_node, policy)
end