Sha256: fed1577bde2b6251116b199d6b00e0b2afc28a025e5788e14270a24aaa5b97a5

Contents?: true

Size: 1.34 KB

Versions: 8

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaValidations
    module Attribute
      class CheckName
        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.
          # Globally allowed characters: "a-z", "A-Z", "0-9".
          # Minus and low line "-", "_" also allowed except as the first or last character.
          #
          # @param name [String, Symbol] Attribute name
          #
          # @raise [SeregaError] when name has invalid format
          # @return [void]
          #
          def call(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

          private

          def message(name)
            %(Invalid attribute name = #{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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
serega-0.6.1 lib/serega/validations/attribute/check_name.rb
serega-0.6.0 lib/serega/validations/attribute/check_name.rb
serega-0.5.2 lib/serega/validations/attribute/check_name.rb
serega-0.5.1 lib/serega/validations/attribute/check_name.rb
serega-0.5.0 lib/serega/validations/attribute/check_name.rb
serega-0.4.0 lib/serega/validations/attribute/check_name.rb
serega-0.3.0 lib/serega/validations/attribute/check_name.rb
serega-0.2.0 lib/serega/validations/attribute/check_name.rb