class BomDB::Query

Public Class Methods

new(edition: 1829, range: nil, search: nil, exclude: nil, exclude_only_quotations: false, headings: false) click to toggle source
# File lib/bomdb/query.rb, line 5
def initialize(edition: 1829, range: nil, search: nil, exclude: nil, exclude_only_quotations: false, headings: false)
  @edition = edition
  @range = range
  @search = search
  @exclude = exclude
  @exclude_only_quotations = exclude_only_quotations
  @headings = headings
end

Public Instance Methods

books() click to toggle source
# File lib/bomdb/query.rb, line 72
def books
  groups = query.all.group_by{ |x| x[:book_name] }
  Enumerator.new(groups.size) do |y|
    groups.each do |heading, rows|
      content = rows.map{ |r| r[:content_body] }.join(" ")
      y.yield(heading, content)
    end
  end
end
chapters() click to toggle source
# File lib/bomdb/query.rb, line 60
def chapters
  groups = query.all.group_by do |x|
    [x[:book_name], x[:verse_chapter]]
  end
  Enumerator.new(groups.size) do |y|
    groups.each do |heading, rows|
      content = rows.map{ |r| r[:content_body] }.join(" ")
      y.yield(heading, content)
    end
  end
end
each(&block) click to toggle source
# File lib/bomdb/query.rb, line 56
def each(&block)
  query.each(&block)
end
print(verse_format: nil, body_format: nil, sep: ' ', linesep: "\n", io: $stdout) click to toggle source
query() click to toggle source
# File lib/bomdb/query.rb, line 14
def query
  db = BomDB.db
  edition_model = Models::Edition.new(db)
  edition = edition_model.find(@edition)
  if edition.nil?
    raise "Unable to find edition: #{@edition}"
  end
  q = db[:verses].
    join(:books, :book_id => :book_id).
    join(:editions).
    join(:contents, :edition_id => :edition_id, :verse_id => Sequel.qualify("verses", "verse_id")).
    order(:book_sort, :verse_heading, :verse_chapter, :verse_number).
    select(:book_name, :verse_chapter, :verse_number, :content_body)
  if @edition
    q = q.where(Sequel.qualify("editions", "edition_id") => edition[:edition_id])
  end
  if not @headings
    q = q.where(:verse_heading => nil)
  end
  if @range
    pericope = Mericope.new(@range)
    pairs = pericope.ranges.map{ |r| [:verse_range_id, r] }
    q = q.where(Sequel::SQL::BooleanExpression.from_value_pairs(pairs, :OR))
  end
  if @search
    q = q.where(Sequel.like(Sequel.function(:LOWER, :content_body), "%#{@search.downcase}%"))
  end
  if @exclude
    excluded_ref_names = @exclude.split(/\s*,\s*/).map do |name|
      Sequel.like(:ref_name, "#{name}%")
    end
    excluded_verse_ids = db[:refs].
      select(:verse_id).
      where(Sequel.&(excluded_ref_names))
    if @exclude_only_quotations
      excluded_verse_ids = excluded_verse_ids.where(ref_is_quotation: true)
    end
    q = q.exclude(Sequel.qualify("verses", "verse_id") => excluded_verse_ids)
  end
  q
end
wordgroups(wordcount) click to toggle source
# File lib/bomdb/query.rb, line 82
def wordgroups(wordcount)
  content = query.all.map{ |r| r[:content_body] }.join(" ")
  groups = content.scan(/[^ ]+/).each_slice(wordcount).map{ |w| w.join(" ") }
  Enumerator.new(groups.size) do |y|
    groups.each do |g|
      y.yield(g)
    end
  end
end