Class: UserVoice::Collection
- Inherits:
-
Object
- Object
- UserVoice::Collection
- Defined in:
- lib/uservoice/collection.rb
Constant Summary collapse
- PER_PAGE =
100
Instance Method Summary collapse
- #[](i) ⇒ Object
- #each ⇒ Object
- #first ⇒ Object
-
#initialize(client, query, opts = {}) ⇒ Collection
constructor
A new instance of Collection.
- #last ⇒ Object
- #map ⇒ Object (also: #collect)
- #size ⇒ Object
- #to_a ⇒ Object
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 |
#each ⇒ Object
39 40 41 42 43 44 |
# File 'lib/uservoice/collection.rb', line 39 def each map do |value| yield value value end end |
#first ⇒ Object
13 14 15 |
# File 'lib/uservoice/collection.rb', line 13 def first self[0] end |
#last ⇒ Object
17 18 19 |
# File 'lib/uservoice/collection.rb', line 17 def last self[size() - 1] end |
#map ⇒ Object 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 |
#size ⇒ Object
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_a ⇒ Object
46 47 48 |
# File 'lib/uservoice/collection.rb', line 46 def to_a each {} end |