Sha256: e373d9cd7d8f465dc56ecb1168eea8266dbe644080dc65e28248471b784a9fe6

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

require 'noid'

module ActiveFedora
  module Noid
    class SynchronizedMinter < Minter::Base
      attr_reader :statefile

      def initialize(template = default_template, statefile = default_statefile)
        super(template)
        @statefile = statefile
      end

      protected

      def default_statefile
        ActiveFedora::Noid.config.statefile
      end

      def state_for(io_object)
        Marshal.load(io_object.read)
      rescue TypeError, ArgumentError
        { template: template }
      end

      def next_id
        id = nil
        ::File.open(statefile, 'a+b', 0644) do |f|
          f.flock(::File::LOCK_EX)
          # Files opened in append mode seek to end of file
          f.rewind
          state = state_for(f)
          state[:template] &&= state[:template].to_s
          minter = ::Noid::Minter.new(state) # minter w/in the minter, lives only for an instant
          id = minter.mint

          # Wipe prior contents so the new state can be written from the beginning of the file
          f.truncate(0)
          new_state = Marshal.dump(minter.dump)
          f.write(new_state)
        end
        id
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
active_fedora-noid-2.0.0.beta3 lib/active_fedora/noid/synchronized_minter.rb
active_fedora-noid-2.0.0.beta2 lib/active_fedora/noid/synchronized_minter.rb
active_fedora-noid-2.0.0.beta1 lib/active_fedora/noid/synchronized_minter.rb