Class: UserVoice::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/uservoice/collection.rb

Constant Summary collapse

PER_PAGE =
100

Instance Method Summary collapse

Constructor Details

#initialize(client, query, opts = {}) ⇒ Collection

Returns a new instance of Collection.



5
6
7
8
9
10
11
# File 'lib/uservoice/collection.rb', line 5

def initialize(client, query, opts={})
  @client = client
  @query = query
  @limit = opts[:limit] || 2**60
  @per_page = [@limit, PER_PAGE].min
  @pages = {}
end

Instance Method Details

#[](i) ⇒ Object



50
51
52
# File 'lib/uservoice/collection.rb', line 50

def [](i)
  load_page((i/PER_PAGE.to_f).floor + 1)[i%PER_PAGE] if (0..@limit-1).include?(i)
end

#eachObject



39
40
41
42
43
44
# File 'lib/uservoice/collection.rb', line 39

def each
  map do |value|
    yield value
    value
  end
end

#firstObject



13
14
15
# File 'lib/uservoice/collection.rb', line 13

def first
  self[0]
end

#lastObject



17
18
19
# File 'lib/uservoice/collection.rb', line 17

def last
  self[size() - 1]
end

#mapObject Also known as: collect



28
29
30
31
32
33
34
35
36
# File 'lib/uservoice/collection.rb', line 28

def map
  index = 0
  records = []
  while record = self[index]
    records.push(yield record)
    index += 1
  end
  return records
end

#sizeObject



21
22
23
24
25
26
# File 'lib/uservoice/collection.rb', line 21

def size
  if @response_data.nil?
    self[0]
  end
  [@response_data['total_records'], @limit].min
end

#to_aObject



46
47
48
# File 'lib/uservoice/collection.rb', line 46

def to_a
  each {}
end