Class: Kiba::Extend::Transforms::Helpers::DelimOnlyChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/helpers/delim_only_checker.rb

Overview

Service object to return whether given values are “delimiter only”.

NOTE: By default, “delimiter only” is true if a value is nil or empty?. It is also true if the value consists of only the given delim and, optionally, any spaces. If given one or more treat_as_null values, it is true if the value consists of those “null” strings, the given delimiter, and spaces only. If you specify blank_result: false, then values that are nil or empty? will not count as “delimiter only”

Since:

  • 2.9.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delim: Kiba::Extend.delim, treat_as_null: nil, blank_result: true) ⇒ DelimOnlyChecker

Returns a new instance of DelimOnlyChecker.

Parameters:

  • delim (String) (defaults to: Kiba::Extend.delim)
  • treat_as_null (nil, String, Array(String)) (defaults to: nil)

    value(s) to treat as though they are null

  • blank_result (Boolean) (defaults to: true)

    what to return for values that are nil? or empty?

Since:

  • 2.9.0



29
30
31
32
33
34
35
36
# File 'lib/kiba/extend/transforms/helpers/delim_only_checker.rb', line 29

def initialize(delim: Kiba::Extend.delim, treat_as_null: nil,
  blank_result: true)
  @delim = delim
  @nullvals = treat_as_null.nil? ? nil : [treat_as_null].flatten.sort_by do |val|
                                           val.length
                                         end.reverse
  @blank_result = blank_result
end

Class Method Details

.call(delim:, value:, treat_as_null:, blank_result:) ⇒ Object

Since:

  • 2.9.0



20
21
22
23
# File 'lib/kiba/extend/transforms/helpers/delim_only_checker.rb', line 20

def call(delim:, value:, treat_as_null:, blank_result:)
  new(delim: delim, treat_as_null: treat_as_null,
    blank_result: blank_result).call(value)
end

Instance Method Details

#call(value) ⇒ true, false

Parameters:

  • value (String)

Returns:

  • (true)

    if value is delimiter-only, nil, or empty

  • (false)

    otherwise

Since:

  • 2.9.0



41
42
43
44
45
46
47
48
49
# File 'lib/kiba/extend/transforms/helpers/delim_only_checker.rb', line 41

def call(value)
  return blank_result if value.nil?

  no_nulls = nullvals ? remove_nulls(value) : value.strip
  return blank_result if no_nulls.empty?

  no_delims = no_nulls.gsub(delim, "").strip
  no_delims.empty?
end