Sha256: 94cba1b08618af67021622ca7e5f8de4e5355df16aa3fc9c986434f0e027cf43

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module EncodedId
  module Rails
    # Configuration class for initializer
    class Configuration
      attr_accessor :salt, :character_group_size, :alphabet, :id_length
      attr_accessor :slug_value_method_name, :annotation_method_name
      attr_reader :group_separator, :slugged_id_separator, :annotated_id_separator

      def initialize
        @character_group_size = 4
        @group_separator = "-"
        @alphabet = ::EncodedId::Alphabet.modified_crockford
        @id_length = 8
        @slug_value_method_name = :name_for_encoded_id_slug
        @slugged_id_separator = "--"
        @annotation_method_name = :annotation_for_encoded_id
        @annotated_id_separator = "_"
      end

      # Perform validation vs alphabet on these assignments

      def group_separator=(value)
        unless valid_separator?(value, alphabet)
          raise ArgumentError, "Group separator characters must not be part of the alphabet"
        end
        @group_separator = value
      end

      def slugged_id_separator=(value)
        if value.blank? || value == group_separator || !valid_separator?(value, alphabet)
          raise ArgumentError, "Slugged ID separator characters must not be part of the alphabet or the same as the group separator"
        end
        @slugged_id_separator = value
      end

      def annotated_id_separator=(value)
        if value.blank? || value == group_separator || !valid_separator?(value, alphabet)
          raise ArgumentError, "Annotated ID separator characters must not be part of the alphabet or the same as the group separator"
        end
        @annotated_id_separator = value
      end

      def valid_separator?(separator, characters)
        separator.chars.none? { |v| characters.include?(v) }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
encoded_id-rails-1.0.0.beta3 lib/encoded_id/rails/configuration.rb
encoded_id-rails-1.0.0.beta2 lib/encoded_id/rails/configuration.rb