Class: BibleRef::Reference

Inherits:
Object
  • Object
show all
Defined in:
lib/bible_ref/reference.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reference, language: 'eng', canon: 'protestant') ⇒ Reference

Create a new Reference instance by passing in the user-supplied bible reference as a string.



10
11
12
13
14
15
# File 'lib/bible_ref/reference.rb', line 10

def initialize(reference, language: 'eng', canon: 'protestant')
  @reference = reference
  @details = parse
  @language = language.respond_to?(:book_id) ? language : LANGUAGES.fetch(language.to_s).new
  @canon = canon.respond_to?(:books) ? canon : CANONS.fetch(canon.to_s).new
end

Instance Attribute Details

#bookObject (readonly)

Returns the value of attribute book.



7
8
9
# File 'lib/bible_ref/reference.rb', line 7

def book
  @book
end

#canonObject (readonly)

Returns the value of attribute canon.



7
8
9
# File 'lib/bible_ref/reference.rb', line 7

def canon
  @canon
end

#languageObject (readonly)

Returns the value of attribute language.



7
8
9
# File 'lib/bible_ref/reference.rb', line 7

def language
  @language
end

#referenceObject (readonly)

Returns the value of attribute reference.



7
8
9
# File 'lib/bible_ref/reference.rb', line 7

def reference
  @reference
end

Instance Method Details

#book_idObject

Returns a USFX-compatible book id, or nil if book is unknown.



31
32
33
34
# File 'lib/bible_ref/reference.rb', line 31

def book_id
  return nil unless @details
  @book_id ||= @language.book_id(@details[:book], @canon)
end

#book_nameObject

Returns the formatted book name



37
38
39
40
# File 'lib/bible_ref/reference.rb', line 37

def book_name
  return nil unless @details
  @book_name ||= @language.book_name(@details[:book], @canon)
end

#normalizeObject

Returns a normalized passage reference. e.g.

‘JOHN 3:16&17’ => ‘John 3:16,17’



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bible_ref/reference.rb', line 50

def normalize
  return unless book_id and ranges
  book_name + ' ' +
  ranges.map do |(ref_from, ref_to)|
    if ref_from != ref_to
      ref_part(ref_from) + '-' + ref_part(ref_to)
    else
      ref_part(ref_from)
    end
  end.join(',')
end

#rangesObject

Returns an array of pairs, each one being the from and to for a range. For single verses, the same ref is repeated twice. This is most helpful for converting the entire passage into a SQL query, and in fact is exactly why this library was built. See github.com/seven1m/bible_api/blob/master/app.rb for an example.



22
23
24
25
26
27
28
# File 'lib/bible_ref/reference.rb', line 22

def ranges
  return nil unless valid?
  @chapter = nil
  [@details[:refs]].flatten.map do |ref|
    normalize_range(ref) || normalize_ref(ref)
  end
end

#valid?Boolean

Returns true if the reference is a known bible passage.

Returns:

  • (Boolean)


43
44
45
# File 'lib/bible_ref/reference.rb', line 43

def valid?
  @details && !book_id.nil?
end