Class: BrowserOfBabel::Locator

Inherits:
Object
  • Object
show all
Defined in:
lib/browser_of_babel/locator.rb

Overview

Finds correct holotheca or text from a reference.

Constant Summary collapse

MAX_DEPTH =
Page.depth
SEP =
/\./
HOLOTHECA_FORMAT =
/(?<holotheca>#{Hex::FORMAT}(?:(?<separator>#{SEP})\d+){0,#{MAX_DEPTH - 1}})/o
TEXT_RANGE_FORMAT =
/(?>(?<range>\d+)|\[(?<range>(?>\d+-\d+|\d+)(?:,\s*(?>\d+-\d+|\d+))*)\])/o
DEFAULT_FORMAT =
/\A#{HOLOTHECA_FORMAT}(?:#{SEP}?#{TEXT_RANGE_FORMAT})?\z/o

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format: DEFAULT_FORMAT) ⇒ Locator

Returns a new instance of Locator.

Parameters:

  • format (Regexp) (defaults to: DEFAULT_FORMAT)

    regexp to parse references; must have capturing groups holotheca, separator, range (text ranges), see DEFAULT_FORMAT for inspiration



21
22
23
# File 'lib/browser_of_babel/locator.rb', line 21

def initialize(format: DEFAULT_FORMAT)
  @format = format
end

Instance Attribute Details

#formatRegexp (readonly)

Returns:

  • (Regexp)


16
17
18
# File 'lib/browser_of_babel/locator.rb', line 16

def format
  @format
end

Instance Method Details

#call(reference) ⇒ Holotheca, String Also known as: from_string

Find a holotheca from a string reference, possibly extracting text from a page.

Examples:

Locator.new.call("2ab.2.4.16.121")
# => Library, Hex 2ab, Wall 2, Shelf 4, Volume 16, Page 121
locator = Locator.new
locator.call("xeh1.2.3.4.5[1-20]")
# => blyxpmaggmbnbri ,xso
locator.call("xeh1.2.3.4.5[1-10,12,15,18-20]")
# => blyxpmaggmnixso

Parameters:

  • reference (String)

Returns:

  • (Holotheca)

    if reference does not contain text ranges

  • (String)

    if reference points to a page and contains text ranges

Raises:

  • (ArgumentError)

    if reference does not match expected format

  • (InvalidIdentifierError)

    if reference contains invalid identifiers

  • (InvalidHolothecaError)

    if reference contains text ranges, but does not point to a page



42
43
44
45
46
47
48
49
50
51
# File 'lib/browser_of_babel/locator.rb', line 42

def call(reference)
  match = format.match(reference)
  return invalid_reference unless match

  reference = match[:holotheca] # : String
  identifiers =
    match[:separator] ? reference.split(match[:separator]) : reference
  identifiers = Array(identifiers) # : Array[String]
  from_identifiers(*identifiers, ranges: extract_ranges(match[:range]))
end

#from_identifiers(*identifiers, ranges: nil) ⇒ Holotheca, String

Find a holotheca from an array of identifiers, possibly extracting text from a page.

Parameters:

  • identifiers (Array<String, Integer>)
  • ranges (Array<Integer, Range<Integer>>) (defaults to: nil)

Returns:

  • (Holotheca)

    if ranges is nil

  • (String)

    if identifiers point to a page, and ranges contain text ranges

Raises:



64
65
66
67
68
69
70
71
# File 'lib/browser_of_babel/locator.rb', line 64

def from_identifiers(*identifiers, ranges: nil)
  holotheca = Library.new.dig(*identifiers)
  return holotheca unless ranges
  return invalid_holotheca unless holotheca.is_a?(Page)

  ranges = Array(ranges)
  ranges.map { holotheca[_1] }.join
end