Class: DatabasusRuby::Collection

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#dataArray[Object] (readonly)

Returns the value of attribute data.

Returns:



14
15
16
# File 'lib/databasus_ruby/collection.rb', line 14

def data
  @data
end

#limitInteger? (readonly)

Returns the value of attribute limit.

Returns:

  • (Integer, nil)


14
15
16
# File 'lib/databasus_ruby/collection.rb', line 14

def limit
  @limit
end

#offsetInteger? (readonly)

Returns the value of attribute offset.

Returns:

  • (Integer, nil)


14
15
16
# File 'lib/databasus_ruby/collection.rb', line 14

def offset
  @offset
end

#totalInteger (readonly)

Returns the value of attribute total.

Returns:

  • (Integer)


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.

Parameters:

  • body (Object)
  • key: (String)
  • wrap: (^(payload) -> Object, nil) (defaults to: nil)

Returns:



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?

Parameters:

  • index (Integer)

Returns:



56
57
58
# File 'lib/databasus_ruby/collection.rb', line 56

def [](index)
  data[index]
end

#eachCollection #eachEnumerator[Object, Array[Object]]

Overloads:

Yields:

Yield Parameters:

Yield Returns:

  • (void)


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

Returns:

  • (Boolean)


52
53
54
# File 'lib/databasus_ruby/collection.rb', line 52

def empty?
  data.empty?
end

#inspectString

Returns:

  • (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

#lastObject?

Returns:



60
61
62
# File 'lib/databasus_ruby/collection.rb', line 60

def last
  data.last
end

#last_page?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/databasus_ruby/collection.rb', line 77

def last_page?
  next_offset.nil?
end

#next_offsetInteger?

Offset to pass to the next call, or nil when this is the last page (or the endpoint does not paginate at all).

Returns:

  • (Integer, nil)


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

Returns:

  • (Boolean)


64
65
66
# File 'lib/databasus_ruby/collection.rb', line 64

def paginated?
  !limit.nil?
end

#sizeInteger Also known as: length

Returns:

  • (Integer)


47
48
49
# File 'lib/databasus_ruby/collection.rb', line 47

def size
  data.size
end