Module: Unitsdb::Commands::Qudt::Updater
- Defined in:
- lib/unitsdb/commands/qudt/updater.rb
Overview
Updater for adding QUDT references to UnitsDB entities
Constant Summary collapse
- QUDT_AUTHORITY =
"qudt"
Class Method Summary collapse
-
.get_entity_id(entity) ⇒ Object
Get entity ID (either from identifiers array or directly).
-
.get_original_yaml_file(_db_entities, output_file) ⇒ Object
Get the original YAML file path.
-
.manually_verified?(_entity) ⇒ Boolean
Check if an entity has been manually verified (has a special flag) Unitsdb::ExternalReference carries no
verifiedflag in the 2.0 schema, so nothing is ever manually verified. -
.preserve_schema_header(original_file, yaml_content) ⇒ Object
Preserve existing schema header or add default one.
-
.update_references(entity_type, matches, db_entities, output_file, include_potential = false) ⇒ Object
Update references in UnitsDB entities with QUDT references.
-
.write_yaml_file(output_file, output_data) ⇒ Object
Helper to write YAML file.
Class Method Details
.get_entity_id(entity) ⇒ Object
Get entity ID (either from identifiers array or directly)
170 171 172 173 174 |
# File 'lib/unitsdb/commands/qudt/updater.rb', line 170 def get_entity_id(entity) return entity.identifiers.first.id unless entity.identifiers.empty? entity.short end |
.get_original_yaml_file(_db_entities, output_file) ⇒ Object
Get the original YAML file path
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/unitsdb/commands/qudt/updater.rb', line 135 def get_original_yaml_file(_db_entities, output_file) # The database path should be available from the update command # We need to construct the path to the original YAML file entity_type = File.basename(output_file, ".yaml") # Try to find the original file in the database directory # Look for it relative to where we expect it to be # nil # Try to get database directory from environment or assume it's the fixtures database_dir = ENV["UNITSDB_DATABASE_PATH"] || File.join(File.dirname(__FILE__), "../../../data") original_yaml_file = File.join(database_dir, "#{entity_type}.yaml") # If that doesn't exist, try to find it relative to the current working directory unless File.exist?(original_yaml_file) original_yaml_file = File.join("data", "#{entity_type}.yaml") end # If still not found, create a fallback unless File.exist?(original_yaml_file) puts "Warning: Could not find original YAML file. Creating empty template." original_yaml_file = output_file FileUtils.mkdir_p(File.dirname(original_yaml_file)) File.write(original_yaml_file, { entity_type => [] }.to_yaml) end puts "Using original YAML file: #{original_yaml_file}" original_yaml_file end |
.manually_verified?(_entity) ⇒ Boolean
Check if an entity has been manually verified (has a special flag)
Unitsdb::ExternalReference carries no verified flag in the
2.0 schema, so nothing is ever manually verified. Kept as a
stub for callers; returns false unconditionally.
180 181 182 |
# File 'lib/unitsdb/commands/qudt/updater.rb', line 180 def manually_verified?(_entity) false end |
.preserve_schema_header(original_file, yaml_content) ⇒ Object
Preserve existing schema header or add default one
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/unitsdb/commands/qudt/updater.rb', line 109 def preserve_schema_header(original_file, yaml_content) schema_header = nil # Extract existing schema header if file exists if File.exist?(original_file) original_content = File.read(original_file) if (match = original_content.match(/^# yaml-language-server: \$schema=.+$/)) schema_header = match[0] end end # Remove any existing schema header from new content to avoid duplication yaml_content = yaml_content.gsub( /^# yaml-language-server: \$schema=.+$\n/, "" ) # Add preserved or default schema header if schema_header "#{schema_header}\n#{yaml_content}" else entity_type = File.basename(original_file, ".yaml") "# yaml-language-server: $schema=schemas/#{entity_type}-schema.yaml\n#{yaml_content}" end end |
.update_references(entity_type, matches, db_entities, output_file, include_potential = false) ⇒ Object
Update references in UnitsDB entities with QUDT references
16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/unitsdb/commands/qudt/updater.rb', line 16 def update_references(entity_type, matches, db_entities, output_file, include_potential = false) puts "Updating QUDT references for #{entity_type}..." # Get the original YAML file path from the database entities original_yaml_file = get_original_yaml_file(db_entities, output_file) # Load the original YAML file as plain data structures yaml_content = File.read(original_yaml_file) output_data = YAML.safe_load(yaml_content) # Create a map of entity IDs to their QUDT references entity_references = {} # Process each match matches.each do |match| db_entity = match[:db_entity] qudt_entity = match[:qudt_entity] # Skip potential matches unless specified next if match[:potential] && !include_potential # Skip if entity has been manually verified if manually_verified?(db_entity) puts "Skipping manually verified entity: #{get_entity_id(db_entity)}" next end # Get entity ID entity_id = get_entity_id(db_entity) next unless entity_id # Store reference data as plain hash entity_references[entity_id] = { "uri" => qudt_entity.uri, "type" => "informative", "authority" => QUDT_AUTHORITY, } end # Update the YAML content output_data[entity_type].each do |entity_yaml| # Find entity by ID entity_id = if entity_yaml["identifiers"] begin entity_yaml["identifiers"].first["id"] rescue StandardError nil end end next unless entity_id && entity_references.key?(entity_id) # Initialize references array if it doesn't exist entity_yaml["references"] ||= [] # Add new references if (ext_ref = entity_references[entity_id]) if entity_yaml["references"].any? do |ref| ref["uri"] == ext_ref["uri"] && ref["authority"] == ext_ref["authority"] end # Skip if reference already exists puts "Reference already exists for entity ID: #{entity_id}" else # Add the reference puts "Adding reference for entity ID: #{entity_id}, URI: #{ext_ref['uri']}, Authority: #{ext_ref['authority']}" entity_yaml["references"] << ext_ref end end end # Write to YAML file write_yaml_file(output_file, output_data) puts "Added #{entity_references.values.size} QUDT references to #{entity_type}" end |
.write_yaml_file(output_file, output_data) ⇒ Object
Helper to write YAML file
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/unitsdb/commands/qudt/updater.rb', line 94 def write_yaml_file(output_file, output_data) # Ensure the output directory exists output_dir = File.dirname(output_file) FileUtils.mkdir_p(output_dir) # Write to YAML file with proper formatting yaml_content = output_data.to_yaml # Preserve existing schema header or add default one yaml_content = preserve_schema_header(output_file, yaml_content) File.write(output_file, yaml_content) end |