Class: BomDB::Query
- Inherits:
-
Object
- Object
- BomDB::Query
- Defined in:
- lib/bomdb/query.rb
Instance Method Summary collapse
- #books ⇒ Object
- #chapters ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(edition: 1829, range: nil, search: nil, exclude: nil, exclude_only_quotations: false, headings: false) ⇒ Query
constructor
A new instance of Query.
- #print(verse_format: nil, body_format: nil, sep: ' ', linesep: "\n", io: $stdout) ⇒ Object
- #query ⇒ Object
- #wordgroups(wordcount) ⇒ Object
Constructor Details
#initialize(edition: 1829, range: nil, search: nil, exclude: nil, exclude_only_quotations: false, headings: false) ⇒ Query
Returns a new instance of Query.
5 6 7 8 9 10 11 12 |
# 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 |
Instance Method Details
#books ⇒ Object
72 73 74 75 76 77 78 79 80 |
# 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 ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# 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) ⇒ Object
56 57 58 |
# 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) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/bomdb/query.rb', line 92 def print(verse_format: nil, body_format: nil, sep: ' ', linesep: "\n", io: $stdout) shown = false verse_format ||= lambda{ |book, chapter, verse| "#{book}#{sep}#{chapter}:#{verse}" } body_format ||= lambda{ |body| body + linesep } query.each do |row| shown = true verse = verse_format[ row[:book_name], row[:verse_chapter], row[:verse_number] ] unless verse.empty? io.print verse io.print sep end begin content_body = row[:content_body] if @search content_body.gsub!(@search, @search.red) end io.print body_format[ content_body ] rescue TypeError next end end io.puts "Nothing to show" unless shown end |
#query ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# 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) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# 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 |