Class: Kj::Chapter

Inherits:
Base
  • Object
show all
Defined in:
lib/kj/chapter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#bookObject



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_idObject

Returns the value of attribute book_id.



7
8
9
# File 'lib/kj/chapter.rb', line 7

def book_id
  @book_id
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/kj/chapter.rb', line 7

def id
  @id
end

#numberObject

Returns the value of attribute number.



7
8
9
# File 'lib/kj/chapter.rb', line 7

def number
  @number
end

Class Method Details

.countObject



59
60
61
# File 'lib/kj/chapter.rb', line 59

def self.count
  1189
end

.randomObject



63
64
65
66
# File 'lib/kj/chapter.rb', line 63

def self.random
  chapter = Db.query("SELECT id, book_id, number FROM chapters WHERE id = ?", [rand(1..count)], true)
  new(id: chapter['id'], book_id: chapter['book_id'], number: chapter['number'])
end

Instance Method Details

#book_nameObject



27
28
29
# File 'lib/kj/chapter.rb', line 27

def book_name
  book.name
end


31
32
33
# File 'lib/kj/chapter.rb', line 31

def book_permalink
  book.permalink
end

#nextObject



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

#prevObject



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

#titleObject



16
17
18
# File 'lib/kj/chapter.rb', line 16

def title
  "#{book_name} #{number}"
end

#to_sObject



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