Class: DatabasusRuby::Collection
- Inherits:
-
Object
- Object
- DatabasusRuby::Collection
- Includes:
- Enumerable, Enumerable[Object]
- Defined in:
- lib/databasus_ruby/collection.rb,
sig/databasus_ruby.rbs
Overview
Uniform return value for every list endpoint.
Databasus answers lists in three different shapes: a bare array (GET /databases), a paginated envelope (GET /backups) and an unpaginated envelope (GET /workspaces). Collection flattens all three into an Enumerable that always answers #total, and answers #limit / #offset when the endpoint actually paginates.
Instance Attribute Summary collapse
-
#data ⇒ Array[Object]
readonly
Returns the value of attribute data.
-
#limit ⇒ Integer?
readonly
Returns the value of attribute limit.
-
#offset ⇒ Integer?
readonly
Returns the value of attribute offset.
-
#total ⇒ Integer
readonly
Returns the value of attribute total.
Class Method Summary collapse
-
.from(body, key:, wrap: nil) ⇒ Collection
keynames the array inside an envelope response and is ignored for bare-array responses.
Instance Method Summary collapse
- #[](index) ⇒ Object?
- #each {|arg0| ... } ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(data: [], total: nil, limit: nil, offset: nil, wrap: nil) ⇒ Collection
constructor
wrapbuilds one entry from its raw hash; it defaults to a plain Object and endpoints pass a Record builder instead. - #inspect ⇒ String
- #last ⇒ Object?
- #last_page? ⇒ Boolean
-
#next_offset ⇒ Integer?
Offset to pass to the next call, or nil when this is the last page (or the endpoint does not paginate at all).
- #paginated? ⇒ Boolean
- #size ⇒ Integer (also: #length)
Constructor Details
#initialize(data: [], total: nil, limit: nil, offset: nil, wrap: nil) ⇒ Collection
wrap builds one entry from its raw hash; it defaults to a plain Object
and endpoints pass a Record builder instead.
18 19 20 21 22 23 |
# File 'lib/databasus_ruby/collection.rb', line 18 def initialize(data: [], total: nil, limit: nil, offset: nil, wrap: nil) @data = data.map { |item| item.is_a?(Object) ? item : (wrap || Object.method(:new)).call(item) } @total = total || @data.size @limit = limit @offset = offset end |
Instance Attribute Details
#data ⇒ Array[Object] (readonly)
Returns the value of attribute data.
14 15 16 |
# File 'lib/databasus_ruby/collection.rb', line 14 def data @data end |
#limit ⇒ Integer? (readonly)
Returns the value of attribute limit.
14 15 16 |
# File 'lib/databasus_ruby/collection.rb', line 14 def limit @limit end |
#offset ⇒ Integer? (readonly)
Returns the value of attribute offset.
14 15 16 |
# File 'lib/databasus_ruby/collection.rb', line 14 def offset @offset end |
#total ⇒ Integer (readonly)
Returns the value of attribute total.
14 15 16 |
# File 'lib/databasus_ruby/collection.rb', line 14 def total @total end |
Class Method Details
.from(body, key:, wrap: nil) ⇒ Collection
key names the array inside an envelope response and is ignored for
bare-array responses.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/databasus_ruby/collection.rb', line 27 def self.from(body, key:, wrap: nil) case body when ::Array new(data: body, wrap: wrap) when Hash new(data: body[key] || [], total: body["total"], limit: body["limit"], offset: body["offset"], wrap: wrap) when nil new else raise Error, "unexpected list payload: #{body.class}" end end |
Instance Method Details
#[](index) ⇒ Object?
56 57 58 |
# File 'lib/databasus_ruby/collection.rb', line 56 def [](index) data[index] end |
#each ⇒ Collection #each ⇒ Enumerator[Object, Array[Object]]
40 41 42 43 44 45 |
# File 'lib/databasus_ruby/collection.rb', line 40 def each(&block) return data.each unless block data.each(&block) self end |
#empty? ⇒ Boolean
52 53 54 |
# File 'lib/databasus_ruby/collection.rb', line 52 def empty? data.empty? end |
#inspect ⇒ String
81 82 83 |
# File 'lib/databasus_ruby/collection.rb', line 81 def inspect "#<#{self.class.name} size=#{size} total=#{total} limit=#{limit.inspect} offset=#{offset.inspect}>" end |
#last_page? ⇒ Boolean
77 78 79 |
# File 'lib/databasus_ruby/collection.rb', line 77 def last_page? next_offset.nil? end |
#next_offset ⇒ Integer?
Offset to pass to the next call, or nil when this is the last page (or the endpoint does not paginate at all).
70 71 72 73 74 75 |
# File 'lib/databasus_ruby/collection.rb', line 70 def next_offset return nil unless paginated? following = (offset || 0) + data.size following < total ? following : nil end |
#paginated? ⇒ Boolean
64 65 66 |
# File 'lib/databasus_ruby/collection.rb', line 64 def paginated? !limit.nil? end |
#size ⇒ Integer Also known as: length
47 48 49 |
# File 'lib/databasus_ruby/collection.rb', line 47 def size data.size end |