Class: TFClient::Models::Outfits

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

Instance Attribute Summary collapse

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

Returns a new instance of Outfits.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/textflight-client/models/scan.rb', line 122

def initialize(lines:)
  line, index = ResponseParser.line_and_index_for_beginning_with(lines: lines,
                                                                 string: "Outfits")
  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]
    mark = hash[:mark].to_i
    setting = hash[:setting].to_i
    { index: index, name: name, mark: mark, setting: setting}
  end
  @max_slots = 0
end

Instance Attribute Details

#max_slotsObject

Returns the value of attribute max_slots.



120
121
122
# File 'lib/textflight-client/models/scan.rb', line 120

def max_slots
  @max_slots
end

Instance Method Details

#slots_usedObject



164
165
166
# File 'lib/textflight-client/models/scan.rb', line 164

def slots_used
  @items.map { |hash| hash[:mark] }.sum
end

#to_sObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/textflight-client/models/scan.rb', line 142

def to_s
  table = TTY::Table.new(header: [
    "#{@translation}: #{slots_used}/#{@max_slots} slots",
    {value: "name", alignment: :center},
    {value: "setting", alignment: :center},
    {value: "index", alignment: :center}
  ])

  @items.each do |item|
    table << [
      "[#{item[:index]}]",
      "#{item[:name]} (#{item[:mark].to_roman})",
      item[:setting],
      "[#{item[:index]}]"
    ]
  end

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