Class: BomDB::Models::Verse

Inherits:
Object
  • Object
show all
Defined in:
lib/bomdb/models/verse.rb

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ Verse

Returns a new instance of Verse.



4
5
6
# File 'lib/bomdb/models/verse.rb', line 4

def initialize(db)
  @db = db
end

Instance Method Details

#create(chapter:, verse:, book_name: nil, book_id: nil, heading: false, range_id: nil) ⇒ Object

Create a verse and return its verse_id



19
20
21
22
23
24
25
26
27
# File 'lib/bomdb/models/verse.rb', line 19

def create(chapter:, verse:, book_name: nil, book_id: nil, heading: false, range_id: nil)
  @db[:verses].insert(
    book_id:       book_by_name_or_id(book_name, book_id),
    verse_chapter: chapter,
    verse_number:  verse,
    verse_heading: heading ? 0 : nil,
    verse_range_id: range_id
  )
end

#find(chapter:, verse:, book_name: nil, book_id: nil, heading: false) ⇒ Object

Find a verse and return a hash, or nil if not found



9
10
11
12
13
14
15
16
# File 'lib/bomdb/models/verse.rb', line 9

def find(chapter:, verse:, book_name: nil, book_id: nil, heading: false)
  @db[:verses].where(
    book_id:       book_by_name_or_id(book_name, book_id),
    verse_chapter: chapter,
    verse_number:  verse,
    verse_heading: heading ? 0 : nil
  ).first
end

#find_or_create(chapter:, verse:, book_name: nil, book_id: nil, heading: false, range_id: nil) ⇒ Object

Returns a verse_id after finding or creating the verse



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bomdb/models/verse.rb', line 30

def find_or_create(chapter:, verse:, book_name: nil, book_id: nil, heading: false, range_id: nil)
  v = find(
    chapter: chapter, verse: verse,
    book_name: book_name, book_id: book_id,
    heading: heading
  )
  (v && v[:verse_id]) || create(
    chapter: chapter, verse: verse,
    book_name: book_name, book_id: book_id,
    heading: heading, range_id: range_id
  )
end