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.



11
12
13
14
15
# File 'app/matchers/bulkrax/application_matcher.rb', line 11

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

Instance Attribute Details

#excludedObject

Returns the value of attribute excluded.



5
6
7
# File 'app/matchers/bulkrax/application_matcher.rb', line 5

def excluded
  @excluded
end

#fromObject

Returns the value of attribute from.



5
6
7
# File 'app/matchers/bulkrax/application_matcher.rb', line 5

def from
  @from
end

#ifObject

Returns the value of attribute if.



5
6
7
# File 'app/matchers/bulkrax/application_matcher.rb', line 5

def if
  @if
end

#nested_typeObject

Returns the value of attribute nested_type.



5
6
7
# File 'app/matchers/bulkrax/application_matcher.rb', line 5

def nested_type
  @nested_type
end

#parsedObject

Returns the value of attribute parsed.



5
6
7
# File 'app/matchers/bulkrax/application_matcher.rb', line 5

def parsed
  @parsed
end

#splitObject

Returns the value of attribute split.



5
6
7
# File 'app/matchers/bulkrax/application_matcher.rb', line 5

def split
  @split
end

#toObject

Returns the value of attribute to.



5
6
7
# File 'app/matchers/bulkrax/application_matcher.rb', line 5

def to
  @to
end

Instance Method Details

#extract_model(src) ⇒ Object



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

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



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

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



64
65
66
67
# File 'app/matchers/bulkrax/application_matcher.rb', line 64

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



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

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



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

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



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

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



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

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



76
77
78
# File 'app/matchers/bulkrax/application_matcher.rb', line 76

def parse_types(src)
  src.strip.titleize
end

#process_parseObject



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

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



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

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



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

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