Module: Macros::Mods

Defined in:
lib/macros/mods.rb

Overview

Macros for extracting MODS values from Nokogiri documents

Instance Method Summary collapse

Instance Method Details

#extract_name(role: nil, exclude: nil) ⇒ Object


6
7
8
9
10
11
12
13
14
15
# File 'lib/macros/mods.rb', line 6

def extract_name(role: nil, exclude: nil)
  clause = if role
             Array(role).map { |r| "text() = '#{r}'" }.join(' or ')
           elsif exclude
             Array(exclude).map { |r| "text() != '#{r}'" }.join(' and ')
           else
             raise ArgumentError, 'You must provide either role or exclude parameters'
           end
  extract_mods("/*/mods:name[mods:role/mods:roleTerm/#{clause}]/mods:namePart")
end

#generate_mods_idObject


17
18
19
20
21
22
23
# File 'lib/macros/mods.rb', line 17

def generate_mods_id
  lambda { |record, accumulator, context|
    identifier = select_identifier(record, context)

    accumulator << identifier_with_prefix(context, identifier) if identifier.present?
  }
end

#generate_relation(xpath) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/macros/mods.rb', line 33

def generate_relation(xpath)
  lambda do |record, accumulator|
    url = record.xpath("#{xpath}/mods:location/mods:url", TrajectPlus::Macros::Mods::NS).map(&:text)
    title = record.xpath("#{xpath}/mods:titleInfo/mods:title", TrajectPlus::Macros::Mods::NS).map(&:text)

    if url.present?
      accumulator.concat(url)
    elsif title.present?
      accumulator.concat(title)
    end
  end
end

#normalize_languageObject


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/macros/mods.rb', line 50

def normalize_language
  mods_lang_label_xp = '/*/mods:language/mods:languageTerm[@authority="iso639-2b"][@type="text"]'
  mods_lang_code_xp = '/*/mods:language/mods:languageTerm[@authority="iso639-2b"][@type="code"]'
  mods_lang_xp = '/*/mods:language/mods:languageTerm'
  first(
    extract_mods(mods_lang_label_xp),
    extract_mods(mods_lang_code_xp, translation_map: ['marc_languages']),
    # the last one is separate to eventually pass fuzzy matching parameters
    extract_mods(mods_lang_xp, translation_map: ['marc_languages', default: '__passthrough__'])
  )
end

#normalize_scriptObject


62
63
64
# File 'lib/macros/mods.rb', line 62

def normalize_script
  extract_mods('/*/mods:language/mods:scriptTerm', translation_map: ['scripts', default: '__passthrough__'])
end

#normalize_typeObject


46
47
48
# File 'lib/macros/mods.rb', line 46

def normalize_type
  extract_mods('/*/mods:typeOfResource', translation_map: 'types')
end

#select_identifier(record, context) ⇒ Object


25
26
27
28
29
30
31
# File 'lib/macros/mods.rb', line 25

def select_identifier(record, context)
  if record.xpath('/*/mods:identifier', TrajectPlus::Macros::Mods::NS).map(&:text).reject(&:blank?).any?
    record.xpath('/*/mods:identifier', TrajectPlus::Macros::Mods::NS).map(&:text).reject(&:blank?).first
  else
    default_identifier(context)
  end
end