Skip to content

Commit

Permalink
refactor: allow parcing of flash point and add unit to density on sam…
Browse files Browse the repository at this point in the history
…ple export
  • Loading branch information
adambasha0 committed Nov 5, 2024
1 parent 5f3954f commit f0f7636
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/export/export_excel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ def filter_with_permission_and_detail_level(sample)
flash_point_format(sample['flash_point'])
elsif column == 'molarity'
"#{sample['molarity_value']} #{sample['molarity_unit']}"
elsif column == 'density'
"#{sample['density']} g/ml"
else
sample[column]
end
Expand Down
2 changes: 2 additions & 0 deletions lib/export/export_sdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def concatenate_data(sample, data, headers = @headers)
sample['flash_point']
when 'refractive index'
sample['refractive_index']
when 'density'
"#{sample['density']} g/mL"
else
sample[column]
end
Expand Down
7 changes: 7 additions & 0 deletions lib/export/export_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,15 @@ def generate_headers_reaction
def flash_point_format(value)
return if value.blank?

# Add quotes around unquoted keys & values
value = value.gsub(/(\w+):/, '"\1":')
value = value.gsub(/:\s*([^",{}\s]+)/, ':"\1"')

flash_point = JSON.parse(value)
"#{flash_point['value']} #{flash_point['unit']}"
rescue JSON::ParserError => e
Rails.logger.warn("Failed to parse flash_point JSON: #{e.message}")
nil
end

def format_headers(headers)
Expand Down
23 changes: 16 additions & 7 deletions lib/import/import_samples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ImportSamples
:processed, :file_path, :collection_id, :current_user_id, :file_name

MOLARITY_UNIT = %r{m/L|mol/L|M}i.freeze
DENSITY_UNIT = %r{g/mL}i.freeze
DENSITY_UNIT = %r{g/mL|g/ml}i.freeze
FLASH_POINT_UNIT = /°C|F|K/i.freeze

def initialize(file_path, collection_id, user_id, file_name, import_type)
Expand Down Expand Up @@ -265,8 +265,7 @@ def handle_flash_point(sample, value)
def handle_density(sample, value)
return sample if value[:unit].nil? || value[:value].nil?

sample['density'] = value[:value] if value[:unit] == 'g/mL'
sample
sample['density'] = value[:value] if value[:unit].match?(DENSITY_UNIT)
end

def handle_molarity(sample, value)
Expand All @@ -282,9 +281,9 @@ def handle_default_fields(sample, db_column, value)

# rubocop:disable Style/StringLiterals
def process_fields(sample, map_column, field, row, molecule)
array = ["\"cas\"", "\"purity\"", "\"density\""]
array = ["\"cas\""]
conditions = map_column.nil? || array.include?(map_column[1])
db_column = conditions ? field : map_column[0].sub('s.', '').delete!('"')
db_column = conditions ? field : (map_column[0].sub('s.', '').delete!('"') || map_column[0].sub('s.', ''))
molecule.create_molecule_name_by_user(row[field], current_user_id) if field == 'molecule name'
process_sample_fields(sample, db_column, field, row)
end
Expand Down Expand Up @@ -351,10 +350,20 @@ def to_value_unit_format(value, db_column)
end

def process_sample_fields(sample, db_column, field, row)
additional_columns = %w[cas mn.name refractive_index molarity flash_point].freeze
additional_columns = %w[
cas
mn.name
molarity
refractive_index
flash_point
density
location
melting_point
purity
].freeze
return unless included_fields.include?(db_column) || additional_columns.include?(db_column)

excluded_column = %w[description solvent location].freeze
excluded_column = %w[description solvent].freeze
val = row[field]
value = process_value(val, db_column)
handle_sample_fields(sample, db_column, value) unless value.nil?
Expand Down

0 comments on commit f0f7636

Please sign in to comment.