Sha256: 7590736673be8c2aa03907c9dd25150a6615e860cd43b3d3f8ae093803e0c568
Contents?: true
Size: 1.39 KB
Versions: 41
Compression:
Stored size: 1.39 KB
Contents
module Spree module Core class NumberGenerator < Module BASE = 10 DEFAULT_LENGTH = 9 attr_accessor :prefix, :length def initialize(options) @prefix = options.fetch(:prefix) @length = options.fetch(:length, DEFAULT_LENGTH) @letters = options[:letters] end def included(host) generator_instance = self host.class_eval do validates(:number, presence: true, uniqueness: { allow_blank: true }) before_validation do |instance| instance.number ||= generate_permalink(host) end define_singleton_method(:number_generator) { generator_instance } def generate_permalink(host) host.number_generator.generate_permalink(host) end end end def generate_permalink(host) length = @length loop do candidate = new_candidate(length) return candidate unless host.exists?(number: candidate) # If over half of all possible options are taken add another digit. length += 1 if host.count > Rational(BASE**length, 2) end end def new_candidate(length) characters = @letters ? 36 : 10 @prefix + SecureRandom.random_number(characters**length).to_s(characters).rjust(length, '0').upcase end end # Permalink end # Core end # Spree
Version data entries
41 entries across 41 versions & 1 rubygems