Created
August 10, 2015 21:08
-
-
Save grosscol/826ea23034acbe786b0e to your computer and use it in GitHub Desktop.
More NOID minting woes with File io
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Make a new rails application and drop this file in. | |
# | |
# Run from the command line with just plain ruby: | |
# bundle exec ruby doit.rb | |
# | |
# Run from the command line with rails: | |
# bundle exec rails runner "eval(File.read 'doit.rb')" | |
require 'active_fedora/noid' | |
def state_for(io_object) | |
Marshal.load(io_object.read) | |
rescue TypeError, ArgumentError | |
{ template: ActiveFedora::Noid.config.template } | |
end | |
def write_state_to_file( out_file, out_state ) | |
begin | |
puts "new state encoding #{out_state.encoding}" | |
out_file.write( out_state ) | |
rescue Encoding::UndefinedConversionError => errz | |
puts "Boom! Error: #{errz}" | |
end | |
end | |
def mint_and_write( out_file ) | |
state = state_for(out_file) | |
puts "state class: #{state.class}" | |
puts "state seed: #{state[:seed]}" | |
puts "state seq: #{state[:seq]}" | |
minter = ::Noid::Minter.new(state) | |
id = minter.mint | |
puts "id: #{id}" | |
out_file.rewind | |
new_state = Marshal.dump(minter.dump) | |
write_state_to_file( out_file, new_state ) | |
puts "End of Line." | |
end | |
puts "NOID with statefile one." | |
statefile_one = '/tmp/stately01' | |
f_one = ::File.open(statefile_one, "a+b", 0644) | |
mint_and_write f_one | |
f_one.close | |
puts "\n-----\n\n" | |
puts "NOID with statefile two opened." | |
statefile_two = '/tmp/stately02' | |
f_two = ::File.open(statefile_two, ::File::BINARY|::File::RDWR|::File::CREAT, 0644, encoding: Encoding::ASCII_8BIT) | |
mint_and_write f_two | |
f_two.close | |
puts "\n-----\n\n" | |
puts "NOID with statefile two repeat." | |
statefile_two = '/tmp/stately02' | |
f_two = ::File.open(statefile_two, "a+b", 0644) | |
mint_and_write f_two | |
f_two.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment