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)

Raises:

  • (ArgumentError)


236
237
238
239
240
241
242
# File 'lib/aspera/cli/result.rb', line 236

def initialize(data, fields: nil, total: nil)
  Aspera.assert_type(data, Array){'object list result data'}
  raise ArgumentError, 'Object list result requires Array of Hash' unless data.all?(Hash)
  Aspera.assert_type(total, Integer, NilClass){'total'}
  super(data: data, fields: fields)
  @total = total
end

Instance Attribute Details

#totalObject (readonly)

Returns the value of attribute total.



231
232
233
# File 'lib/aspera/cli/result.rb', line 231

def total
  @total
end

Instance Method Details

#format(formatter) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/aspera/cli/result.rb', line 244

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