Class: Kj::Book
Instance Attribute Summary collapse
-
#abbreviations ⇒ Object
Returns the value of attribute abbreviations.
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#permalink ⇒ Object
Returns the value of attribute permalink.
Class Method Summary collapse
- .all ⇒ Object
- .count ⇒ Object
- .from_name_or_number(name_or_number) ⇒ Object
- .from_names_or_numbers(names_or_numbers) ⇒ Object
- .random ⇒ Object
Instance Method Summary collapse
- #chapter(number) ⇒ Object
- #chapters(*numbers) ⇒ Object
-
#initialize(args) ⇒ Book
constructor
A new instance of Book.
- #title ⇒ Object
- #to_s ⇒ Object
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
#abbreviations ⇒ Object
Returns the value of attribute abbreviations.
7 8 9 |
# File 'lib/kj/book.rb', line 7 def abbreviations @abbreviations end |
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/kj/book.rb', line 7 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/kj/book.rb', line 7 def name @name end |
#permalink ⇒ Object
Returns the value of attribute permalink.
7 8 9 |
# File 'lib/kj/book.rb', line 7 def permalink @permalink end |
Class Method Details
.all ⇒ Object
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 |
.count ⇒ Object
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 |
.random ⇒ Object
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 |
#title ⇒ Object
15 16 17 |
# File 'lib/kj/book.rb', line 15 def title name end |
#to_s ⇒ Object
19 20 21 |
# File 'lib/kj/book.rb', line 19 def to_s name end |