Class: Yandex360::Collection
- Inherits:
-
Object
- Object
- Yandex360::Collection
- Includes:
- Enumerable
- Defined in:
- lib/yandex360/collection.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Class Method Summary collapse
Instance Method Summary collapse
- #[](index) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #first(idx = nil) ⇒ Object
-
#initialize(data:, items:, total:) ⇒ Collection
constructor
A new instance of Collection.
- #last(idx = nil) ⇒ Object
- #size ⇒ Object (also: #length, #count)
Constructor Details
#initialize(data:, items:, total:) ⇒ Collection
Returns a new instance of Collection.
18 19 20 21 22 |
# File 'lib/yandex360/collection.rb', line 18 def initialize(data:, items:, total:) @data = data @items = items @total = total end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
7 8 9 |
# File 'lib/yandex360/collection.rb', line 7 def data @data end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
7 8 9 |
# File 'lib/yandex360/collection.rb', line 7 def items @items end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
7 8 9 |
# File 'lib/yandex360/collection.rb', line 7 def total @total end |
Class Method Details
.from_response(response, key:, type:) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/yandex360/collection.rb', line 9 def self.from_response(response, key:, type:) body = response.body new( data: body[key]&.map {|attrs| type.new(attrs) } || [], items: body["items"] || 0, total: body["total"] || 0 ) end |
Instance Method Details
#[](index) ⇒ Object
46 47 48 |
# File 'lib/yandex360/collection.rb', line 46 def [](index) data[index] end |
#each(&block) ⇒ Object
24 25 26 |
# File 'lib/yandex360/collection.rb', line 24 def each(&block) data.each(&block) end |
#empty? ⇒ Boolean
34 35 36 |
# File 'lib/yandex360/collection.rb', line 34 def empty? data.empty? end |
#first(idx = nil) ⇒ Object
38 39 40 |
# File 'lib/yandex360/collection.rb', line 38 def first(idx=nil) n ? data.first(idx) : data.first end |
#last(idx = nil) ⇒ Object
42 43 44 |
# File 'lib/yandex360/collection.rb', line 42 def last(idx=nil) n ? data.last(idx) : data.last end |
#size ⇒ Object Also known as: length, count
28 29 30 |
# File 'lib/yandex360/collection.rb', line 28 def size data.size end |