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, exclude: nil, 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, exclude: nil, headings: false) ⇒ Query
Returns a new instance of Query.
5 6 7 8 9 |
# File 'lib/bomdb/query.rb', line 5 def initialize(edition: 1829, exclude: nil, headings: false) @edition = edition @exclude = exclude @headings = headings end |
Instance Method Details
#books ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/bomdb/query.rb', line 50 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
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bomdb/query.rb', line 38 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
34 35 36 |
# File 'lib/bomdb/query.rb', line 34 def each(&block) query.each(&block) end |
#print(verse_format: nil, body_format: nil, sep: ' ', linesep: "\n", io: $stdout) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/bomdb/query.rb', line 70 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 io.print body_format[ row[:content_body] ] rescue TypeError next end end io.puts "Nothing shown" unless shown end |
#query ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/bomdb/query.rb', line 11 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 => :verses__verse_id). order(:book_sort, :verse_heading, :verse_chapter, :verse_number). select(:book_name, :verse_chapter, :verse_number, :content_body) q.where!(:editions__edition_id => edition[:edition_id]) if @edition q.where!(:verse_heading => nil) unless @headings if @exclude excluded_verse_ids = db[:refs].select(:verse_id). where(:ref_name => @exclude.split(/\s*,\s*/)) q.exclude!(:verses__verse_id => excluded_verse_ids) end q end |
#wordgroups(wordcount) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/bomdb/query.rb', line 60 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 |