Class: TFClient::Models::Cargo

Inherits:
ModelWithItems show all
Defined in:
lib/textflight-client/models/scan.rb

Instance Attribute Summary

Attributes inherited from ModelWithItems

#items

Attributes inherited from Model

#label, #translation, #values_hash

Instance Method Summary collapse

Methods inherited from ModelWithItems

#count, #items_to_s, #lines_offset, #response_str

Constructor Details

#initialize(lines:) ⇒ Cargo

Returns a new instance of Cargo.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/textflight-client/models/scan.rb', line 171

def initialize(lines:)
  line, index = ResponseParser.line_and_index_for_beginning_with(lines: lines,
                                                                 string: "Cargo")
  super(line: line)

  items = ResponseParser.collect_list_items(lines: lines, start_index: index + 1)
  @items = items.map do |item|
    line = item.strip

    hash = ResponseParser.hash_from_line_values(line: line)

    index = hash[:index].to_i
    name = hash[:name]
    count = hash[:count].to_i
    # TODO: this must be the mark?
    mark = hash[:extra].to_i
    { index: index, name: name, count: count, mark: mark}
  end
end

Instance Method Details

#to_sObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/textflight-client/models/scan.rb', line 191

def to_s
  table = TTY::Table.new(header: [
    "Weight: #{weight}",
    {value: "cargo", alignment: :center},
    {value: "amount", alignment: :center},
    {value: "index", alignment: :center}
  ])

  @items.each do |item|
    name = item[:name]
    mark = item[:mark].to_i
    if mark && (mark != 0)
      name = "#{name} (#{mark.to_roman})"
    end
    table << ["[#{item[:index]}]",
              name,
              item[:count],
              "[#{item[:index]}]"]
  end

  table.render(:ascii, Models::TABLE_OPTIONS) do |renderer|
    renderer.alignments= [:right, :right, :center, :center]
  end
end

#weightObject

TODO: only some items in the inventory contribute to weight



217
218
219
# File 'lib/textflight-client/models/scan.rb', line 217

def weight
  @items.map { |hash| hash[:count] }.sum
end