Module: Bulkrax::HasMatchers

Extended by:
ActiveSupport::Concern
Included in:
Entry
Defined in:
app/models/concerns/bulkrax/has_matchers.rb

Instance Method Summary collapse

Instance Method Details

#add_metadata(node_name, node_content, index = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 30

def (node_name, node_content, index = nil)
  field_to(node_name).each do |name|
    matcher = self.class.matcher(name, mapping[name].symbolize_keys) if mapping[name] # the field matched to a pre parsed value in application_matcher.rb
    object_name = get_object_name(name) || false # the "key" of an object property. e.g. { object_name: { alpha: 'beta' } }
    multiple = multiple?(name) # the property has multiple values. e.g. 'letters': ['a', 'b', 'c']
    object_multiple = object_name && multiple?(object_name) # the property's value is an array of object(s)

    next unless field_supported?(name) || (object_name && field_supported?(object_name))

    if object_name
      Rails.logger.info("Bulkrax Column automatically matched object #{node_name}, #{node_content}")
      [object_name] ||= object_multiple ? [{}] : {}
    end

    value = if matcher
              result = matcher.result(self, node_content)
              (multiple, name, result, object_multiple)
            elsif multiple
              Rails.logger.info("Bulkrax Column automatically matched #{node_name}, #{node_content}")
              (node_content)
            else
              Rails.logger.info("Bulkrax Column automatically matched #{node_name}, #{node_content}")
              (node_content)
            end

    object_name.present? ? set_parsed_object_data(object_multiple, object_name, name, index, value) : set_parsed_data(name, value)
  end
end

#excluded?(field) ⇒ Boolean

Check whether a field is explicitly excluded in the mapping

Returns:

  • (Boolean)


184
185
186
187
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 184

def excluded?(field)
  return false if mapping[field].blank?
  mapping[field]['excluded'] || false
end

#field_supported?(field) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
128
129
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 123

def field_supported?(field)
  field = field.gsub('_attributes', '')

  return false if excluded?(field)
  return true if supported_bulkrax_fields.include?(field)
  return factory_class.method_defined?(field) && factory_class.properties[field].present?
end

#field_to(field) ⇒ Array

Hyrax field to use for the given import field

Parameters:

  • field (String)

    the importer field name

Returns:

  • (Array)

    hyrax fields



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 168

def field_to(field)
  fields = mapping&.map do |key, value|
    return unless value

    if value['from'].instance_of?(Array)
      key if value['from'].include?(field) || key == field
    elsif (value['from'] == field) || key == field
      key
    end
  end&.compact

  return [field] if fields.blank?
  return fields
end

#get_object_name(field) ⇒ Object



161
162
163
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 161

def get_object_name(field)
  mapping&.[](field)&.[]('object')
end

#matched_metadata(multiple, name, result, object_multiple) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 109

def (multiple, name, result, object_multiple)
  if object_multiple
    if mapping[name]['nested_type'] && mapping[name]['nested_type'] == 'Array'
      (result)
    else
      (result)
    end
  elsif multiple
    (result)
  else
    (result)
  end
end

#multiple?(field) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 145

def multiple?(field)
  @multiple_bulkrax_fields ||=
    %W[
      file
      remote_files
      rights_statement
      #{related_parents_parsed_mapping}
      #{related_children_parsed_mapping}
    ]

  return true if @multiple_bulkrax_fields.include?(field)
  return false if field == 'model'

  field_supported?(field) && factory_class&.properties&.[](field)&.[]('multiple')
end

#multiple_metadata(content) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 92

def (content)
  return unless content

  case content
  when Nokogiri::XML::NodeSet
    content&.content
  when Array
    content
  when Hash
    Array.wrap(content)
  when String
    Array.wrap(content.strip)
  else
    Array.wrap(content)
  end
end

#set_parsed_data(name, value) ⇒ Object



59
60
61
62
63
64
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 59

def set_parsed_data(name, value)
  return [name] = value unless multiple?(name)

  [name] ||= []
  [name] += Array.wrap(value).flatten
end

#set_parsed_object_data(object_multiple, object_name, name, index, value) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 66

def set_parsed_object_data(object_multiple, object_name, name, index, value)
  if object_multiple
    index ||= 0
    [object_name][index] ||= {}
    [object_name][index][name] ||= []
    if value.is_a?(Array)
      [object_name][index][name] += value
    else
      [object_name][index][name] = value
    end
  else
    [object_name][name] ||= []
    if value.is_a?(Array)
      [object_name][name] += value
    else
      [object_name][name] = value
    end
  end
end

#single_metadata(content) ⇒ Object



86
87
88
89
90
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 86

def (content)
  content = content.content if content.is_a?(Nokogiri::XML::NodeSet)
  return unless content
  Array.wrap(content.to_s.strip).join('; ')
end

#supported_bulkrax_fieldsObject



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 131

def supported_bulkrax_fields
  @supported_bulkrax_fields ||=
    %W[
      id
      file
      remote_files
      model
      visibility
      delete
      #{related_parents_parsed_mapping}
      #{related_children_parsed_mapping}
    ]
end