Class: BioDSL::AddKey
- Inherits:
-
Object
- Object
- BioDSL::AddKey
- 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 collapse
- STATS =
%i(records_in records_out)
Instance Method Summary collapse
-
#initialize(options) ⇒ Proc
constructor
Constructor for AddKey.
-
#lmb ⇒ Proc
Add a key or replace a key for all records with a specified value or a forthrunning number with a prefix.
Constructor Details
#initialize(options) ⇒ Proc
Constructor for AddKey.
69 70 71 72 73 |
# File 'lib/BioDSL/commands/add_key.rb', line 69 def initialize() @options = end |
Instance Method Details
#lmb ⇒ Proc
Add a key or replace a key for all records with a specified value or a forthrunning number with a prefix.
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 |