Class: BioDSL::AddKey

Inherits:
Object
  • Object
show all
Defined in:
lib/BioDSL/commands/add_key.rb

Overview

Add a key/value pair to all records in stream.

add_key can be used to add a fixed value to a specified key to all records in the stream, or add a numeric forth running number (zero-based) with a specified prefix.

Usage

add_key(<key: <string>[, value: <string> | prefix: <string>])

Options

  • key: <string> - Key to add or overwrite.

  • value: <string> - Value to use with key.

  • prefix: <string> - Prefix to use with key.

Examples

To add a value to all records in the stream do:

add_key(key: "FOO", value: "BAR")

To add a forth running number to all records in the stream do:

add_key(key: :ID, prefix: "")

Finally, to add a forth running number with a prefix do:

add_key(key: :ID, prefix: "ID_")

Constant Summary

STATS =
%i(records_in records_out)

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Proc

Constructor for AddKey.

Parameters:

  • options (Hash)

    Options hash.

Options Hash (options):

  • :key (Symbol)

    Key to add or replace.

  • :value (String)

    Value to use with :key.

  • :prefix (String)

    Prefix to use with :key.



69
70
71
72
73
# File 'lib/BioDSL/commands/add_key.rb', line 69

def initialize(options)
  @options = options

  check_options
end

Instance Method Details

#lmbProc

Add a key or replace a key for all records with a specified value or a forthrunning number with a prefix.

Parameters:

  • options (Hash)

    Options hash.

Returns:

  • (Proc)

    Returns the command lambda.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/BioDSL/commands/add_key.rb', line 84

def lmb
  lambda do |input, output, status|
    status_init(status, STATS)

    input.each_with_index do |record, i|
      @status[:records_in] += 1

      record[@options[:key].to_sym] = @options[:value] ||
                                      "#{@options[:prefix]}#{i}"

      output << record

      @status[:records_out] += 1
    end
  end
end