Class: Aspera::Cli::Result::ObjectList

Inherits:
Result
  • Object
show all
Defined in:
lib/aspera/cli/result.rb

Overview

Note:

The total parameter is used to display pagination information (e.g., "Items: 10/100")

Object list result (Array of Hash)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, fields: nil, total: nil) ⇒ ObjectList

Returns a new instance of ObjectList.

Parameters:

  • data (Array<Hash>) —

    Array of hash objects to display

  • fields (Array<String>, Proc, nil) (defaults to: nil) —

    Fields to display in table/csv format

  • total (Integer, nil) (defaults to: nil) —

    Total number of items available (for pagination display)



268
269
270
271
272
273
# File 'lib/aspera/cli/result.rb', line 268

def initialize(data, fields: nil, total: nil)
  Aspera.assert_array_all(data, Hash, type: ArgumentError) { 'object list result data' }
  Aspera.assert_type(total, Integer, NilClass) { 'total' }
  super(data: data, fields: fields)
  @total = total
end

Instance Attribute Details

#total ⇒ Object (readonly)

Returns the value of attribute total.



263
264
265
# File 'lib/aspera/cli/result.rb', line 263

def total
  @total
end

Instance Method Details

#format(formatter) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/aspera/cli/result.rb', line 275

def format(formatter)
  formatter.display_item_count(@data.length, @total)
  case formatter.format_type
  when :image
    # Extract single field for image display
    Aspera.assert(@data.length == 1, 'image display requires a single result', type: Cli::BadArgument)
    SingleObject.new(@data.first).format(formatter)
  when :table, :csv
    Aspera.assert_array_all(@data, Hash) { 'result' }
    data = formatter.flat_hash? ? @data.map { |obj| DotContainer.new(obj).to_dotted } : @data
    formatter.display_table(data, formatter.compute_fields(data, @fields), single: false)
  else
    super
  end
end