Class: TheCity::Collection
- Inherits:
-
Object
- Object
- TheCity::Collection
- Includes:
- Enumerable
- Defined in:
- lib/the_city/collection.rb
Overview
Utility class for collections with paged responses
Instance Attribute Summary collapse
-
#attrs ⇒ Object
(also: #to_h, #to_hash, #to_hsh)
readonly
Returns the value of attribute attrs.
Class Method Summary collapse
-
.from_response(response, key, klass, client, request_method, path, options) ⇒ TheCity::Collection
Construct a new Collection object from a response hash.
Instance Method Summary collapse
- #[](n) ⇒ Object
- #current_cursor ⇒ Object
- #current_page ⇒ Object
- #each(start = 0, &block) ⇒ Enumerator
- #first? ⇒ Boolean
-
#initialize(attrs, key, klass, client, request_method, path, options) ⇒ TheCity::Collection
constructor
Initializes a new Collection.
- #last ⇒ Object
- #last? ⇒ Boolean
- #last_page? ⇒ Boolean
- #next_cursur ⇒ Object (also: #next)
- #next_page ⇒ Object
- #previous_cursor ⇒ Object (also: #previous)
- #total_entries ⇒ Object (also: #total)
Constructor Details
#initialize(attrs, key, klass, client, request_method, path, options) ⇒ TheCity::Collection
Initializes a new Collection
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/the_city/collection.rb', line 34 def initialize(attrs, key, klass, client, request_method, path, ) @key = key.to_sym @klass = klass @client = client @request_method = request_method.to_sym @path = path = @collection = [] set_attrs(attrs) end |
Instance Attribute Details
#attrs ⇒ Object (readonly) Also known as: to_h, to_hash, to_hsh
Returns the value of attribute attrs.
5 6 7 |
# File 'lib/the_city/collection.rb', line 5 def attrs @attrs end |
Class Method Details
.from_response(response, key, klass, client, request_method, path, options) ⇒ TheCity::Collection
Construct a new Collection object from a response hash
20 21 22 |
# File 'lib/the_city/collection.rb', line 20 def self.from_response(response, key, klass, client, request_method, path, ) new(response[:body], key, klass, client, request_method, path, ) end |
Instance Method Details
#[](n) ⇒ Object
102 103 104 |
# File 'lib/the_city/collection.rb', line 102 def [](n) @collection[n] end |
#current_cursor ⇒ Object
61 62 63 |
# File 'lib/the_city/collection.rb', line 61 def current_cursor @current_cursor end |
#current_page ⇒ Object
85 86 87 |
# File 'lib/the_city/collection.rb', line 85 def current_page @current_page end |
#each(start = 0, &block) ⇒ Enumerator
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/the_city/collection.rb', line 46 def each(start = 0, &block) return to_enum(:each) unless block_given? for element in Array(@collection[start..-1]) yield element @current_cursor += 1 end unless last? start = [@collection.size, start].max fetch_next_page unless last_page? each(start, &block) end @current_cursor = 1 self end |
#first? ⇒ Boolean
76 77 78 |
# File 'lib/the_city/collection.rb', line 76 def first? previous_cursor.zero? end |
#last ⇒ Object
106 107 108 |
# File 'lib/the_city/collection.rb', line 106 def last @collection.last end |
#last? ⇒ Boolean
81 82 83 |
# File 'lib/the_city/collection.rb', line 81 def last? current_cursor >= total_entries end |
#last_page? ⇒ Boolean
93 94 95 |
# File 'lib/the_city/collection.rb', line 93 def last_page? current_page == @total_pages end |
#next_cursur ⇒ Object Also known as: next
65 66 67 |
# File 'lib/the_city/collection.rb', line 65 def next_cursur (current_cursor + 1) || -1 end |
#next_page ⇒ Object
89 90 91 |
# File 'lib/the_city/collection.rb', line 89 def next_page current_page + 1 end |
#previous_cursor ⇒ Object Also known as: previous
70 71 72 |
# File 'lib/the_city/collection.rb', line 70 def previous_cursor current_cursor - 1 end |
#total_entries ⇒ Object Also known as: total
97 98 99 |
# File 'lib/the_city/collection.rb', line 97 def total_entries @total_entries.is_a?(Array) ? @total_entries.first : @total_entries end |