Sha256: 7494ca2c391a364c86efeebc725c529905bf47bee1b0f1c8cd240223ebf2514a

Contents?: true

Size: 1.61 KB

Versions: 8

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaPlugins
    module Metadata
      class MetaAttribute
        class CheckPath
          FORMAT_ONE_CHAR = /\A[a-zA-Z0-9]\z/
          FORMAT_MANY_CHARS = /\A[a-zA-Z0-9][a-zA-Z0-9_-]*?[a-zA-Z0-9]\z/ # allow '-' and '_' in the middle

          private_constant :FORMAT_ONE_CHAR, :FORMAT_MANY_CHARS

          class << self
            #
            # Checks allowed characters in specified metadata path parts.
            # Globally allowed characters: "a-z", "A-Z", "0-9".
            # Minus and low line "-", "_" also allowed except as the first or last character.
            #
            # @param path [Array<String, Symbol>] Metadata attribute path names
            #
            # @raise [SeregaError] when metadata attribute name has invalid format
            # @return [void]
            #
            def call(path)
              path.each { |attr_name| check_name(attr_name) }
            end

            private

            def check_name(name)
              name = name.to_s

              valid =
                case name.size
                when 0 then false
                when 1 then name.match?(FORMAT_ONE_CHAR)
                else name.match?(FORMAT_MANY_CHARS)
                end

              return if valid

              raise SeregaError, message(name)
            end

            def message(name)
              %(Invalid metadata path #{name.inspect}, globally allowed characters: "a-z", "A-Z", "0-9". Minus and low line "-", "_" also allowed except as the first or last character)
            end
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
serega-0.6.1 lib/serega/plugins/metadata/validations/check_path.rb
serega-0.6.0 lib/serega/plugins/metadata/validations/check_path.rb
serega-0.5.2 lib/serega/plugins/metadata/validations/check_path.rb
serega-0.5.1 lib/serega/plugins/metadata/validations/check_path.rb
serega-0.5.0 lib/serega/plugins/metadata/validations/check_path.rb
serega-0.4.0 lib/serega/plugins/metadata/validations/check_path.rb
serega-0.3.0 lib/serega/plugins/metadata/validations/check_path.rb
serega-0.2.0 lib/serega/plugins/metadata/validations/check_path.rb