Class: BioDSL::MergeValues

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

Overview

Merge values of specified keys.

merge_values merges the values of a list of keys using a given delimiter and saves the new value as the value of the first key.

Usage

merge_values(<keys: <list>>[, delimiter: <string>])

Options

  • keys: <list> - List of keys to merge.

  • delimiter: <string> - Delimiter (default='_').

Examples

Consider the following record:

{ID: "FOO", COUNT: 10, SEQ: "gataag"}

To merge the values so that the COUNT and ID is merged in that order do:

merge_values(keys: [:COUNT, :ID])

{:ID=>"FOO", :COUNT=>"10_FOO", :SEQ=>"gataag"}

Changing the delimiter and order:

merge_values(keys: [:ID, :COUNT], delimiter: ':count=')

{:ID=>"FOO:count=10", :COUNT=>10, :SEQ=>"gataag"}

Constant Summary

STATS =
%i(records_in records_out)

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ MergeValues

Constructor for MergeValues.

Parameters:

  • options (Hash)

    Options hash.

Options Hash (options):

  • :keys (Array)

    Keys whos values to merge.

  • :delimiter (String)

    Delimiter for joining.



70
71
72
73
74
75
76
77
# File 'lib/BioDSL/commands/merge_values.rb', line 70

def initialize(options)
  @options = options
  check_options
  defaults

  @keys      = options[:keys]
  @delimiter = options[:delimiter]
end

Instance Method Details

#lmbProc

Return command lambda for merge_values.

Returns:

  • (Proc)

    Command lambda.



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

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

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

      if @keys.all? { |key| record.key? key }
        values = @keys.inject([]) { |a, e| a << record[e.to_sym] }
        record[@keys.first] = values.join(@delimiter)
      end

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