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

Returns a new instance of Walker.



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

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

  gather
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



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

def cache
  @cache
end

#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



44
45
46
47
48
# File 'lib/trifle/docs/harvester.rb', line 44

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

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

#content_for(url:) ⇒ Object



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

def content_for(url:)
  route_for(url: url)&.content
end

#gatherObject

rubocop:disable Metrics/MethodLength



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

def gather # rubocop:disable Metrics/MethodLength
  Dir["#{path}/**/*.*"].each do |file|
    @harvesters.each do |harvester|
      sieve = harvester::Sieve.new(path: path, file: file)
      next unless sieve.match?

      @router[sieve.to_url] = harvester::Conveyor.new(
        file: file, url: sieve.to_url, namespace: namespace, cache: cache
      )
      break
    end
  end
  true
end

#meta_for(url:) ⇒ Object



54
55
56
# File 'lib/trifle/docs/harvester.rb', line 54

def meta_for(url:)
  route_for(url: url)&.meta
end

#not_found(url:) ⇒ Object



62
63
64
# File 'lib/trifle/docs/harvester.rb', line 62

def not_found(url:)
  puts "No route found for url: #{url}"
end

#route_for(url:) ⇒ Object



58
59
60
# File 'lib/trifle/docs/harvester.rb', line 58

def route_for(url:)
  @router[url] || not_found(url: url)
end

#sitemapObject



34
35
36
37
38
39
40
41
42
# File 'lib/trifle/docs/harvester.rb', line 34

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