Sha256: 96f904679a77d0c59768d6dfff65fa6527f3455babf047cfff89f2a026b5e4de

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

require 'noid'

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

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

        def default_statefile
          ActiveFedora::Noid.config.statefile
        end

        def read
          with_file do |f|
            state_for(f)
          end
        end

        def write!(minter)
          with_file do |f|
            # Wipe prior contents so the new state can be written from the beginning of the file
            f.truncate(0)
            f.write(Marshal.dump(minter.dump))
          end
        end

        protected

        def with_file
          ::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
            yield f
          end
        end

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

        def next_id
          state = read
          state[:template] &&= state[:template].to_s
          minter = ::Noid::Minter.new(state) # minter w/in the minter, lives only for an instant
          id = minter.mint
          write!(minter)
          id
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_fedora-noid-2.0.0.beta5 lib/active_fedora/noid/minter/file.rb
active_fedora-noid-2.0.0.beta4 lib/active_fedora/noid/minter/file.rb