Skip to content

Instantly share code, notes, and snippets.

@billdueber
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billdueber/0e05d7db014989cacb05 to your computer and use it in GitHub Desktop.
Save billdueber/0e05d7db014989cacb05 to your computer and use it in GitHub Desktop.
Traject extract_marc default value
require 'traject'
include Traject::Macros::Marc21
r = MARC::Record.new
r << MARC::ControlField.new('001', '12345')
r << MARC::DataField.new('245', ' ', ' ', ['a', 'My Title'])
context = Traject::Indexer::Context.new
acc1 = []
em1 = extract_marc('245a:999b', :default=>'Default Value')
em1.call(r,acc1, context) #=> acc1 == ['My Title']
acc2 = []
em2 = extract_marc('999b', :default=>'Default Value')
em2.call(r,acc2,context) #=> acc2 == ['Default Value']
acc3 = []
em3 = extract_marc('999b', :default => nil)
em3.call(r,acc3,context) #=> acc3 == [], not [nil]
class NullObject
def nil?
true
end
end
acc4 = []
nobj = NullObject.new
em4 = extract_marc('999b', :default => nobj)
em4.call(r,acc4,context) #=> acc4 == [nobj]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment