Class: TFClient::Models::Structures

Inherits:
ModelWithItems show all
Defined in:
lib/textflight-client/models/nav.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:) ⇒ Structures

Returns a new instance of Structures.



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/textflight-client/models/nav.rb', line 256

def initialize(lines:)
    line, index = ResponseParser.line_and_index_for_beginning_with(lines: lines,
                                                                   string: "Structures")
  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)

    id = hash[:id].to_i
    name = hash[:name]
    # TODO: distinguish shipyards from bases
    type = hash[:sclass] || "base"
    { id: id, name: name, type: type }
  end
end

Instance Method Details

#to_sObject



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/textflight-client/models/nav.rb', line 275

def to_s
  table = TTY::Table.new(header: [
    {value: "#{@translation}", alignment: :right},
    {value: "name", alignment: :center},
    {value: "ship class", alignment: :center},
    {value: "id", alignment: :center }
  ])

  @items.each do |item|
    table << ["[#{item[:id]}]", item[:name], item[:type], "[#{item[:id]}]"]
  end

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