Skip to content

Instantly share code, notes, and snippets.

View demery's full-sized avatar

Doug Emery demery

  • Philadelphia, Pennsylvania
View GitHub Profile
@demery
demery / marc-search.rb
Last active July 14, 2023 15:38
Search a set of MARC XML for datafield nodes by datafield tag and subfield code
#!/usr/bin/env ruby
require 'nokogiri'
require 'optparse'
CMD = File.basename __FILE__
def usage
"Usage: #{CMD} [OPTIONS] TAG [CODE...] FILE [FILE...]"
end
Jun 28 2013 W4_tei.xml <surface n="Upper board outside" xml:id="n348.022200"><graphic url="master/W4_000001_1200.tif" xml:id="n348.022201"/>
Jun 28 2013 W7_tei.xml <surface n="Upper board outside" xml:id="n352.028025"><graphic url="master/W7_000001_1022.tif" xml:id="n352.028026"/>
Jun 28 2013 W12_tei.xml <surface n="Upper board outside" xml:id="n357.048799"><graphic url="master/W12_000001_1200.tif" xml:id="n357.048800"/>
Jun 28 2013 W13_tei.xml <surface n="Upper board outside" xml:id="n471.035784"><graphic url="master/W13_000001_1200.tif" xml:id="n471.035785"/>
Jun 28 2013 W18_tei.xml <surface n="Upper board outside" xml:id="n342.010960"><graphic url="master/W18_000001_700.tif" xml:id="n342.010961"/>
Jun 28 2013 W26_tei.xml <surface n="Upper board outside" xml:id="n360.007143"><graphic url="master/W26_000001_1200.tif" xml:id="n360.007144"/>
Jun 28 2013 W30_tei.xml <surface n="Upper board outside" xml:id=
@demery
demery / incremental_web_mv.sh
Created March 5, 2015 15:37
Incremental web folders
#!/usr/bin/env bash
# IMPORTANT: this script assumes that are no spaces in any of your paths
# create a dir list this way:
#
# $ find /path/to/source/directories -type d -name web | grep -v extra > /path/to/list-of-web-directories.txt
#
# This command will find all directories (-type d) named 'web' -- but not
# the ones in the `extra` folders -- and redirect all that to a file listing
@demery
demery / gssapi-with-mic.sh
Created February 7, 2012 19:27
Permission denied (publickey,gssapi-with-mic)
$ rsync -Cavz -e 'ssh -p 1234 -i $HOME/.ssh/id_dsa' usernam@host.com:path/to/files/*.txt .
Permission denied (publickey,gssapi-with-mic).
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(601) [Receiver=3.0.8]
@demery
demery / hg merge
Created August 24, 2011 15:57
Merge a named branch
# switch to the named branch
$ hg update -C my_branch
# write some code and commit the changes
$ hg commit
# switch to default
$ hg update -C default
# merge the revised code from the branch
def magical_solution_to_all_your_problems(*args)
# do something critical
end
# config/initializers/deletable.rb
ActiveRecord::Base.class_eval do
before_destroy :check_deletable
def self.undeletable_if_used(by_hash)
unless by_hash[:by]
raise "Expected hash to have key :by and a value of one or more symbols"
end
define_method(:deletable?) do
![by_hash[:by]].flatten.detect { |a|
def destroy_tag(object)
if current_user.admin?
if object.deletable?
link_to('Destroy', object, :confirm => 'Are you sure?',
:method => :delete)
else
"[Item used]"
end
end
end
def self.undeletable_if_used(by_hash)
unless by_hash[:by]
raise "Expected hash to have key :by and a value of one or more symbols"
end
define_method(:deletable?) do
![by_hash[:by]].flatten.detect { |a|
if self.respond_to? "#{a.to_s}_count"
self.send("#{a.to_s}_count") > 0
else
self.send(a.to_s).count
class Category < ActiveRecord::Base
before_destroy :check_deletable
has_many :tasks
has_many :projects
# ...
end
class Task < ActiveRecord::Base