Class: TFClient::Models::Planets

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:) ⇒ Planets

Returns a new instance of Planets.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/textflight-client/models/nav.rb', line 208

def initialize(lines:)
  line, index = ResponseParser.line_and_index_for_beginning_with(lines: lines,
                                                                 string: "Planets")
  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
    type = hash[:planet_type]
    name = hash[:name]
    faction = hash[:faction]

    { index: index, type: type, name: name, faction: faction }
  end
end

Instance Method Details

#to_sObject



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/textflight-client/models/nav.rb', line 228

def to_s
  return "" if @items.empty?
  table = TTY::Table.new(header: [
    {value: "#{@translation}", alignment: :right},
    {value: "type", alignment: :center},
    {value: "name", alignment: :center},
    {value: "faction", alignment: :center},
    {value: "index", alignment: :center}
  ])

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

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