Method: BibTeX::Bibliography#query
- Defined in:
- lib/bibtex/bibliography.rb
#query(*arguments, &block) ⇒ Object Also known as: q
call-seq:
bib.query() #=> returns all elements
bib.query('@book') #=> returns all books
bib.query('@entry') #=> returns all entries (books, articles etc.)
bib.query('@*') #=> same as above
bib.query(:first, '@book, @article')
#=> returns the first book or article or nil
bib.query('@book[year<=2011], @article)
#=> returns all books published in 2011 or earlier and all articles
bib.query('@book, @article) { |o| o.year == '2011' }
#=> returns all books and articles published in 2011
bib.query('@book[year=2011], @article[year=2011])
#=> same as above without using a block
Returns objects in the Bibliography which match the given selector and, optionally, the conditions specified in the given block.
Queries offer syntactic sugar for common enumerator invocations:
>> bib.query(:all, '@book')
=> same as bib.select { |b| b.has_type?(:book) }
>> bib.query('@book')
=> same as above
>> bib.query(:first, '@book')
=> same as bib.detect { |b| b.has_type?(:book) }
>> bib.query(:none, '@book')
=> same as bib.reject { |b| b.has_type?(:book) }
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
# File 'lib/bibtex/bibliography.rb', line 465 def query(*arguments, &block) case arguments.length when 0 selector = :all q = nil when 1 selector = :all q = arguments[0] when 2 selector, q = arguments else raise ArgumentError, "wrong number of arguments (#{arguments.length} for 0..2)" end filter = block ? proc { |e| e.match?(q) && block.call(e) } : proc { |e| e.match?(q) } send(query_handler(selector), &filter) end |