Class: Trifle::Docs::Harvester::Walker

Inherits:
Object
  • Object
show all
Defined in:
lib/trifle/docs/harvester.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**keywords) ⇒ Walker



9
10
11
12
13
14
15
16
# File 'lib/trifle/docs/harvester.rb', line 9

def initialize(**keywords)
  @path = keywords.fetch(:path)
  @harvesters = keywords.fetch(:harvesters)
  @namespace = keywords.fetch(:namespace)
  @router = {}

  gather
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



7
8
9
# File 'lib/trifle/docs/harvester.rb', line 7

def namespace
  @namespace
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/trifle/docs/harvester.rb', line 7

def path
  @path
end

#routerObject (readonly)

Returns the value of attribute router.



7
8
9
# File 'lib/trifle/docs/harvester.rb', line 7

def router
  @router
end

Instance Method Details

#collection_for(url:) ⇒ Object



41
42
43
44
45
# File 'lib/trifle/docs/harvester.rb', line 41

def collection_for(url:)
  return sitemap if url.empty?

  sitemap.dig(*url.split('/'))
end

#content_for(url:) ⇒ Object



47
48
49
# File 'lib/trifle/docs/harvester.rb', line 47

def content_for(url:)
  @router[url].content
end

#gatherObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/trifle/docs/harvester.rb', line 18

def gather
  Dir["#{path}/**/*.*"].each do |file|
    @harvesters.each do |harvester|
      sieve = harvester::Sieve.new(path: path, file: file)
      if sieve.match?
        @router[sieve.to_url] = harvester::Conveyor.new(file: file, url: sieve.to_url, namespace: namespace)
        break
      end
    end
  end
  true
end

#meta_for(url:) ⇒ Object



51
52
53
# File 'lib/trifle/docs/harvester.rb', line 51

def meta_for(url:)
  @router[url].meta
end

#sitemapObject



31
32
33
34
35
36
37
38
39
# File 'lib/trifle/docs/harvester.rb', line 31

def sitemap
  @sitemap ||= begin
    mapping = router.keys.each_with_object({}) do |url, out|
      out[url] = meta_for(url: url)
    end

    Trifle::Docs::Helper::Tree.new(mapping: mapping).menu
  end
end