Class: Mab2::Document::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/mab2/document/scope.rb

Defined Under Namespace

Classes: Subfield

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Scope

Returns a new instance of Scope.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mab2/document/scope.rb', line 10

def initialize(options = {})
  @document = options[:document]
  @ind1 = [options[:ind1]].flatten(1).compact
  @ind2 = [options[:ind2]].flatten(1).compact
  @tag = options[:tag]
  @subfield = [options[:subfield]].flatten(1).compact

  # negation extraction
  @ind1.delete_if do |ind1|
    if ind1.start_with?("-") && ind1.length > 1
      (@not_ind1 ||= []).push(ind1[1..-1])
      true
    end
  end
end

Instance Method Details

#fieldsObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/mab2/document/scope.rb', line 40

def fields
  datafields.map do |datafield|
    self.class.new({
      document: @document,
      ind1: datafield["@ind1"],
      ind2: datafield["@ind2"],
      tag: datafield["@tag"]
    })
  end
end

#getObject



26
27
28
# File 'lib/mab2/document/scope.rb', line 26

def get
  self
end

#get_subfield(code) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/mab2/document/scope.rb', line 30

def get_subfield(code)
  [datafields.first["subfield"]].compact.flatten(1)
  .try(:find) do |subfield|
    subfield["@code"] == code
  end
  .try do |subfield|
    Subfield.new(subfield["@code"], subfield["$"])
  end || Subfield.new
end

#subfield(codes) ⇒ Object



51
52
53
# File 'lib/mab2/document/scope.rb', line 51

def subfield(codes)
  self.class.new(to_options.merge(subfield: codes))
end

#subfieldsObject



55
56
57
58
59
60
61
# File 'lib/mab2/document/scope.rb', line 55

def subfields
  datafields.map do |datafield|
    [datafield["subfield"]].compact.flatten(1).try(:map) do |subfield|
      Subfield.new(subfield["@code"], subfield["$"])
    end
  end.compact.flatten(1) || []
end

#value(options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/mab2/document/scope.rb', line 63

def value(options = {})
  separator = options[:join_subfields] || " "

  if @document.legacy_mabmapper_mode?
    values.first.join(separator)
  else
    values.map(&:presence).compact.first.try(:join, separator)
  end
end

#values(options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mab2/document/scope.rb', line 73

def values(options = {})
  datafields
  .map do |field|
    [field["subfield"]].compact.flatten(1)
    .select do |subfield|
      @subfield.empty? || @subfield.include?(subfield["@code"])
    end
    .map do |subfield|
      subfield["$"]
    end
    .try do |subfield_values|
      if separator = options[:join_subfields]
        subfield_values.join(separator)
      else
        subfield_values
      end
    end
  end
end