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



29
30
31
# File 'app/services/bulkrax/csv_template/mapping_manager.rb', line 29

def find_by_flag(field_name, default)
  @mappings.find { |_k, v| v[field_name] == true }&.first || default
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

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/bulkrax/csv_template/mapping_manager.rb', line 37

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



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

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