Class: Kiba::Extend::Transforms::Count::FieldValues
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::Count::FieldValues
- Defined in:
- lib/kiba/extend/transforms/count/field_values.rb
Overview
Adds the given field(s) to the row with nil value if they do not already exist in row
Examples
Input table is not shown separately. It is just name
column of the results tables shown below. The blank
value in name indicates an empty Ruby String object. The nil indicates a Ruby NilValue object.
Example 1
No placeholder value is given, so “NULL” is treated as a string value. count_empty
defaults to false, so
empty values are not counted.
Used in pipeline as:
transform Count::FieldValues, field: :name, target: :ct, delim: ';'
Results in:
| name | ct |
|----------------------+----|
| Weddy | 1 |
| NULL | 1 |
| | 0 |
| nil | 0 |
| Earlybird;Divebomber | 2 |
| ;Niblet | 1 |
| Hunter; | 1 |
| NULL;Earhart | 2 |
| ; | 0 |
| NULL;NULL | 2 |
Example 2
No placeholder value is given, so “NULL” is treated as a string value. count_empty
is true, so
empty values are counted. Note that fully empty field values are not counted, but when a
delimiter is present, any empty values delimited by it are counted.
These options are functionally equivalent to those shown in example 4.
Used in pipeline as:
transform Count::FieldValues, field: :name, target: :ct, delim: ';', count_empty: true
Results in:
| name | ct |
|----------------------+----|
| Weddy | 1 |
| NULL | 1 |
| | 0 |
| nil | 0 |
| Earlybird;Divebomber | 2 |
| ;Niblet | 2 |
| Hunter; | 2 |
| NULL;Earhart | 2 |
| ; | 2 |
| NULL;NULL | 2 |
Example 3
Placeholder value is given, so “NULL” is treated as an empty value. count_empty
default to false, so empty
values, including any values that equal the given placeholder value, are not counted.
Used in pipeline as:
transform Count::FieldValues, field: :name, target: :ct, delim: ';', placeholder: 'NULL'
Results in:
| name | ct |
|----------------------+----|
| Weddy | 1 |
| NULL | 0 |
| | 0 |
| nil | 0 |
| Earlybird;Divebomber | 2 |
| ;Niblet | 1 |
| Hunter; | 1 |
| NULL;Earhart | 1 |
| ; | 0 |
| NULL;NULL | 0 |
Example 4
Placeholder value is given, so “NULL” is treated as an empty value. count_empty
is true, so
empty values are counted. Note that fully empty field values are not counted, but when a
delimiter is present, any empty values delimited by it are counted.
These options are functionally equivalent to those shown in example 2.
Used in pipeline as:
transform Count::FieldValues, field: :name, target: :ct, delim: ';', placeholder: 'NULL', count_empty: true
Results in:
| name | ct |
|----------------------+----|
| Weddy | 1 |
| NULL | 1 |
| | 0 |
| nil | 0 |
| Earlybird;Divebomber | 2 |
| ;Niblet | 2 |
| Hunter; | 2 |
| NULL;Earhart | 2 |
| ; | 2 |
| NULL;NULL | 2 |
Instance Method Summary collapse
-
#initialize(field:, target:, delim: Kiba::Extend.delim, placeholder: nil, count_empty: false) ⇒ FieldValues
constructor
A new instance of FieldValues.
-
#process(row) ⇒ Object
Constructor Details
#initialize(field:, target:, delim: Kiba::Extend.delim, placeholder: nil, count_empty: false) ⇒ FieldValues
Returns a new instance of FieldValues.
136 137 138 139 140 141 142 143 |
# File 'lib/kiba/extend/transforms/count/field_values.rb', line 136 def initialize(field:, target:, delim: Kiba::Extend.delim, placeholder: nil, count_empty: false) @field = field @target = target @delim = delim @placeholder = placeholder @count_empty = count_empty end |
Instance Method Details
#process(row) ⇒ Object
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/kiba/extend/transforms/count/field_values.rb', line 146 def process(row) row[@target] = "0" val = row.fetch(@field, nil) return row unless val split = val.split(@delim, -1) to_count = handle_empty(split) row[@target] = to_count.length.to_s row end |