Sha256: c49bc591729df7956b383133236ab4689d7b8445d0f52198a65fef79ae394e6a

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

require 'noid'

module Sufia
  module IdService

    def self.noid_template
      Sufia.config.noid_template
    end

    @minter = ::Noid::Minter.new(template: noid_template)
    @pid = $$
    @semaphore = Mutex.new
    def self.valid?(identifier)
      # remove the fedora namespace since it's not part of the noid
      noid = identifier#.split(":").last
      @minter.valid? noid
    end
    def self.mint
      @semaphore.synchronize do
        while true
          pid = self.next_id
          return pid unless ActiveFedora::Base.exists?(pid)
        end
      end
    end

    protected

    def self.next_id
      id = ''
      File.open(Sufia.config.minter_statefile, File::RDWR|File::CREAT, 0644) do |f|
        f.flock(File::LOCK_EX)
        yaml = YAML::load(f.read)
        yaml = {template: noid_template} unless yaml
        minter = ::Noid::Minter.new(yaml)
        id = minter.mint
        f.rewind
        yaml = YAML::dump(minter.dump)
        f.write yaml
        f.flush
        f.truncate(f.pos)
      end
      id
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
sufia-6.0.0.rc4 sufia-models/app/services/sufia/id_service.rb
sufia-models-6.0.0.rc4 app/services/sufia/id_service.rb
sufia-6.0.0.rc3 sufia-models/app/services/sufia/id_service.rb
sufia-models-6.0.0.rc3 app/services/sufia/id_service.rb