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)


199
200
201
202
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 199

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

#field_supported?(field) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 128

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

  return false if excluded?(field)
  return true if supported_bulkrax_fields.include?(field)

  Bulkrax.object_factory.field_supported?(field: field, model: factory_class)
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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 183

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

#fields_that_are_always_multipleObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 151

def fields_that_are_always_multiple
  @fields_that_are_always_multiple = %w[
    id
    delete
    model
    visibility
    visibility_during_embargo
    embargo_release_date
    visibility_after_embargo
    visibility_during_lease
    lease_expiration_date
    visibility_after_lease
  ]
end

#fields_that_are_always_singularObject



166
167
168
169
170
171
172
173
174
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 166

def fields_that_are_always_singular
  @fields_that_are_always_singular ||= %W[
    file
    remote_files
    rights_statement
    #{related_parents_parsed_mapping}
    #{related_children_parsed_mapping}
  ]
end

#get_object_name(field) ⇒ Object



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

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

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



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 114

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

Determine a multiple properties field

Returns:

  • (Boolean)


144
145
146
147
148
149
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 144

def multiple?(field)
  return true if fields_that_are_always_singular.include?(field.to_s)
  return false if fields_that_are_always_multiple.include?(field.to_s)

  Bulkrax.object_factory.field_multi_value?(field: field, model: factory_class)
end

#multiple_metadata(content) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 97

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

#schema_form_definitionsObject



176
177
178
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 176

def schema_form_definitions
  @schema_form_definitions ||= ::SchemaLoader.new.form_definitions_for(factory_class.name.underscore.to_sym)
end

#set_parsed_data(name, value) ⇒ Object



63
64
65
66
67
68
69
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 63

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

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

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 71

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



91
92
93
94
95
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 91

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



137
138
139
140
# File 'app/models/concerns/bulkrax/has_matchers.rb', line 137

def supported_bulkrax_fields
  @supported_bulkrax_fields ||= fields_that_are_always_singular +
                                fields_that_are_always_multiple
end