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.



9
10
11
12
13
# File 'app/matchers/bulkrax/application_matcher.rb', line 9

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



88
89
90
91
92
93
94
95
96
# File 'app/matchers/bulkrax/application_matcher.rb', line 88

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



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

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



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

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



77
78
79
80
81
82
83
84
85
86
# File 'app/matchers/bulkrax/application_matcher.rb', line 77

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



56
57
58
# File 'app/matchers/bulkrax/application_matcher.rb', line 56

def parse_remote_files(src)
  { url: src.strip } if src.present?
end

#parse_resource_type(src) ⇒ Object

Only add valid resource types



99
100
101
102
103
# File 'app/matchers/bulkrax/application_matcher.rb', line 99

def parse_resource_type(src)
  Hyrax::ResourceTypesService.label(src.to_s.strip.titleize)
rescue KeyError
  nil
end

#parse_subject(src) ⇒ Object



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

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

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

#parse_types(src) ⇒ Object



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

def parse_types(src)
  src.to_s.strip.titleize
end

#process_parseObject



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

def process_parse
  # New parse methods will need to be added here
  parsed_fields = ['remote_files', 'language', 'subject', 'types', 'model', 'resource_type', 'format_original']
  # 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



31
32
33
34
35
36
37
38
# File 'app/matchers/bulkrax/application_matcher.rb', line 31

def process_split
  if self.split.is_a?(TrueClass)
    @result = @result.split(/\s*[:;|]\s*/) # default split by : ; |
  elsif self.split
    result = @result.split(Regexp.new(self.split))
    @result = result.map(&:strip)
  end
end

#result(_parser, content) ⇒ Object



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

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 = content.to_s.gsub(/\s/, ' ') # remove any line feeds and tabs
  @result.strip!
  process_split
  @result = @result[0] if @result.is_a?(Array) && @result.size == 1
  process_parse
  return @result
end