Module: Eco::Data::Locations::NodeBase::TagValidations

Includes:
Language::AuxiliarLogger
Included in:
Eco::Data::Locations::NodeBase
Defined in:
lib/eco/data/locations/node_base/tag_validations.rb

Constant Summary collapse

ALLOWED_CHARACTERS =
"A-Za-z0-9 &_'/.-".freeze
VALID_TAG_REGEX =
/^[#{ALLOWED_CHARACTERS}]+$/
INVALID_TAG_REGEX =
/[^#{ALLOWED_CHARACTERS}]+/
VALID_TAG_CHARS =
/[#{ALLOWED_CHARACTERS}]+/
DOUBLE_BLANKS =
/\s\s+/

Instance Attribute Summary

Attributes included from Language::AuxiliarLogger

#logger

Instance Method Summary collapse

Methods included from Language::AuxiliarLogger

#log

Instance Method Details

#clean_id(str, notify: true, ref: '') ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/eco/data/locations/node_base/tag_validations.rb', line 11

def clean_id(str, notify: true, ref: '')
  blanks_x2 = has_double_blanks?(str) # dubocop:disable Naming/VariableNumber

  partial   = replace_not_allowed(str)

  remove_double_blanks(partial).tap do |result|
    next unless notify
    next if invalid_warned?(str)

    if partial != str
      invalid_chars = identify_invalid_characters(str)
      log(:warn) {
        "* #{ref}Invalid characters _#{invalid_chars}_ <<_removed_: '#{str}' :_converted_>> '#{result}'"
      }
    elsif blanks_x2
      log(:warn) {
        "* #{ref}Double blanks removed: '#{str}' :_converted_>> '#{result}'"
      }
    end

    invalid_warned!(str)
  end
end

#has_double_blanks?(str) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/eco/data/locations/node_base/tag_validations.rb', line 46

def has_double_blanks?(str)
  return false if str.nil?
  str.match(DOUBLE_BLANKS)
end

#identify_invalid_characters(str) ⇒ Object



64
65
66
# File 'lib/eco/data/locations/node_base/tag_validations.rb', line 64

def identify_invalid_characters(str)
  str.gsub(VALID_TAG_CHARS, '')
end

#invalid_warnedObject



42
43
44
# File 'lib/eco/data/locations/node_base/tag_validations.rb', line 42

def invalid_warned
  @invalid_warned ||= {}
end

#invalid_warned!(str) ⇒ Object



38
39
40
# File 'lib/eco/data/locations/node_base/tag_validations.rb', line 38

def invalid_warned!(str)
  invalid_warned[str] = true
end

#invalid_warned?(str) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/eco/data/locations/node_base/tag_validations.rb', line 34

def invalid_warned?(str)
  invalid_warned[str] ||= false
end

#remove_double_blanks(str) ⇒ Object



51
52
53
54
55
# File 'lib/eco/data/locations/node_base/tag_validations.rb', line 51

def remove_double_blanks(str)
  return if str.nil?

  str.gsub(DOUBLE_BLANKS, ' ').strip
end

#replace_not_allowed(str) ⇒ Object



57
58
59
60
61
62
# File 'lib/eco/data/locations/node_base/tag_validations.rb', line 57

def replace_not_allowed(str)
  return     if str.nil?
  return str if str.match(VALID_TAG_REGEX)

  str.gsub(INVALID_TAG_REGEX, ' ')
end