Class: Labimotion::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/labimotion/utils/serializer.rb

Class Method Summary collapse

Class Method Details

.element_properties(object) ⇒ Object



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/labimotion/utils/serializer.rb', line 59

def self.element_properties(object)
  object.properties[Labimotion::Prop::LAYERS]&.keys.each do |key|
    # layer = object.properties[key]
    field_sample_molecules = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| Labimotion::FieldType::DRAG_ALL.include?(ss['type']) }
    field_sample_molecules.each do |field|
      next unless field['value'].is_a?(Hash)

      idx = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
      sid = field.dig('value') != '' && field.dig('value', 'el_id')
      next unless sid.present?

      case field['type']
      when Labimotion::FieldType::DRAG_SAMPLE
        el = Sample.find_by(id: sid)
      when Labimotion::FieldType::DRAG_MOLECULE
        el = Molecule.find_by(id: sid)
      when Labimotion::FieldType::DRAG_ELEMENT
        el = Labimotion::Element.find_by(id: sid)
      end
      next unless el.present?
      next unless object.properties.dig(Labimotion::Prop::LAYERS, key, Labimotion::Prop::FIELDS, idx, 'value').present?

      object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_label'] = el.short_label if %w[drag_sample drag_element].include?(field['type'])
      object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_tip'] = el.short_label if %w[drag_sample].include?(field['type'])
      object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_tip'] = "#{el.element_klass&.label}@@#{el.name}" if %w[drag_element].include?(field['type'])
      object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['icon_name'] = el.element_klass&.icon_name || '' if %w[drag_element].include?(field['type'])
      object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_svg'] = field['type'] == Labimotion::FieldType::DRAG_SAMPLE ? el.get_svg_path : File.join('/images', 'molecules', el&.molecule_svg_file || 'nosvg') if Labimotion::FieldType::DRAG_MS.include?(field['type'])
      object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx]['value']['el_decoupled'] = el.decoupled if %w[drag_sample].include?(field['type'])
    end

    field_tables = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].select { |ss| ss['type'] == Labimotion::FieldType::TABLE }
    field_tables.each do |field|
      idx = object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS].index(field)
      next unless field['sub_values'].present? && field[Labimotion::Prop::SUBFIELDS].present?

      field_table_molecules = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_MOLECULE }
      object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] = set_table(field, field_table_molecules, Labimotion::Prop::MOLECULE) if field_table_molecules.present?

      field_table_samples = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_SAMPLE }
      object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] = set_table(field, field_table_samples, Labimotion::Prop::SAMPLE) if field_table_samples.present?

      field_table_elements = field[Labimotion::Prop::SUBFIELDS].select { |ss| ss['type'] == Labimotion::FieldType::DRAG_ELEMENT }
      object.properties[Labimotion::Prop::LAYERS][key][Labimotion::Prop::FIELDS][idx] = set_table_element(field, field_table_elements) if field_table_elements.present?
    end
  end
  object.properties
end

.set_table(field, field_table_objs, obj) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/labimotion/utils/serializer.rb', line 5

def self.set_table(field, field_table_objs, obj)
  col_ids = field_table_objs.map { |x| x.values[0] }
  col_ids.each do |col_id|
    field['sub_values'].each do |sub_value|
      next unless sub_value[col_id].present? && sub_value[col_id]['value'].present? && sub_value[col_id]['value']['el_id'].present?

      find_obj = obj.constantize.find_by(id: sub_value[col_id]['value']['el_id'])
      next unless find_obj.present?

      case obj
      when Labimotion::Prop::MOLECULE
        sub_value[col_id]['value']['el_svg'] = File.join('/images', 'molecules', find_obj.molecule_svg_file) if find_obj&.molecule_svg_file&.present?
        sub_value[col_id]['value']['el_inchikey'] = find_obj.inchikey
        sub_value[col_id]['value']['el_smiles'] = find_obj.cano_smiles
        sub_value[col_id]['value']['el_iupac'] = find_obj.iupac_name
        sub_value[col_id]['value']['el_molecular_weight'] = find_obj.molecular_weight
      when Labimotion::Prop::SAMPLE
        sub_value[col_id]['value']['el_svg'] = find_obj.get_svg_path
        sub_value[col_id]['value']['el_label'] = find_obj.short_label
        sub_value[col_id]['value']['el_short_label'] = find_obj.short_label
        sub_value[col_id]['value']['el_name'] = find_obj.name
        sub_value[col_id]['value']['el_external_label'] = find_obj.external_label
        sub_value[col_id]['value']['el_molecular_weight'] = find_obj.decoupled ? find_obj.molecular_mass : find_obj.molecule.molecular_weight
        sub_value[col_id]['value']['el_decoupled'] = find_obj.decoupled
      end
    end
  end
  field
end

.set_table_element(field, field_table_elements) ⇒ Object

Refresh the stored label/name of every drag_element cell of a table field. Shared by PropertiesEntity and ElnElementEntity so all three loaders agree.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/labimotion/utils/serializer.rb', line 37

def self.set_table_element(field, field_table_elements)
  col_ids = field_table_elements.map { |x| x.values[0] }
  col_ids.each do |col_id|
    field['sub_values'].each do |sub_value|
      cell = sub_value[col_id]
      next unless cell.present? && cell['value'].present? && cell['value']['el_id'].present?
      ## A drag_element cell may link a standard ELN element (reaction, wellplate, ...);
      ## el_id is then not a Labimotion::Element id, so keep what the client stored.
      next if Labimotion::LinkedElement::MODELS.key?(cell['value']['el_klass'].to_s)

      find_obj = Labimotion::Element.find_by(id: cell['value']['el_id'])
      next if find_obj.blank?

      cell['value']['el_label'] = find_obj.short_label
      cell['value']['el_name'] = find_obj.name
      cell['value']['el_tip'] = "#{find_obj.element_klass&.label}@@#{find_obj.name}"
      cell['value']['icon_name'] = find_obj.element_klass&.icon_name || ''
    end
  end
  field
end