Sha256: bd60417dc3a3e7516191c2240e3b91da6d3f8565fe2513686933018c178db622

Contents?: true

Size: 516 Bytes

Versions: 3

Compression:

Stored size: 516 Bytes

Contents

module PublicId
  module Util
    def self.generate_random_suffix(length)
      SecureRandom.urlsafe_base64(length)
    end
    def self.generate_prefix(klass, joiner)
      klass.to_s.demodulize.underscore.first(3) + joiner
    end
    def self.new_public_id(klass, options = {})
      length = options[:length] || 10
      prefix = options.fetch(:prefix, generate_prefix(klass, options.fetch(:joiner, '-') ))
      suffix = generate_random_suffix(length)
      "#{prefix ? prefix : ''}#{suffix}"
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
public_id-1.0.2 lib/public_id/util.rb
public_id-1.0.1 lib/public_id/util.rb
public_id-1.0.0 lib/public_id/util.rb