Class: Bulkrax::CsvTemplate::MappingManager

Inherits:
Object
  • Object
show all
Defined in:
app/services/bulkrax/csv_template/mapping_manager.rb

Overview

Handles loading and filtering of Bulkrax field mappings

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(include_generated: true) ⇒ MappingManager

Returns a new instance of MappingManager.

Parameters:

  • include_generated (Boolean) (defaults to: true)

    when false, excludes mapping entries flagged ‘generated: true` (system-maintained fields like date_uploaded, depositor, source_identifier). Template generation passes false so the downloadable template doesn’t expose system columns; import validation uses the default true so that user-configured mappings like ‘rights_statement` (which Bulkrax ships with `generated: true`) are still recognised when the CSV uses one of their `from:` aliases.



17
18
19
# File 'app/services/bulkrax/csv_template/mapping_manager.rb', line 17

def initialize(include_generated: true)
  @mappings = load_mappings(include_generated: include_generated)
end

Instance Attribute Details

#mappingsObject (readonly)

Returns the value of attribute mappings.



7
8
9
# File 'app/services/bulkrax/csv_template/mapping_manager.rb', line 7

def mappings
  @mappings
end

Instance Method Details

#find_by_flag(field_name, default) ⇒ Object



51
52
53
# File 'app/services/bulkrax/csv_template/mapping_manager.rb', line 51

def find_by_flag(field_name, default)
  @mappings.find { |_k, v| v[field_name] == true }&.first || default
end

#get_object_name(key) ⇒ Object

Returns the ‘object:` value for a given mapping key, or nil. Mirrors the importer-side `Bulkrax::HasMatchers#get_object_name` for callers working with the template-side mapping manager.



32
33
34
# File 'app/services/bulkrax/csv_template/mapping_manager.rb', line 32

def get_object_name(key)
  @mappings.dig(key, "object")
end

#key_to_mapped_column(key) ⇒ Object



25
26
27
# File 'app/services/bulkrax/csv_template/mapping_manager.rb', line 25

def key_to_mapped_column(key)
  @mappings.dig(key, "from")&.first || key
end

#mapped_to_key(column_str) ⇒ Object



21
22
23
# File 'app/services/bulkrax/csv_template/mapping_manager.rb', line 21

def mapped_to_key(column_str)
  @mappings.find { |_k, v| v["from"].include?(column_str) }&.first || column_str
end

#object_columns_for(object_name) ⇒ Object

Returns the column names that target a given object name via the ‘object:` field-mapping pattern. The template generator uses this to emit the per-child columns (e.g. redirect_path, redirect_canonical, redirect_sequence) instead of the bare property name (redirects). Numbering is intentionally omitted — the template shows the column shape once; CSV rows can repeat the column with numeric suffixes (e.g. redirect_path_1, redirect_path_2) at import time.



43
44
45
46
47
48
49
# File 'app/services/bulkrax/csv_template/mapping_manager.rb', line 43

def object_columns_for(object_name)
  @mappings
    .select { |_k, v| v.is_a?(Hash) && v["object"] == object_name }
    .values
    .flat_map { |v| Array(v["from"]) }
    .uniq
end

#resolve_column_name(key: nil, flag: nil, default: nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/services/bulkrax/csv_template/mapping_manager.rb', line 59

def resolve_column_name(key: nil, flag: nil, default: nil)
  if flag
    mapped_key = find_by_flag(flag, nil)
    if mapped_key
      mapped_options = @mappings.dig(mapped_key, "from") || []
      return mapped_options if mapped_options.any?
    end
  end

  if key
    mapped_options = @mappings.dig(key, "from") || []
    return mapped_options if mapped_options.any?
  end

  default ? [default] : []
end

#split_value_for(mapping_key) ⇒ Object



55
56
57
# File 'app/services/bulkrax/csv_template/mapping_manager.rb', line 55

def split_value_for(mapping_key)
  @mappings.dig(mapping_key, "split")
end