Sha256: 4e5f485bf2ab85d7ec87bb83b33b4324d90f5b6e13da1334169e3d73e153113a

Contents?: true

Size: 777 Bytes

Versions: 5

Compression:

Stored size: 777 Bytes

Contents

# frozen_string_literal: true

module MyTankInfo
  class Collection
    attr_reader :data, :size

    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

    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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
my_tank_info-1.1.1 lib/my_tank_info/collection.rb
my_tank_info-1.1.0 lib/my_tank_info/collection.rb
my_tank_info-1.0.2 lib/my_tank_info/collection.rb
my_tank_info-1.0.1 lib/my_tank_info/collection.rb
my_tank_info-1.0.0 lib/my_tank_info/collection.rb