Sha256: 62a446cacd05956c8388483225cebbf865b0b79c32813676b4c9b7184c0d6e8c

Contents?: true

Size: 1.63 KB

Versions: 6

Compression:

Stored size: 1.63 KB

Contents

# Copyright © 2012 The Pennsylvania State University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'noid'

module Sufia
  module IdService
    @minter = ::Noid::Minter.new(:template => '.reeddeeddk')
    @pid = $$
    @namespace = Sufia::Engine.config.id_namespace
    @semaphore = Mutex.new
    def self.valid?(identifier)
      # remove the fedora namespace since it's not part of the noid
      noid = identifier.split(":").last
      return @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
      pid = ''
      File.open("tmp/minter-state", File::RDWR|File::CREAT, 0644) {|f|
        f.flock(File::LOCK_EX)
        yaml = YAML::load(f.read)
        yaml = {:template => '.reeddeeddk'} unless yaml
        minter = ::Noid::Minter.new(yaml)
        pid =  "#{@namespace}:#{minter.mint}"
        f.rewind
        yaml = YAML::dump(minter.dump)
        f.write yaml
        f.flush
        f.truncate(f.pos)
      }
      return pid
    end
    
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
sufia-1.3.0 lib/sufia/id_service.rb
sufia-models-0.0.2 lib/sufia/models/id_service.rb
sufia-models-0.0.1 lib/sufia/models/id_service.rb
sufia-1.2.0 lib/sufia/id_service.rb
sufia-1.1.0 lib/sufia/id_service.rb
sufia-1.0.0 lib/sufia/id_service.rb