module RPaste class ResultSet < Array # # Creates a new ResultSet object with the given _results_. # def initialize(results=[],&block) @pastes = {} super(results,&block) end # # Selects metadata with the matching _name_. # # results.with_name('secret') # results.with_name(/login/) # def with_name(name) if name.kind_of?(Regexp) return self.select { |data| data.name =~ name } else return self.select { |data| data.name == name } end end # # Selects metadata with the matching _author_. # # results.by_author('dj food') # results.by_author(/^ro/) # def by_author(author) if author.kind_of?(Regexp) return self.select { |data| data.author =~ author } else return self.select { |data| data.author == author } end end # # Iterates over the metadata with the matching name, passing each # to the given _block_. # # results.each_with_name('secret') # results.each_with_name(/reg/) # def each_with_name(name,&block) with_name(name).each(&block) end # # Iterates over the metadata with the matching author name, passing # each to the given _block_. # # results.each_by_author('aphex twin') # results.each_by_author(/square/) # def each_by_author(author,&block) by_author(author).each(&block) end # # Returns the paste at the given _index_. If _index_ is a Range, then # all Pastes within the range are returned. # # results.paste(10) # results.paste(1..4) # def paste(index) if index.kind_of?(Range) return self[index].map { |data| cache_paste(data) } else return cache_paste(self[index]) end end # # Returns all pastes. # def pastes self.map { |data| cache_paste(data) } end # # Iterates over all pastes, passing each to the given _block_. # # results.each_paste do |paste| # if paste.syntax == 'ASM' # puts paste # end # end # def each_paste(&block) pastes.each(&block) end # # Returns the pastes which match the given _block_. # def pastes_with(&block) self.select(&block).map { |data| cache_paste(data) } end # # Returns the pastes which have the matching _name_. # # results.pastes_with_name('config') # results.pastes_with_name(/rock/) # def pastes_with_name(name) if name.kind_of?(Regexp) return pastes_with { |data| data.name =~ name } else return pastes_with { |data| data.name == name } end end # # Returns the pastes which have the matching _author_. # # results.pastes_by_author('adam freeland') # results.pastes_by_author(/hendrix/) # def pastes_by_author(author) if author.kind_of?(Regexp) return pastes_with { |data| data.author =~ author } else return pastes_with { |data| data.author == author } end end # # Returns the pastes which have the matching _date_. # # results.pastes_on_date(Time.now) # def pastes_on_date(date) pastes_with { |data| data.date == date } end # # Returns the pastes which have the matching _syntax_. # # results.pastes_with_syntax('C') # def pastes_with_syntax(syntax) pastes_with { |data| data.syntax == syntax } end # # Returns the pastes which contain the specified _text_. # # results.pastes_with_text('ls') # def pastes_with_text(text) if text.kind_of?(Regexp) return pastes_with { |data| data.text =~ text } else return pastes_with { |data| data.text == text } end end # # Iterates over the pastes with the matching _name_, passing each # to the given _block_. # # results.each_paste_with_name('code') do |paste| # puts paste # end # def each_paste_with_name(name,&block) pastes_with_name(name).each(&block) end # # Iterates over the pastes with the matching _author_, passing each # to the given _block_. # # results.each_paste_with_name('King Tubby') do |paste| # puts paste # end # def each_paste_by_author(author,&block) pastes_by_author(author).each(&block) end # # Iterates over the pastes with the matching _date_, passing each # to the given _block_. # # results.each_paste_on_date(Time.now) do |paste| # puts paste # end # def each_paste_on_date(date,&block) pastes_on_date(date).each(&block) end # # Iterates over the pastes with the matching _syntax_, passing each # to the given _block_. # # results.each_paste_with_syntax('Ruby') do |paste| # puts paste # end # def each_paste_with_syntax(syntax,&block) pastes_with_syntax(syntax).each(&block) end # # Iterates over the pastes containing the specified _text_, passing # each to the given _block_. # # results.each_paste_with_text('strcpy') do |paste| # puts paste # end # # results.each_paste_with_text(/strcpy\(\w+,\w+\)/) do |paste| # puts paste # end # def each_paste_with_text(text,&block) pastes_with_text(text).each(&block) end # # Clears the results. # def clear clear_pastes return super end protected # # Retrieves the Paste from the _metadata_ and caches it. # def cache_paste(metadata) @pastes[metadata.name] ||= metadata.paste end # # Clears all cached pastes. # def clear_pastes @pastes.clear end end end