Class: Text::Gen::Investigator

Inherits:
Object
  • Object
show all
Defined in:
lib/text/gen/investigator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, lookup:, max_recursion: 10) ⇒ Investigator

Returns a new instance of Investigator.



8
9
10
11
12
# File 'lib/text/gen/investigator.rb', line 8

def initialize(key:, lookup:, max_recursion: 10)
  @store = Store.new(lookup)
  @key = key.to_s.downcase
  @max_recursion = max_recursion
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/text/gen/investigator.rb', line 6

def key
  @key
end

#lookupObject (readonly)

Returns the value of attribute lookup.



6
7
8
# File 'lib/text/gen/investigator.rb', line 6

def lookup
  @lookup
end

#max_recursionObject (readonly)

Returns the value of attribute max_recursion.



6
7
8
# File 'lib/text/gen/investigator.rb', line 6

def max_recursion
  @max_recursion
end

Instance Method Details

#investigateObject



14
15
16
# File 'lib/text/gen/investigator.rb', line 14

def investigate
  visit_and_recurse(key, 0).sort.uniq
end

#visit_and_recurse(key, depth) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/text/gen/investigator.rb', line 18

def visit_and_recurse(key, depth)
  depth += 1
  raise MaxRecursionError if depth > max_recursion

  builder = store.fetch(key)
  keys = [key]
  builder["items"].each do |item|
    item["segments"].each do |segment|
      if segment["type"] == "reference"
        keys << segment["text"]
        keys += visit_and_recurse(segment["text"], depth)
      end
    end
  end
  keys
rescue StandardError
  [key]
end