Class: MyTankInfo::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/my_tank_info/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, filter_attribute: nil, filter_value: nil) ⇒ Collection

Returns a new instance of Collection.



22
23
24
25
26
27
28
29
30
31
# File 'lib/my_tank_info/collection.rb', line 22

def initialize(data:, filter_attribute: nil, filter_value: nil)
  @data =
    if filter_attribute && filter_value
      data.select { |item| item.send(filter_attribute) == filter_value }
    else
      data
    end

  @size = @data.size
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/my_tank_info/collection.rb', line 5

def data
  @data
end

#sizeObject (readonly)

Returns the value of attribute size.



5
6
7
# File 'lib/my_tank_info/collection.rb', line 5

def size
  @size
end

Class Method Details

.from_response(response, type:, filter_attribute: nil, filter_value: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/my_tank_info/collection.rb', line 7

def self.from_response(response, type:, filter_attribute: nil, filter_value: nil)
  body =
    if response.body.instance_of?(Hash)
      [response.body]
    else
      response.body
    end

  @collection = new(
    data: body.map { |attrs| type.new(attrs) },
    filter_attribute: filter_attribute,
    filter_value: filter_value
  )
end