Class: Kj::Chapter
Instance Attribute Summary collapse
- #book ⇒ Object
-
#book_id ⇒ Object
Returns the value of attribute book_id.
-
#id ⇒ Object
Returns the value of attribute id.
-
#number ⇒ Object
Returns the value of attribute number.
Class Method Summary collapse
Instance Method Summary collapse
- #book_name ⇒ Object
- #book_permalink ⇒ Object
-
#initialize(args) ⇒ Chapter
constructor
A new instance of Chapter.
- #next ⇒ Object
- #prev ⇒ Object
- #title ⇒ Object
- #to_s ⇒ Object
- #verse(verse_number) ⇒ Object
- #verses(*numbers) ⇒ Object
Constructor Details
#initialize(args) ⇒ Chapter
Returns a new instance of Chapter.
10 11 12 13 14 |
# File 'lib/kj/chapter.rb', line 10 def initialize(args) args.each do |k,v| instance_variable_set("@#{k}", v) unless v.nil? end end |
Instance Attribute Details
#book ⇒ Object
20 21 22 23 24 25 |
# File 'lib/kj/chapter.rb', line 20 def book @book ||= begin b = Db.query("SELECT id, name, permalink FROM books WHERE id = ?", [book_id], true) Book.new(id: b['id'], name: b['name'], permalink: b['permalink']) end end |
#book_id ⇒ Object
Returns the value of attribute book_id.
7 8 9 |
# File 'lib/kj/chapter.rb', line 7 def book_id @book_id end |
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/kj/chapter.rb', line 7 def id @id end |
#number ⇒ Object
Returns the value of attribute number.
7 8 9 |
# File 'lib/kj/chapter.rb', line 7 def number @number end |
Class Method Details
.count ⇒ Object
59 60 61 |
# File 'lib/kj/chapter.rb', line 59 def self.count 1189 end |
Instance Method Details
#book_name ⇒ Object
27 28 29 |
# File 'lib/kj/chapter.rb', line 27 def book_name book.name end |
#book_permalink ⇒ Object
31 32 33 |
# File 'lib/kj/chapter.rb', line 31 def book_permalink book.permalink end |
#next ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/kj/chapter.rb', line 39 def next @next ||= begin c = Db.query("SELECT id, book_id, number FROM chapters WHERE id = ?", [id + 1], true) self.class.new(id: c['id'], book_id: c['book_id'], number: c['number']) rescue Kj::Iniquity c = Db.query("SELECT id, book_id, number FROM chapters WHERE id = ?", [1], true) self.class.new(id: c['id'], book_id: c['book_id'], number: c['number']) end end |
#prev ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/kj/chapter.rb', line 49 def prev @prev ||= begin c = Db.query("SELECT id, book_id, number FROM chapters WHERE id = ?", [id - 1], true) self.class.new(id: c['id'], book_id: c['book_id'], number: c['number']) rescue Kj::Iniquity c = Db.query("SELECT id, book_id, number FROM chapters WHERE id = ?", [self.class.count], true) self.class.new(id: c['id'], book_id: c['book_id'], number: c['number']) end end |
#title ⇒ Object
16 17 18 |
# File 'lib/kj/chapter.rb', line 16 def title "#{book_name} #{number}" end |
#to_s ⇒ Object
35 36 37 |
# File 'lib/kj/chapter.rb', line 35 def to_s title end |
#verse(verse_number) ⇒ Object
68 69 70 71 |
# File 'lib/kj/chapter.rb', line 68 def verse(verse_number) verse = Db.query("SELECT id FROM verses WHERE chapter_id = ? AND number = ?", [id, verse_number], true) Verse.new(id: verse['id'], book_name: book_name, chapter_id: id, number: verse_number) end |
#verses(*numbers) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/kj/chapter.rb', line 73 def verses(*numbers) if numbers.empty? @verses ||= begin results = Db.query("SELECT id, number, page FROM verses WHERE chapter_id = ?", [id]) results.map{|verse| Verse.new(id: verse['id'], book_name: book_name, chapter_id: id, number: verse['number'], page: verse['page'])} end else results = Db.query("SELECT id, number, page FROM verses WHERE chapter_id = #{id} AND number IN (#{numbers.flatten.join(',')})") results.map!{|verse| Verse.new(id: verse['id'], book_name: book_name, chapter_id: id, number: verse['number'], page: verse['page'])}.sort!{ |a,b| a.id <=> b.id } end end |