class BioDSL::Dump

Dump records in stream to STDOUT.

dump outputs records from the stream to STDOUT.

Usage

dump([first: <uint> |last: <uint>])

Options

Examples

To dump all records in the stream:

dump

To dump only the first 10 records:

dump(first: 10)

To dump only the last 10 records:

dump(last: 10)

Constants

STATS

Public Class Methods

new(options) click to toggle source

Constructor for the Dump class.

@param [Hash] options Options hash. @option options [Integer] :first Dump first number of records. @option options [Integer] :last Dump last number of records.

@return [Dump] Returns an instance of the Dump class.

# File lib/BioDSL/commands/dump.rb, line 65
def initialize(options)
  @options = options

  check_options
end

Public Instance Methods

lmb() click to toggle source

Return a lambda for the dump command.

@return [Proc] Returns the dump command lambda.

# File lib/BioDSL/commands/dump.rb, line 74
def lmb
  lambda do |input, output, status|
    status_init(status, STATS)

    if @options[:first]
      dump_first(input, output)
    elsif @options[:last]
      dump_last(input, output)
    else
      dump_all(input, output)
    end
  end
end

Private Instance Methods

check_options() click to toggle source

Check the options and return a lambda for the command.

# File lib/BioDSL/commands/dump.rb, line 91
def check_options
  options_allowed(@options, :first, :last)
  options_unique(@options, :first, :last)
  options_assert(@options, ':first > 0')
  options_assert(@options, ':last > 0')
end
dump_all(input, output) click to toggle source

Dump all records.

@param input [Enumerator::Yielder] Input stream. @param output [Enumerator::Yielder] Output stream.

# File lib/BioDSL/commands/dump.rb, line 144
def dump_all(input, output)
  input.each do |record|
    @status[:records_in] += 1

    puts record

    if output
      output << record
      @status[:records_out] += 1
    end
  end
end
dump_first(input, output) click to toggle source

Dump the first number of records.

@param input [Enumerator::Yielder] Input stream. @param output [Enumerator::Yielder] Output stream.

# File lib/BioDSL/commands/dump.rb, line 102
def dump_first(input, output)
  input.first(@options[:first]).each do |record|
    @status[:records_in] += 1

    puts record

    if output
      output << record
      @status[:records_out] += 1
    end
  end
end
dump_last(input, output) click to toggle source

Dump the last number of records.

@param input [Enumerator::Yielder] Input stream. @param output [Enumerator::Yielder] Output stream.

# File lib/BioDSL/commands/dump.rb, line 119
def dump_last(input, output)
  buffer = []
  last   = @options[:last]

  input.each do |record|
    @status[:records_in] += 1

    buffer << record
    buffer.shift if buffer.size > last
  end

  buffer.each do |record|
    puts record

    if output
      output << record
      @status[:records_out] += 1
    end
  end
end