Class: BrowserOfBabel::Locator
- Inherits:
-
Object
- Object
- BrowserOfBabel::Locator
- 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
- #format ⇒ Regexp readonly
Instance Method Summary collapse
-
#call(reference) ⇒ Holotheca, String
(also: #from_string)
Find a holotheca from a string reference, possibly extracting text from a page.
-
#from_identifiers(*identifiers, ranges: nil) ⇒ Holotheca, String
Find a holotheca from an array of identifiers, possibly extracting text from a page.
-
#initialize(format: DEFAULT_FORMAT) ⇒ Locator
constructor
A new instance of Locator.
Constructor Details
#initialize(format: DEFAULT_FORMAT) ⇒ Locator
Returns a new instance of Locator.
21 22 23 |
# File 'lib/browser_of_babel/locator.rb', line 21 def initialize(format: DEFAULT_FORMAT) @format = format end |
Instance Attribute Details
#format ⇒ Regexp (readonly)
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.
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.
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 |