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

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

Overview

rubocop:disable Metrics/ClassLength

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



70
71
72
73
74
# File 'lib/trifle/docs/harvester.rb', line 70

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

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

#content_for(url:) ⇒ Object



76
77
78
# File 'lib/trifle/docs/harvester.rb', line 76

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



91
92
93
# File 'lib/trifle/docs/harvester.rb', line 91

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

#not_found(url:) ⇒ Object



99
100
101
# File 'lib/trifle/docs/harvester.rb', line 99

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

#raw_content_for(url:) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/trifle/docs/harvester.rb', line 80

def raw_content_for(url:)
  conveyor = route_for(url: url)
  return unless conveyor

  if conveyor.respond_to?(:raw_content)
    conveyor.raw_content
  elsif conveyor.respond_to?(:content)
    conveyor.content
  end
end

#route_for(url:) ⇒ Object



95
96
97
# File 'lib/trifle/docs/harvester.rb', line 95

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

#search_for(query:, scope: nil, limit: 10) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/trifle/docs/harvester.rb', line 44

def search_for(query:, scope: nil, limit: 10) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
  return [] if query.nil? || query.strip.empty?

  query_terms = [query.downcase.strip]
  matches = []

  searchable_routes = filter_searchable_routes(scope)

  searchable_routes.each do |url, conveyor|
    score = calculate_fuzzy_match_score(conveyor, query_terms)
    next if score.zero?

    matches << {
      url: url,
      conveyor: conveyor,
      score: score
    }

    break if matches.size >= limit * 2
  end

  matches.sort_by { |match| -match[:score] }
         .first(limit)
         .map { |match| build_search_result(match, query_terms) }
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