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 &_'\/.-"
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
# 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)
  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)


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

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

#identify_invalid_characters(str) ⇒ Object



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

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

#invalid_warnedObject



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

def invalid_warned
  @invalid_warned ||= {}
end

#invalid_warned!(str) ⇒ Object



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

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

#invalid_warned?(str) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/eco/data/locations/node_base/tag_validations.rb', line 31

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

#remove_double_blanks(str) ⇒ Object



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

def remove_double_blanks(str)
  return nil if str.nil?
  str.gsub(DOUBLE_BLANKS, ' ').strip
end

#replace_not_allowed(str) ⇒ Object



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

def replace_not_allowed(str)
  return nil if str.nil?
  return str if str.match(VALID_TAG_REGEX)
  str.gsub(INVALID_TAG_REGEX, ' ')
end