Class: BibleBot::ReferenceMatch

Inherits:
Object
  • Object
show all
Defined in:
lib/bible_bot/reference_match.rb

Overview

Note:

You shouldn’t need to use this class directly. For the majority of use cases, just use BibleBot::Reference.parse.

This class contains all the logic for mapping the different parts of a scripture Match into an actual Reference. It wraps the Match returned from the regular expression defined in Bible.scripture_re.

A scripture reference can take many forms, but the least abbreviated form is:

Genesis 1:1 - Genesis 1:2

Internally, this class represents this form using the following variables:

b1 c1:v1 - b2 c2:v2

See Readme for list of supported abbreviation rules.

Advanced Use Cases

This is a low level class used internally by BibleBot::Reference.parse. There are however some advanced use cases which you might want to use it for. For example, if you want to know where in the parsed String certain matches occur.

For this there are a few convenience attributes:

Examples:

matches = ReferenceMatch.scan("Mark 1:5 and another Romans 4:1")
matches[0].match[0]   #=> "Mark 1:5"
matches[0].offset     #=> 0
matches[0].length     #=> 8
matches[0].match[0]   #=> "Romans 4:1"
matches[1].offset     #=> 21
matches[1].length     #=> 10

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lengthInteger (readonly)

Returns The length of the match in the text string.

Returns:

  • (Integer)

    The length of the match in the text string



39
40
41
# File 'lib/bible_bot/reference_match.rb', line 39

def length
  @length
end

#matchMatch (readonly)

Returns The Match instance returned from the Regexp.

Returns:

  • (Match)

    The Match instance returned from the Regexp



38
39
40
# File 'lib/bible_bot/reference_match.rb', line 38

def match
  @match
end

#offsetInteger (readonly)

Returns The starting position of the match in the text string.

Returns:

  • (Integer)

    The starting position of the match in the text string



40
41
42
# File 'lib/bible_bot/reference_match.rb', line 40

def offset
  @offset
end

Class Method Details

.scan(text) ⇒ Array<ReferenceMatch>

Converts a string into an array of ReferenceMatches. Note: Does not validate References.

Parameters:

  • text (String)

Returns:



47
48
49
50
51
52
# File 'lib/bible_bot/reference_match.rb', line 47

def self.scan(text)
  scripture_reg = Bible.scripture_re
  Array.new.tap do |matches|
    text.scan(scripture_reg){ matches << self.new($~, $~.offset(0)[0]) }
  end
end

Instance Method Details

#referenceReference

Returns Note: Reference is not yet validated.

Returns:

  • (Reference)

    Note: Reference is not yet validated



55
56
57
58
59
60
# File 'lib/bible_bot/reference_match.rb', line 55

def reference
  @reference ||= Reference.new(
    start_verse: Verse.new(book: start_book, chapter_number: start_chapter.to_i, verse_number: start_verse.to_i),
    end_verse: Verse.new(book: end_book, chapter_number: end_chapter.to_i, verse_number: end_verse.to_i),
  )
end