Class: Kj::Book

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Book

Returns a new instance of Book.



9
10
11
12
13
# File 'lib/kj/book.rb', line 9

def initialize(args)
  args.each do |k,v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end
end

Instance Attribute Details

#abbreviationsObject

Returns the value of attribute abbreviations.



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

def abbreviations
  @abbreviations
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Returns the value of attribute permalink.



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

def permalink
  @permalink
end

Class Method Details

.allObject



42
43
44
45
46
47
# File 'lib/kj/book.rb', line 42

def self.all
  @all ||= begin
    books = Db.query("SELECT id, name, permalink, abbreviations FROM books")
    books.map{ |book| new(id: book['id'], name: book['name'], permalink: book['permalink'], abbreviations: book['abbreviations']) }
  end
end

.countObject



49
50
51
# File 'lib/kj/book.rb', line 49

def self.count
  66
end

.from_name_or_number(name_or_number) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/kj/book.rb', line 23

def self.from_name_or_number(name_or_number)
  if name_or_number.is_a?(Integer)
    book = Db.query("SELECT id, name, permalink FROM books WHERE id = ?", [name_or_number], true)
  else
    book = Db.query("SELECT id, name, permalink FROM books WHERE name = ? OR abbreviations LIKE ? OR permalink = ?", [name_or_number.to_s.downcase, "%#{name_or_number.to_s}%", name_or_number.to_s], true)
  end
  new(id: book['id'], name: book['name'], permalink: book['permalink'])
end

.from_names_or_numbers(names_or_numbers) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/kj/book.rb', line 32

def self.from_names_or_numbers(names_or_numbers)
  names_or_numbers.flatten!
  numbers = names_or_numbers.uniq.select{|n| n.is_a?(Integer)}
  names = (names_or_numbers - numbers).uniq.map{|name| name.to_s.downcase}
  results = []
  results << Db.query("SELECT id, name, permalink, abbreviations FROM books WHERE id IN (#{numbers.join(',')})") if numbers.any?
  names.each{ |name| results << Db.query("SELECT id, name, permalink, abbreviations FROM books WHERE abbreviations LIKE ? OR permalink = ?", ["%#{name.to_s}%", name.to_s]) }
  results.flatten.map!{|book| new(id: book['id'], name: book['name'], permalink: book['permalink'], abbreviations: book['abbreviations'])}.uniq.sort!{ |a,b| a.id <=> b.id }
end

.randomObject



53
54
55
# File 'lib/kj/book.rb', line 53

def self.random
  from_name_or_number(rand(1..count))
end

Instance Method Details

#chapter(number) ⇒ Object



57
58
59
60
# File 'lib/kj/book.rb', line 57

def chapter(number)
  chapter = Db.query("SELECT id FROM chapters WHERE book_id = ? AND number = ?", [id, number], true)
  Chapter.new(id: chapter['id'], book_id: id, number: number)
end

#chapters(*numbers) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kj/book.rb', line 62

def chapters(*numbers)
  if numbers.empty?
    @chapters ||= begin
      results = Db.query("SELECT id, number FROM chapters WHERE book_id = ?", [id])
      results.map{|chapter| Chapter.new(id: chapter['id'], book_id: id, number: chapter['number'])}
    end
  else
    results = Db.query("SELECT id, number FROM chapters WHERE book_id = #{id} AND number IN (#{numbers.flatten.join(',')})")
    results.map{|chapter| Chapter.new(id: chapter['id'], book_id: id, number: chapter['number'])}.sort!{ |a,b| a.id <=> b.id }
  end
end

#titleObject



15
16
17
# File 'lib/kj/book.rb', line 15

def title
  name
end

#to_sObject



19
20
21
# File 'lib/kj/book.rb', line 19

def to_s
  name
end