Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Unreleased]
### Fixed
* Support Rails 8.1, Ruby 4.0. Drop support for Ruby 3.2
* Allow direct assignment of `table.table_metadata`

## 11.4.1 / 2025-11-05
### Fixed
Expand Down
6 changes: 3 additions & 3 deletions lib/ndr_import/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class Table
include NdrImport::Mapper

def self.all_valid_options
%w[canonical_name delimiter liberal_parsing filename_pattern file_password last_data_column
tablename_pattern header_lines footer_lines format klass columns slurp row_identifier
significant_mapped_fields]
%w[canonical_name delimiter liberal_parsing filename_pattern file_password
last_data_column tablename_pattern header_lines footer_lines
format klass columns slurp row_identifier significant_mapped_fields table_metadata]
end

def all_valid_options
Expand Down
6 changes: 4 additions & 2 deletions lib/ndr_import/universal_importer_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,17 @@ def table_options_from(table_mapping)

# This method does the table row yielding for the extract method, setting the notifier
# so that we can monitor progress
def yield_tables_and_their_content(filename, tables, &block)
def yield_tables_and_their_content(filename, tables, &)
return enum_for(:yield_tables_and_their_content, filename, tables) unless block_given?

tables.each do |tablename, table_content, file_metadata|
mapping = get_table_mapping(filename, tablename)
next if mapping.nil?

mapping.notifier = get_notifier(record_total(filename, table_content))
mapping.table_metadata = file_metadata || {}
# Merge file_metadata (from VCF or XML) with directly assigned table_metadata
# to form `table_metadata`
mapping.table_metadata = (mapping.table_metadata || {}).merge(file_metadata || {})
yield(mapping, table_content)
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/non_tabular/table_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_all_valid_options
valid_options = %w[
canonical_name capture_end_line capture_start_line columns end_in_a_record end_line_pattern
filename_pattern file_password format klass remove_lines row_identifier
significant_mapped_fields start_in_a_record start_line_pattern
significant_mapped_fields start_in_a_record start_line_pattern table_metadata
]
assert_equal valid_options.sort,
NdrImport::NonTabular::Table.all_valid_options.sort
Expand Down
36 changes: 36 additions & 0 deletions test/universal_importer_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,40 @@ class TestImporterWithoutNotifier

assert_equal({ record_count: '6349923' }, table_mapping.table_metadata)
end

test 'should allow assigning to table_metadata directly in table definition' do
table_mappings = [
NdrImport::Table.new(filename_pattern: /\.xls\z/i,
header_lines: 1,
footer_lines: 0,
table_metadata: { 'hello' => 'world' },
klass: 'SomeTestClass',
columns: [{ 'column' => '1a' }])
]
source_file = @permanent_test_files.join('sample_xls.xls')
@test_importer.stubs(:get_table_mapping).returns(table_mappings.first)
@test_importer.extract(source_file) do |table, _rows|
assert_instance_of NdrImport::Table, table

assert_equal({ 'hello' => 'world' }, table.table_metadata)
end
end

test 'should merge file metadata with directly assigned metadata' do
table_mapping =
NdrImport::Xml::Table.new(filename_pattern: /.xml/i,
yield_xml_record: true,
xml_record_xpath: 'BreastRecord',
format: 'xml_table',
xml_file_metadata: { record_count: '//COSD:RecordCount/@value' },
table_metadata: { hello: 'world' },
klass: 'SomeTestClass',
columns: {})

source_file = @permanent_test_files.join('complex_xml.xml')
@test_importer.stubs(:get_table_mapping).returns(table_mapping)
@test_importer.extract(source_file) { |table, rows| table.transform(rows) }

assert_equal({ record_count: '6349923', hello: 'world' }, table_mapping.table_metadata)
end
end
4 changes: 2 additions & 2 deletions test/vcf/table_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def setup

test 'test_all_valid_options' do
valid_options = %w[canonical_name columns file_password filename_pattern format klass
last_data_column liberal_parsing row_identifier
significant_mapped_fields slurp tablename_pattern vcf_file_metadata]
last_data_column liberal_parsing row_identifier significant_mapped_fields
slurp table_metadata tablename_pattern vcf_file_metadata]
assert_equal valid_options.sort, NdrImport::Vcf::Table.all_valid_options.sort
end

Expand Down