Class: Kiba::Extend::Transforms::Delete::DelimiterOnlyFieldValues
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::Delete::DelimiterOnlyFieldValues
- Includes:
- Allable
- Defined in:
- lib/kiba/extend/transforms/delete/delimiter_only_field_values.rb
Overview
Converts any value in given field(s) to nil if that value is empty?
, or consists only
of the specified delimiter, treat_as_null
value(s), and, optionally, spaces.
See also Helpers::DelimOnlyChecker, which is used by this transform.
Examples
Default
Deletes delimiter-only field values from all fields. No strings except ''
(empty string) are
treated as null. This assumes your project has set Kiba::Extend.delim
to equal |
.
Source data:
[
{foo: 'a| b', bar: ' | ', baz: ''},
{foo: nil, bar: '|', baz: ' |b'},
{foo: %NULLVALUE%, bar: "%NULLVALUE%|%NULLVALUE%", baz: "%NULLVALUE%| %NULLVALUE%"},
{foo: 'NULL', bar: "NULL |%NULLVALUE%", baz: "NULL| NULL"},
]
Setup:
transform Delete::DelimiterOnlyFieldValues
Result:
[
{foo: 'a| b', bar: nil, baz: nil},
{foo: nil, bar: nil, baz: ' |b'},
{foo: %NULLVALUE%, bar: "%NULLVALUE%|%NULLVALUE%", baz: "%NULLVALUE%| %NULLVALUE%"},
{foo: 'NULL', bar: "NULL |%NULLVALUE%", baz: "NULL| NULL"},
]
With non-default delim
and treat_as_null
parameters
NOTE: The replacements done to determine whether or not a value is delimiter-only are not applied to the remaining field values. Otherwise, we’d see the
%NULLVALUE%
s in the results transformed to%VALUE%
.
Source data:
[
{foo: 'a; b', bar: ' ; ', baz: ''},
{foo: nil, bar: ';', baz: ' ;b'},
{foo: %NULLVALUE%, bar: "%NULLVALUE%;%NULLVALUE%", baz: "%NULLVALUE%; %NULLVALUE%"},
{foo: 'NULL', bar: "NULL ;%NULLVALUE%", baz: "NULL; NULL"},
]
Setup:
transform Delete::DelimiterOnlyFieldValues, delim: ';', treat_as_null: 'NULL'
Result:
[
{foo: 'a; b', bar: nil, baz: nil},
{foo: nil, bar: nil, baz: ' ;b'},
{foo: %NULLVALUE%, bar: "%NULLVALUE%;%NULLVALUE%", baz: "%NULLVALUE%; %NULLVALUE%"},
{foo: nil, bar: " ;%NULLVALUE%", baz: nil},
]
With Array of treat_as_null
values
Source data:
[
{foo: 'a| b', bar: ' | ', baz: ''},
{foo: nil, bar: '|', baz: ' |b'},
{foo: %NULLVALUE%, bar: "%NULLVALUE%|%NULLVALUE%", baz: "%NULLVALUE%| %NULLVALUE%"},
{foo: '%NULL%', bar: "%NULL% |%NULLVALUE%", baz: "%NULL%| %NULL%"},
]
Setup:
transform Delete::DelimiterOnlyFieldValues, treat_as_null: ['%NULL%', '%NULLVALUE%']
Result:
[
{foo: 'a| b', bar: nil, baz: nil},
{foo: nil, bar: nil, baz: ' |b'},
{foo: nil, bar: nil, baz: nil},
{foo: nil, bar: nil, baz: nil}
]
Instance Method Summary collapse
-
#initialize(fields: :all, delim: Kiba::Extend.delim, treat_as_null: nil) ⇒ DelimiterOnlyFieldValues
constructor
A new instance of DelimiterOnlyFieldValues.
-
#process(row) ⇒ Object
Constructor Details
#initialize(fields: :all, delim: Kiba::Extend.delim, treat_as_null: nil) ⇒ DelimiterOnlyFieldValues
Returns a new instance of DelimiterOnlyFieldValues.
122 123 124 125 126 127 128 129 |
# File 'lib/kiba/extend/transforms/delete/delimiter_only_field_values.rb', line 122 def initialize(fields: :all, delim: Kiba::Extend.delim, treat_as_null: nil) @fields = [fields].flatten @delim = delim @treat_as_null = treat_as_null @checker = Helpers::DelimOnlyChecker.new(delim: delim, treat_as_null: treat_as_null) end |
Instance Method Details
#process(row) ⇒ Object
132 133 134 135 136 |
# File 'lib/kiba/extend/transforms/delete/delimiter_only_field_values.rb', line 132 def process(row) finalize_fields(row) unless fields_set process_fields(row) row end |