Module: PennMARC::Test::MarcHelpers

Defined in:
lib/pennmarc/test/marc_helpers.rb

Overview

Helper methods for use in constructing MARC objects for testing

Instance Method Summary collapse

Instance Method Details

#location_mapHash

Mock map for location lookup using Location helper The location codes :dent and :stor are the two outermost keys :specific_location, :library, :display are the inner keys that store location values

Examples:

location_map[:stor][:library] #=> 'LIBRA'

Returns:

  • (Hash)


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pennmarc/test/marc_helpers.rb', line 70

def location_map
  { dent: {  specific_location: 'Levy Dental Medicine Library - Stacks',
             library: ['Health Sciences Libraries', 'Levy Dental Medicine Library'],
             display: 'Levy Dental Medicine Library - Stacks' },
    stor: { specific_location: 'LIBRA',
            library: 'LIBRA',
            display: 'LIBRA' },
    vanp: { specific_location: 'Van Pelt - Stacks',
            library: 'Van Pelt-Dietrich Library Center',
            display: 'Van Pelt Library' } }
end

#marc_control_field(tag:, value:) ⇒ MARC::ControlField

Return a new ControlField (000-009)

Parameters:

  • tag (String)
  • value (String)

Returns:

  • (MARC::ControlField)


29
30
31
# File 'lib/pennmarc/test/marc_helpers.rb', line 29

def marc_control_field(tag:, value:)
  MARC::ControlField.new tag, value
end

#marc_field(tag: 'TST', indicator1: ' ', indicator2: ' ', subfields: {}) ⇒ MARC::DataField

Create an isolated MARC::DataField object for use in specs Can pass in tag, indicators and subfields (using simple hash structure). E.g., marc_field(tag: ‘650’, indicator2: ‘7’),

subfields: { a: 'Tax planning',
             m: ['Multiple', 'Subfields']
             z: 'United States.',
             '0': http://id.loc.gov/authorities/subjects/sh2008112546 }
)

Parameters:

  • tag (String (frozen)) (defaults to: 'TST')

    MARC tag, e.g., 001, 665

  • indicator1 (String (frozen)) (defaults to: ' ')

    MARC indicator 1, e.g., 0

  • indicator2 (String (frozen)) (defaults to: ' ')
  • subfields (Hash) (defaults to: {})

    hash of subfield values as code => value or code => [value, value]

Returns:

  • (MARC::DataField)


46
47
48
49
50
51
# File 'lib/pennmarc/test/marc_helpers.rb', line 46

def marc_field(tag: 'TST', indicator1: ' ', indicator2: ' ', subfields: {})
  subfield_objects = subfields.each_with_object([]) do |(code, value), array|
    Array.wrap(value).map { |v| array << marc_subfield(code, v) }
  end
  MARC::DataField.new tag, indicator1, indicator2, *subfield_objects
end

#marc_record(fields: [], leader: nil) ⇒ MARC::Record

Return a MARC::Record containing passed in DataFields

Parameters:

  • fields (Array<MARC::DataField>) (defaults to: [])
  • leader (String, nil) (defaults to: nil)

Returns:

  • (MARC::Record)


57
58
59
60
61
62
# File 'lib/pennmarc/test/marc_helpers.rb', line 57

def marc_record(fields: [], leader: nil)
  record = MARC::Record.new
  fields.each { |field| record << field }
  record.leader = leader if leader
  record
end

#marc_subfield(code, value) ⇒ MARC::Subfield

Create an isolated MARC::Subfield object for use in specs or as part of a MARC::Field

Parameters:

  • code (String)
  • value (String)

Returns:

  • (MARC::Subfield)


21
22
23
# File 'lib/pennmarc/test/marc_helpers.rb', line 21

def marc_subfield(code, value)
  MARC::Subfield.new code.to_s, value
end

#record_from(filename) ⇒ MARC::Record, NilClass

Return a MARC::XMLReader that will parse a given file and return MARC::Record objects

Parameters:

  • filename (String)

    filename of MARCXML fixture

Returns:

  • (MARC::Record, NilClass)


13
14
15
# File 'lib/pennmarc/test/marc_helpers.rb', line 13

def record_from(filename)
  MARC::XMLReader.new(marc_xml_path(filename)).first
end