Skip to content

Instantly share code, notes, and snippets.



Last active Jan 24, 2018
What would you like to do?
Rails runner friendly script to associate pids with a collection

The associated Ruby script is more of a proof of concept to demonstrate using pipes and parameters in conjunction with the Rails runner. I have not fully verified that it works.

echo '<work_pid>' | bundle exec rails runner associate_pids_with_collection.rb -c <collection_pid>
# !/usr/bin/env ruby
# In general this script does the following, assuming that it is run within a Rails context:
# ```ruby
# work = ActiveFedora::Base.find(patent_pid, cast: true)
# collection = LibraryCollection(collection_pid)
# return true if work.library_collection_ids.include?(
# work.library_collection_ids +=
# ```
require 'optparse'
config = {
environment: 'development'
command_name = File.basename(__FILE__) do |options|
# This banner is the first line of your help documentation.
options.set_banner "Usage: bundle exec rails runner #{command_name} --collection_id [PID] [files]\n\n" \
"Associate items identified by PIDs in the source files with the given --collection_id.\n\n" \
"Each line of the given file is assumed to be a single pid\n\n " \
"Examples:\n" \
"\t$ bundle exec rails runner #{command_name} -c 'und:123456789' list_of_work_pids.txt\n" \
"\t$ bundle exec rails runner #{command_name} -c 'und:123456789' list_of_pids.txt\n" \
"\t$ bundle exec rails runner #{command_name} -c 'und:123456789' list_of_pids.txt | more\n" \
"\t$ bundle exec rails runner #{command_name} -c 'und:123456789' list_of_pids.txt > output.txt\n" \
"\t$ bundle exec rails runner #{command_name} -c 'und:123456789' list_of_pids.txt second_list_of_pids.txt\n" \
"\t$ echo 'und:abcdefg' | bundle exec rails runner #{command_name} -c 'und:123456789'"
# Separator just adds a new line with the specified text.
options.separator ""
options.separator "Specific options:"
# Note this is provided to "parrot" the parameters of rails runner
options.on("-e", "--environment=name", String, "Specifies the environment for the runner to operate under (test/development/production).", "Default: #{config[:environment]}") do |environment|
config[:environment] = environment
options.on("-c", "--collection_pid [PID]", String, "The full PID of the target Collection (REQUIRED)") do |collection_pid|
config[:collection_pid] = collection_pid
options.on_tail("-h", "--help", "You're looking at it!") do
$stderr.puts options
exit 1
# Responsible for associating the PID to the config assigned collection
def associate_pid(pid, config)
pid = pid.strip
collection = config.fetch(:collection)
work = nil
work = ActiveFedora::Base.find(pid, cast: true)
rescue ActiveFedora::ObjectNotFoundError
$stderr.puts "Unable to find ActiveFedora::Base ID='#{pid}' and cannot add to the #{collection.class} ID='#{}'"
return false
if work.library_collection_ids.include?(
$stdout.puts "#{work.class} ID='#{}' already associated with #{collection.class} ID='#{}'"
return true
work.library_collection_ids += []!
$stdout.puts "Added #{work.class} ID='#{}' to #{collection.class} ID='#{}'"
return true
unless config.key?(:collection_pid)
$stderr.puts "Error: #{command_name} requires a --collection_pid option\nSee '#{command_name} -h' for more information"
exit 2
$stdout.puts "Finding LibraryCollection ID='#{config.fetch(:collection_pid)}'"
config[:collection] = LibraryCollection.find(config.fetch(:collection_pid))
rescue ActiveFedora::ObjectNotFoundError
$stderr.puts "Unable to find LibraryCollection ID='#{config.fetch(:collection_pid)}'"
exit 3
# Keep reading lines of input as long as they're coming.
while input = ARGF.gets
input.each_line do |pid|
associate_pid(pid, config)
rescue Errno::EPIPE
# sysexits(3) specifies that exit code 74 represent an IO error,
# which is the likely situation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.