Class: Bulkrax::ApplicationMatcher

Inherits:
Object
  • Object
show all
Defined in:
app/matchers/bulkrax/application_matcher.rb

Direct Known Subclasses

BagitMatcher, CsvMatcher, OaiMatcher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ ApplicationMatcher

Returns a new instance of ApplicationMatcher.



13
14
15
16
17
# File 'app/matchers/bulkrax/application_matcher.rb', line 13

def initialize(args)
  args.each do |k, v|
    send("#{k}=", v)
  end
end

Instance Attribute Details

#excludedObject

Returns the value of attribute excluded.



7
8
9
# File 'app/matchers/bulkrax/application_matcher.rb', line 7

def excluded
  @excluded
end

#fromObject

Returns the value of attribute from.



7
8
9
# File 'app/matchers/bulkrax/application_matcher.rb', line 7

def from
  @from
end

#ifObject

Returns the value of attribute if.



7
8
9
# File 'app/matchers/bulkrax/application_matcher.rb', line 7

def if
  @if
end

#nested_typeObject

Returns the value of attribute nested_type.



7
8
9
# File 'app/matchers/bulkrax/application_matcher.rb', line 7

def nested_type
  @nested_type
end

#parsedObject

Returns the value of attribute parsed.



7
8
9
# File 'app/matchers/bulkrax/application_matcher.rb', line 7

def parsed
  @parsed
end

#splitObject

Returns the value of attribute split.



7
8
9
# File 'app/matchers/bulkrax/application_matcher.rb', line 7

def split
  @split
end

#toObject

Returns the value of attribute to.



7
8
9
# File 'app/matchers/bulkrax/application_matcher.rb', line 7

def to
  @to
end

Instance Method Details

#extract_model(src) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'app/matchers/bulkrax/application_matcher.rb', line 94

def extract_model(src)
  if src&.match(URI::ABS_URI)
    src.split('/').last
  else
    src
  end
rescue StandardError
  nil
end

#parse_format_original(src) ⇒ Object



112
113
114
115
116
117
118
# File 'app/matchers/bulkrax/application_matcher.rb', line 112

def parse_format_original(src)
  # drop the case completely then upcase the first letter
  string = src.to_s.strip.downcase
  return if string.blank?

  string.slice(0, 1).capitalize + string.slice(1..-1)
end

#parse_language(src) ⇒ Object



66
67
68
69
# File 'app/matchers/bulkrax/application_matcher.rb', line 66

def parse_language(src)
  l = ::LanguageList::LanguageInfo.find(src.strip)
  l ? l.name : src
end

#parse_model(src) ⇒ Object

Allow for mapping a model field to the work type or collection



83
84
85
86
87
88
89
90
91
92
# File 'app/matchers/bulkrax/application_matcher.rb', line 83

def parse_model(src)
  model = nil
  if src.is_a?(Array)
    models = src.map { |m| extract_model(m) }.compact
    model = models.first if models.present?
  else
    model = extract_model(src)
  end
  return model
end

#parse_remote_files(src) ⇒ Object



59
60
61
62
63
64
# File 'app/matchers/bulkrax/application_matcher.rb', line 59

def parse_remote_files(src)
  return if src.blank?
  src.strip!
  name = Bulkrax::Importer.safe_uri_filename(src)
  { url: src, file_name: name }
end

#parse_resource_type(src) ⇒ Object

Only add valid resource types



105
106
107
108
109
110
# File 'app/matchers/bulkrax/application_matcher.rb', line 105

def parse_resource_type(src)
  ActiveSupport::Deprecation.warn('#parse_resource_type will be removed in Bulkrax v6.0.0')
  Hyrax::ResourceTypesService.label(src.to_s.strip.titleize)
rescue KeyError
  nil
end

#parse_subject(src) ⇒ Object



71
72
73
74
75
76
# File 'app/matchers/bulkrax/application_matcher.rb', line 71

def parse_subject(src)
  string = src.strip.downcase
  return if string.blank?

  string.slice(0, 1).capitalize + string.slice(1..-1)
end

#parse_types(src) ⇒ Object



78
79
80
# File 'app/matchers/bulkrax/application_matcher.rb', line 78

def parse_types(src)
  src.strip.titleize
end

#process_parseObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/matchers/bulkrax/application_matcher.rb', line 45

def process_parse
  # This accounts for prefixed matchers
  parser = parsed_fields.find { |field| to&.include? field }

  if @result.is_a?(Array) && self.parsed && self.respond_to?("parse_#{parser}")
    @result.each_with_index do |res, index|
      @result[index] = send("parse_#{parser}", res.strip)
    end
    @result.delete(nil)
  elsif self.parsed && self.respond_to?("parse_#{parser}")
    @result = send("parse_#{parser}", @result)
  end
end

#process_splitObject



36
37
38
39
40
41
42
43
# File 'app/matchers/bulkrax/application_matcher.rb', line 36

def process_split
  if self.split.is_a?(TrueClass)
    @result = @result.split(Bulkrax.multi_value_element_split_on)
  elsif self.split
    @result = @result.split(Regexp.new(self.split))
    @result = @result.map(&:strip).select(&:present?)
  end
end

#result(_parser, content) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/matchers/bulkrax/application_matcher.rb', line 19

def result(_parser, content)
  return nil if self.excluded == true || Bulkrax.reserved_properties.include?(self.to)
  return nil if self.if && (!self.if.is_a?(Array) && self.if.length != 2)

  if self.if
    return unless content.send(self.if[0], Regexp.new(self.if[1]))
  end

  # @result will evaluate to an empty string for nil content values
  @result = content.to_s.gsub(/\s/, ' ').strip # remove any line feeds and tabs
  # blank needs to be based to split, only skip nil
  process_split unless @result.nil?
  @result = @result[0] if @result.is_a?(Array) && @result.size == 1
  process_parse
  return @result
end