class TFClient::Models::Planets

Public Class Methods

new(lines:) click to toggle source
Calls superclass method TFClient::Models::Model::new
# 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

Public Instance Methods

to_s() click to toggle source
# 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