Sha256: b9f33b47f42ddb5541646276bade0277fc5c8f7e100c23d32ee9d5539dc30712

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

module Eco::Data::Locations::NodeBase
  module TagValidations
    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+/

    def clean_id(str, notify: true)
      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?
        if partial != str
          invalid_chars = identify_invalid_characters(str)
          puts "• (Row: #{self.row_num}) Invalid characters _#{invalid_chars}_ (removed): '#{str}' (converted to '#{result}')"
        elsif blanks_x2
          puts "• (Row: #{self.row_num}) Double blanks (removed): '#{str}' (converted to '#{result}')"
        end
        invalid_warned!
      end
    end

    def invalid_warned?
      @invalid_warned ||= false
    end

    def invalid_warned!
      @invalid_warned = true
    end

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

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

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eco-helpers-2.6.0 lib/eco/data/locations/node_base/tag_validations.rb
eco-helpers-2.5.10 lib/eco/data/locations/node_base/tag_validations.rb
eco-helpers-2.5.9 lib/eco/data/locations/node_base/tag_validations.rb