Class: RPaste::ResultSet

Inherits:
Array
  • Object
show all
Defined in:
lib/rpaste/result_set.rb

Instance Method Summary collapse

Constructor Details

#initialize(results = [], &block) ⇒ ResultSet

Creates a new ResultSet object with the given results.


7
8
9
10
11
# File 'lib/rpaste/result_set.rb', line 7

def initialize(results=[],&block)
  @pastes = {}

  super(results,&block)
end

Instance Method Details

#by_author(author) ⇒ Object

Selects metadata with the matching author.

results.by_author('dj food')
results.by_author(/^ro/)

33
34
35
36
37
38
39
# File 'lib/rpaste/result_set.rb', line 33

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

#clearObject

Clears the results.


231
232
233
234
# File 'lib/rpaste/result_set.rb', line 231

def clear
  clear_pastes
  return super
end

#each_by_author(author, &block) ⇒ Object

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/)

59
60
61
# File 'lib/rpaste/result_set.rb', line 59

def each_by_author(author,&block)
  by_author(author).each(&block)
end

#each_paste(&block) ⇒ Object

Iterates over all pastes, passing each to the given block.

results.each_paste do |paste|
  if paste.syntax == 'ASM'
    puts paste
  end
end

94
95
96
# File 'lib/rpaste/result_set.rb', line 94

def each_paste(&block)
  pastes.each(&block)
end

#each_paste_by_author(author, &block) ⇒ Object

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

184
185
186
# File 'lib/rpaste/result_set.rb', line 184

def each_paste_by_author(author,&block)
  pastes_by_author(author).each(&block)
end

#each_paste_on_date(date, &block) ⇒ Object

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

196
197
198
# File 'lib/rpaste/result_set.rb', line 196

def each_paste_on_date(date,&block)
  pastes_on_date(date).each(&block)
end

#each_paste_with_name(name, &block) ⇒ Object

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

172
173
174
# File 'lib/rpaste/result_set.rb', line 172

def each_paste_with_name(name,&block)
  pastes_with_name(name).each(&block)
end

#each_paste_with_syntax(syntax, &block) ⇒ Object

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

208
209
210
# File 'lib/rpaste/result_set.rb', line 208

def each_paste_with_syntax(syntax,&block)
  pastes_with_syntax(syntax).each(&block)
end

#each_paste_with_text(text, &block) ⇒ Object

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

224
225
226
# File 'lib/rpaste/result_set.rb', line 224

def each_paste_with_text(text,&block)
  pastes_with_text(text).each(&block)
end

#each_with_name(name, &block) ⇒ Object

Iterates over the metadata with the matching name, passing each to the given block.

results.each_with_name('secret')
results.each_with_name(/reg/)

48
49
50
# File 'lib/rpaste/result_set.rb', line 48

def each_with_name(name,&block)
  with_name(name).each(&block)
end

#paste(index) ⇒ Object

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)

70
71
72
73
74
75
76
# File 'lib/rpaste/result_set.rb', line 70

def paste(index)
  if index.kind_of?(Range)
    return self[index].map { |data| cache_paste(data) }
  else
    return cache_paste(self[index])
  end
end

#pastesObject

Returns all pastes.


81
82
83
# File 'lib/rpaste/result_set.rb', line 81

def pastes
  self.map { |data| cache_paste(data) }
end

#pastes_by_author(author) ⇒ Object

Returns the pastes which have the matching author.

results.pastes_by_author('adam freeland')
results.pastes_by_author(/hendrix/)

125
126
127
128
129
130
131
# File 'lib/rpaste/result_set.rb', line 125

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

#pastes_on_date(date) ⇒ Object

Returns the pastes which have the matching date.

results.pastes_on_date(Time.now)

138
139
140
# File 'lib/rpaste/result_set.rb', line 138

def pastes_on_date(date)
  pastes_with { |data| data.date == date }
end

#pastes_with(&block) ⇒ Object

Returns the pastes which match the given block.


101
102
103
# File 'lib/rpaste/result_set.rb', line 101

def pastes_with(&block)
  self.select(&block).map { |data| cache_paste(data) }
end

#pastes_with_name(name) ⇒ Object

Returns the pastes which have the matching name.

results.pastes_with_name('config')
results.pastes_with_name(/rock/)

111
112
113
114
115
116
117
# File 'lib/rpaste/result_set.rb', line 111

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

#pastes_with_syntax(syntax) ⇒ Object

Returns the pastes which have the matching syntax.

results.pastes_with_syntax('C')

147
148
149
# File 'lib/rpaste/result_set.rb', line 147

def pastes_with_syntax(syntax)
  pastes_with { |data| data.syntax == syntax }
end

#pastes_with_text(text) ⇒ Object

Returns the pastes which contain the specified text.

results.pastes_with_text('ls')

156
157
158
159
160
161
162
# File 'lib/rpaste/result_set.rb', line 156

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

#with_name(name) ⇒ Object

Selects metadata with the matching name.

results.with_name('secret')
results.with_name(/login/)

19
20
21
22
23
24
25
# File 'lib/rpaste/result_set.rb', line 19

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