Last active
August 29, 2015 14:27
-
-
Save grosscol/faf3f154b1b382c6fb5b to your computer and use it in GitHub Desktop.
AF NOID writing to state file elicits complaints from rails.
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}" | |
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 opened in binary mode but WITHOUT explicit encoding." | |
statefile_one = '/tmp/stately01' | |
f_one = ::File.open(statefile_one, ::File::BINARY|::File::RDWR|::File::CREAT, 0644) | |
mint_and_write f_one | |
f_one.close | |
puts "\n-----\n\n" | |
puts "NOID with statefile opened in binary mode WITH explicitly set internal & external encoding" | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment