Class: Kiba::Extend::Transforms::Compare::FieldValues

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/compare/field_values.rb

Overview

Compares values in the given fields and puts same or diff in the given target field.

Examples

Input table:

| id  | pid | zid |
|-----+-----+-----|
| a   | a   | a   |
| A   | a   | a   |
| a   |  a  |  a  |
|     | a   | a   |
| nil | a   | a   |
|     | nil |     |

The values in the third row are id = ‘a ‘, pid = ‘ a’, zid = ‘ a ‘.

Used in pipeline as:

 transform Compare::FieldValues, fields: %i[id pid zid], target: :comp

Results in:

| id  | pid | zid | comp |
|-----+-----+-----+------|
| a   | a   | a   | same |
| A   | a   | a   | same |
| a   |  a  |  a  | same |
|     | a   | a   | diff |
| nil | a   | a   | diff |
|     | nil |     | same |

Used in pipeline as:

 transform Compare::FieldValues, fields: %i[id pid zid], target: :comp, downcase: false, strip: false

Results in:

| id  | pid | zid | comp |
|-----+-----+-----+------|
| a   | a   | a   | same |
| A   | a   | a   | diff |
| a   |  a  |  a  | diff |
|     | a   | a   | diff |
| nil | a   | a   | diff |
|     | nil |     | same |

Used in pipeline as:

 transform Compare::FieldValues, fields: %i[id pid zid], target: :comp, ignore_blank: true

Results in:

| id  | pid | zid | comp |
|-----+-----+-----+------|
| a   | a   | a   | same |
| A   | a   | a   | same |
| a   |  a  |  a  | same |
|     | a   | a   | same |
| nil | a   | a   | same |
|     | nil |     | same |

Since:

  • 2.7.1.62

Instance Method Summary collapse

Constructor Details

#initialize(fields:, target:, downcase: true, strip: true, ignore_blank: false) ⇒ FieldValues

Returns a new instance of FieldValues.

Parameters:

  • fields (Array<Symbol>)

    names of fields whose values will be compared

  • target (Symbol)

    new field in which to record comparison result

  • downcase (Boolean) (defaults to: true)

    whether to downcase all values for comparison. false results in a case sensitive comparison. true results in a case insensitive comparison.

  • strip (Boolean) (defaults to: true)

    whether to remove leading/trailing spaces prior to comparison

  • ignore_blank (Boolean) (defaults to: false)

    true drops empty or nil values from the comparison

Since:

  • 2.7.1.62



93
94
95
96
97
98
99
100
# File 'lib/kiba/extend/transforms/compare/field_values.rb', line 93

def initialize(fields:, target:, downcase: true, strip: true,
  ignore_blank: false)
  @fields = [fields].flatten
  @target = target
  @strip = strip
  @downcase = downcase
  @ignore_blank = ignore_blank
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

  • row (Hash{ Symbol => String, nil })

Since:

  • 2.7.1.62



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/kiba/extend/transforms/compare/field_values.rb', line 103

def process(row)
  row[@target] = "diff"
  values = []
  @fields.each do |field|
    value = row.fetch(field, "").dup
    value = "" if value.nil?
    value = value.downcase if @downcase
    value = value.strip if @strip
    values << value
  end
  values.reject!(&:blank?) if @ignore_blank
  row[@target] = "same" if values.uniq.length == 1 || values.empty?
  row
end