Sha256: e3285db973402beb5978dfe796f7935a8f524ad8060a0336b29d3bc4ebacb6a5

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true
module HumanizedId
  class Humanizer
    SOURCE_CHARSET = (('0'..'9').to_a + ('a'..'z').to_a).freeze

    def initialize(id:, min_length: nil, prefix: '')
      @id               = id
      @min_length       = min_length
      @prefix           = prefix.to_s
      @target_charset   = CHARSET.join
      @source_charset   = SOURCE_CHARSET.join
    end

    def generate_humanized_id
      new_id = resize convert_to_target_charset convert_to_target_base(@id)
      "#{@prefix}#{new_id}"
    end

    private

    def convert_to_target_base(id)
      fail Error, 'id is not an integer' unless id.is_a? Integer
      id.to_s(@target_charset.length)
    end

    def convert_to_target_charset(id)
      source_charset_subset = @source_charset.slice(0, @target_charset.length)
      id.tr(source_charset_subset, @target_charset)
    end

    def resize(id)
      return id if @min_length.nil? || @min_length <= id.length
      padding = @target_charset[0] * (@min_length - id.length)
      "#{padding}#{id}"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
humanized_id-0.4.0 lib/humanized_id/humanizer.rb
humanized_id-0.3.0 lib/humanized_id/humanizer.rb
humanized_id-0.2.0 lib/humanized_id/humanizer.rb