Class: Kiba::Extend::Transforms::Compare::FieldValues
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::Compare::FieldValues
- 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 |
Instance Method Summary collapse
-
#initialize(fields:, target:, downcase: true, strip: true, ignore_blank: false) ⇒ FieldValues
constructor
A new instance of FieldValues.
-
#process(row) ⇒ Object
Constructor Details
#initialize(fields:, target:, downcase: true, strip: true, ignore_blank: false) ⇒ FieldValues
Returns a new instance of FieldValues.
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
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 |