Class: BomDB::Query
- Inherits:
-
Object
- Object
- BomDB::Query
- Defined in:
- lib/bomdb/query.rb
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#initialize(edition:, 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
Constructor Details
#initialize(edition:, 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:, exclude: nil, headings: false) @edition = edition @exclude = exclude @headings = headings end |
Instance Method Details
#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
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/bomdb/query.rb', line 38 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 |