Sha256: 609f7abdfd3e72677fc9fa0472059055f4b3047de59dcb138e1b2c6df8de407e

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

require 'noid'

module Noid
  module Rails
    module Minter
      # @abstract the base class for minters
      class Base < ::Noid::Minter
        ##
        # @param template [#to_s] a NOID template
        # @see Noid::Template
        def initialize(template = default_template)
          super(template: template.to_s)
        end

        ##
        # Sychronously mint a new identifier.
        #
        # @return [String] the minted identifier
        def mint
          Mutex.new.synchronize do
            loop do
              pid = next_id
              return pid unless identifier_in_use?(pid)
            end
          end
        end

        ##
        # @return [Hash{Symbol => String, Object}] representation of the current minter state
        def read
          raise NotImplementedError, 'Implement #read in child class'
        end

        ##
        # Updates the minter state to that of the `minter` parameter.
        #
        # @param minter [Minter::Base]
        # @return [void]
        def write!(_)
          raise NotImplementedError, 'Implement #write! in child class'
        end

        private

        def identifier_in_use?(id)
          Noid::Rails.config.identifier_in_use.call(id)
        end

        ##
        # @return [#to_s] the default template for this
        def default_template
          Noid::Rails.config.template
        end

        ##
        # @return [String] a new identifier
        def next_id
          raise NotImplementedError, 'Implement #next_id in child class'
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
noid-rails-3.2.0 lib/noid/rails/minter/base.rb
noid-rails-3.1.0 lib/noid/rails/minter/base.rb
noid-rails-3.0.3 lib/noid/rails/minter/base.rb
noid-rails-3.0.2 lib/noid/rails/minter/base.rb
noid-rails-3.0.1 lib/noid/rails/minter/base.rb
noid-rails-3.0.0 lib/noid/rails/minter/base.rb