Class: Labimotion::ExportElement
- Inherits:
-
Object
- Object
- Labimotion::ExportElement
- Defined in:
- lib/labimotion/libs/export_element.rb
Constant Summary collapse
- TABLE_BLANK_WORDML =
'<w:p/>'- TABLE_BORDER =
'<w:tblBorders>' \ '<w:top w:val="single" w:sz="4" w:color="auto"/>' \ '<w:left w:val="single" w:sz="4" w:color="auto"/>' \ '<w:bottom w:val="single" w:sz="4" w:color="auto"/>' \ '<w:right w:val="single" w:sz="4" w:color="auto"/>' \ '<w:insideH w:val="single" w:sz="4" w:color="auto"/>' \ '<w:insideV w:val="single" w:sz="4" w:color="auto"/>' \ '</w:tblBorders>'
- DATETIME_RANGE_HEADERS =
['Start', 'Stop', 'Duration (calc)', 'Duration'].freeze
- DATETIME_RANGE_PRECISE_LABELS =
%w[year month day hour minute second].freeze
- DURATION_UNIT_LABELS =
{ 'd' => 'day', 'h' => 'hour', 'min' => 'minute', 's' => 'second' }.freeze
- TABLE_CELL_TEXT_RENDERERS =
{ Labimotion::FieldType::DRAG_SAMPLE => ->(_sf, c, ctx) { ctx.table_cell_sample_text(c['value'] || {}) }, Labimotion::FieldType::DRAG_MOLECULE => ->(_sf, c, ctx) { ctx.table_cell_molecule_text(c['value'] || {}) }, Labimotion::FieldType::SELECT => ->(_sf, c, _ctx) { c['value'].to_s }, Labimotion::FieldType::SYSTEM_DEFINED => ->(sf, c, ctx) { ctx.table_cell_sysdef_text(sf, c) } }.freeze
Instance Method Summary collapse
- #borrow_unit(higher, lower, base) ⇒ Object
- #build_datetime_range_wordml(field) ⇒ Object
- #build_field(layer, field) ⇒ Object
- #build_field_html(layer, field, cols) ⇒ Object
- #build_fields(layer) ⇒ Object
- #build_fields_html(layer) ⇒ Object
- #build_layers ⇒ Object
- #build_layers_html ⇒ Object
- #build_table_body_rows(sub_values, sub_fields, width, font_size) ⇒ Object
- #build_table_field(sub_val, sub_field) ⇒ Object
- #build_table_header_row(sub_fields, width, font_size) ⇒ Object
- #build_table_wordml(field) ⇒ Object
- #build_utc_from_parts(parts) ⇒ Object
- #build_wordml_cell(text, width, font_size, bold) ⇒ Object
- #compute_duration_calc(time_start, time_stop) ⇒ Object
- #datetime_range_parts_complete?(parts) ⇒ Boolean
- #datetime_range_values(by_col) ⇒ Object
- #datetime_range_wordml_table(values) ⇒ Object
- #duration_unit_label(value_system, value) ⇒ Object
- #format_duration_calc(_duration_calc_sf, time_start, time_stop) ⇒ Object
- #format_duration_value(duration_sf) ⇒ Object
- #format_precise_part(count, label) ⇒ Object
- #html_labimotion ⇒ Object
-
#initialize(current_user, element, export_format) ⇒ ExportElement
constructor
A new instance of ExportElement.
- #parse_datetime_range_value(str) ⇒ Object
- #pluralize_unit?(value) ⇒ Boolean
- #precise_diff_components(start_t, stop_t) ⇒ Object
- #precise_diff_humanize(start_t, stop_t) ⇒ Object
- #precise_diff_raw(start_t, stop_t) ⇒ Object
- #res_name ⇒ Object
- #sample_url ⇒ Object
- #table_cell_molecule_text(val) ⇒ Object
- #table_cell_sample_text(val) ⇒ Object
- #table_cell_sysdef_text(sub_field, cell) ⇒ Object
- #table_cell_text(sub_val, sub_field) ⇒ Object
- #to_docx ⇒ Object
- #xml_escape(str) ⇒ Object
Constructor Details
#initialize(current_user, element, export_format) ⇒ ExportElement
Returns a new instance of ExportElement.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/labimotion/libs/export_element.rb', line 11 def initialize(current_user, element, export_format) @current_user = current_user @element = element @parent = case element.class.name when 'Labimotion::Segment' element&.element when 'Labimotion::Dataset' element&.element&.root_element end @element_klass = case element.class.name when 'Labimotion::Element' element.element_klass when 'Labimotion::Segment' element.segment_klass when 'Labimotion::Dataset' element.dataset_klass end @name = @element.instance_of?(Labimotion::Element) ? element.name : @parent&.name @short_label = @element.instance_of?(Labimotion::Element) ? element.short_label : @parent&.short_label @element_name = "#{@element_klass.label}_#{@short_label}".gsub(/\s+/, '') @properties = element.properties @options = element.properties_release[Labimotion::Prop::SEL_OPTIONS] @export_format = export_format rescue StandardError => e Labimotion.log_exception(e) end |
Instance Method Details
#borrow_unit(higher, lower, base) ⇒ Object
352 353 354 355 356 |
# File 'lib/labimotion/libs/export_element.rb', line 352 def borrow_unit(higher, lower, base) return [higher, lower] unless lower.negative? [higher - 1, lower + base] end |
#build_datetime_range_wordml(field) ⇒ Object
239 240 241 242 243 244 245 246 247 |
# File 'lib/labimotion/libs/export_element.rb', line 239 def build_datetime_range_wordml(field) sub_fields = field.fetch('sub_fields', []) || [] by_col = sub_fields.each_with_object({}) { |sf, h| h[sf['col_name']] = sf if sf.is_a?(Hash) } values = datetime_range_values(by_col) datetime_range_wordml_table(values) rescue StandardError => e Labimotion.log_exception(e) Sablon.content(:word_ml, TABLE_BLANK_WORDML) end |
#build_field(layer, field) ⇒ Object
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 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 168 169 |
# File 'lib/labimotion/libs/export_element.rb', line 75 def build_field(layer, field) field_obj = {} field_obj[:label] = field['label'] field_obj[:field] = field['field'] field_obj[:type] = field['type'] field_obj.merge!(field.slice('value', 'value_system')) field_obj[:is_table] = false field_obj[:not_table] = true case field['type'] when Labimotion::FieldType::DRAG_ELEMENT field_obj[:value] = (field['value'] && field['value']['el_label']) || '' field_obj[:obj] = field['value'] ### change to object when Labimotion::FieldType::DRAG_SAMPLE val = field.fetch('value', nil) if val.present? instance = Sample.find_by(id: val['el_id']) field_obj[:value] = val['el_label'] field_obj[:has_structure] = true obj_os = Entities::SampleReportEntity.new( instance, current_user: @current_user, detail_levels: ElementDetailLevelCalculator.new(user: @current_user, element: instance).detail_levels, ).serializable_hash obj_final = OpenStruct.new(obj_os) field_obj[:structure] = Reporter::Docx::DiagramSample.new(obj: obj_final, format: 'png').generate end when Labimotion::FieldType::SYS_REACTION val = field.fetch('value', nil) if val.present? instance = Reaction.find_by(id: val['el_id']) field_obj[:value] = val['el_label'] field_obj[:has_structure] = true obj_os = Entities::ReactionReportEntity.new( instance, current_user: @current_user, detail_levels: ElementDetailLevelCalculator.new(user: @current_user, element: instance).detail_levels, ).serializable_hash obj_final = OpenStruct.new(obj_os) field_obj[:structure] = Reporter::Docx::DiagramReaction.new(obj: obj_final, format: 'png').generate end when Labimotion::FieldType::DRAG_MOLECULE val = field.fetch('value', nil) if val.present? obj = Molecule.find_by(id: val['el_id']) field_obj[:value] = val['el_label'] end when Labimotion::FieldType::SELECT field_obj[:value] = @options.fetch(field['option_layers'], nil)&.fetch('options', nil)&.find { |ss| ss['key'] == field['value'] }&.fetch('label', nil) || field['value'] when Labimotion::FieldType::UPLOAD files = field.fetch('value', nil)&.fetch('files', []) val = files&.map { |file| "#{file['filename']} #{file['label']}" }&.join('\n') field_obj[:value] = val when Labimotion::FieldType::TABLE field_obj[:is_table] = false field_obj[:not_table] = true field_obj[:value] = build_table_wordml(field) when Labimotion::FieldType::DATETIME_RANGE field_obj[:is_table] = false field_obj[:not_table] = true field_obj[:value] = build_datetime_range_wordml(field) when Labimotion::FieldType::INPUT_GROUP val = [] field.fetch('sub_fields', [])&.each do |sub_field| if sub_field['type'] == Labimotion::FieldType::SYSTEM_DEFINED val.push("#{sub_field['value']} #{sub_field['value_system']}") else val.push(sub_field['value']) end end field_obj[:value] = val.join(' ') when Labimotion::FieldType::WF_NEXT if field['value'].present? && field['wf_options'].present? field_obj[:value] = field['wf_options'].find { |ss| ss['key'] == field['value'] }&.fetch('label', nil)&.split('(')&.first&.strip end when Labimotion::FieldType::SYSTEM_DEFINED _field = field unit = Labimotion::Units::FIELDS.find { |o| o[:field] == _field['option_layers'] }&.fetch(:units, []).find { |u| u[:key] == _field['value_system'] }&.fetch(:label, '') val = _field['value'].to_s + ' ' + unit val = Sablon.content(:html, "<div>" + val + "</div>") if val.include? '<' field_obj[:value] = val when Labimotion::FieldType::TEXT_FORMULA field.fetch('text_sub_fields', []).each do |sub_field| va = @properties['layers'][sub_field.fetch('layer','')]['fields'].find { |f| f['field'] == sub_field.fetch('field','') }&.fetch('value',nil) field_obj[:value] = (field_obj[:value] || '') + va.to_s + sub_field.fetch('separator','') if va.present? end # field_obj[:value] = (field['value'] && field['value']['el_label']) || '' # field_obj[:obj] = field['value'] ### change to object else field_obj[:value] = field['value'] end field_obj rescue StandardError => e Labimotion.log_exception(e) end |
#build_field_html(layer, field, cols) ⇒ Object
489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
# File 'lib/labimotion/libs/export_element.rb', line 489 def build_field_html(layer, field, cols) case field['type'] when Labimotion::FieldType::DRAG_SAMPLE, Labimotion::FieldType::DRAG_ELEMENT, Labimotion::FieldType::DRAG_MOLECULE val = (field['value'] && field['value']['el_label']) || '' when Labimotion::FieldType::UPLOAD val = (field['value'] && field['value']['files'] && field['value']['files'].first && field['value']['files'].first['filename'] ) || '' else val = field['value'] end htd = field['hasOwnRow'] == true ? "<td colspan=#{cols}>" : '<td>' "#{htd}<b>#{field['label']}: </b><br />#{val}</td>" rescue StandardError => e Labimotion.log_exception(e) end |
#build_fields(layer) ⇒ Object
439 440 441 442 443 444 445 446 447 448 449 450 451 |
# File 'lib/labimotion/libs/export_element.rb', line 439 def build_fields(layer) fields = layer[Labimotion::Prop::FIELDS] || [] field_objs = [] fields.each do |field| next if field['type'] == 'dummy' field_obj = build_field(layer, field) field_objs.push(field_obj) end field_objs rescue StandardError => e Labimotion.log_exception(e) end |
#build_fields_html(layer) ⇒ Object
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 |
# File 'lib/labimotion/libs/export_element.rb', line 504 def build_fields_html(layer) fields = layer[Labimotion::Prop::FIELDS] || [] field_objs = [] cols = layer['cols'] || 0 field_objs.push('<table style="width: 4000dxa"><tr>') fields.each do |field| if cols&.zero? || field['hasOwnRow'] == true field_objs.push('</tr><tr>') cols = layer['cols'] end field_obj = build_field_html(layer, field, layer['cols']) field_objs.push(field_obj) cols -= 1 cols = 0 if field['hasOwnRow'] == true end field_objs.push('</tr></table>') field_objs&.join("")&.gsub('<tr></tr>', '') rescue StandardError => e Labimotion.log_exception(e) end |
#build_layers ⇒ Object
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 |
# File 'lib/labimotion/libs/export_element.rb', line 46 def build_layers objs = [] @properties[Labimotion::Prop::LAYERS]&.keys&.sort_by do |key| [ @properties[Labimotion::Prop::LAYERS].fetch(key, nil)&.fetch('position', 0) || 0, @properties[Labimotion::Prop::LAYERS].fetch(key, nil)&.fetch('wf_position', 0) || 0 ] end&.each do |key| layer = @properties[Labimotion::Prop::LAYERS][key] || {} ## Build fields html # field_objs = build_fields_html(layer) if layer[Labimotion::Prop::FIELDS]&.length&.positive? # field_html = Sablon.content(:html, field_objs) if field_objs.present? field_objs = build_fields(layer) layer_info = { label: layer['label'], layer: layer['layer'], cols: layer['cols'], timeRecord: layer['timeRecord'], fields: field_objs } objs.push(layer_info) end objs rescue StandardError => e Labimotion.log_exception(e) end |
#build_layers_html ⇒ Object
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
# File 'lib/labimotion/libs/export_element.rb', line 526 def build_layers_html layer_html = [] first_line = true @properties[Labimotion::Prop::LAYERS]&.keys&.each do |key| layer_html.push('</tr></table>') if first_line == false layer = @properties[Labimotion::Prop::LAYERS][key] || {} fields = layer[Labimotion::Prop::FIELDS] || [] layer_html.push("<h2><b>Layer:#{layer['label']}</b></h2>") layer_html.push('<table><tr>') cols = layer['cols'] fields.each do |field| if (cols === 0) layer_html.push('</tr><tr>') end val = field['value'].is_a?(Hash) ? field['value']['el_label'] : field['value'] layer_html.push("<td>#{field['label']}: <br />#{val}</td>") cols -= 1 end first_line = false end layer_html.push('</tr></table>') layer_html.join('') rescue StandardError => e Labimotion.log_exception(e) end |
#build_table_body_rows(sub_values, sub_fields, width, font_size) ⇒ Object
374 375 376 377 378 379 |
# File 'lib/labimotion/libs/export_element.rb', line 374 def build_table_body_rows(sub_values, sub_fields, width, font_size) sub_values.map do |sv| cells = sub_fields.map { |sf| build_wordml_cell(table_cell_text(sv, sf), width, font_size, false) }.join "<w:tr>#{cells}</w:tr>" end.join end |
#build_table_field(sub_val, sub_field) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/labimotion/libs/export_element.rb', line 171 def build_table_field(sub_val, sub_field) return '' if sub_field.fetch('id', nil).nil? || sub_val[sub_field['id']].nil? case sub_field['type'] when Labimotion::FieldType::DRAG_SAMPLE val = sub_val[sub_field['id']]['value'] || {} label = val['el_label'].present? ? "Short Label: [#{val['el_label']}] \n" : '' name = val['el_name'].present? ? "Name: [#{val['el_name']}] \n" : '' ext = val['el_external_label'].present? ? "Ext. Label: [#{val['el_external_label']}] \n" : '' mass = val['el_molecular_weight'].present? ? "Mass: [#{val['el_molecular_weight']}] \n" : '' url = val['el_id'].present? ? "#{sample_url}/#{val['el_id']}" : '' "#{label}#{name}#{ext}#{mass}#{url}" when Labimotion::FieldType::DRAG_MOLECULE val = sub_val[sub_field['id']]['value'] || {} smile = val['el_smiles'].present? ? "SMILES: [#{val['el_smiles']}] \n" : '' inchikey = val['el_inchikey'].present? ? "InChiKey:[#{val['el_inchikey']}] \n" : '' iupac = val['el_iupac'].present? ? "IUPAC:[#{val['el_iupac']}] \n" : '' mass = val['el_molecular_weight'].present? ? "MASS: [#{val['el_molecular_weight']}] \n" : '' "#{smile}#{inchikey}#{iupac}#{mass}" when Labimotion::FieldType::SELECT sub_val[sub_field['id']]['value'] when Labimotion::FieldType::SYSTEM_DEFINED unit = Labimotion::Units::FIELDS.find { |o| o[:field] == sub_field['option_layers'] }&.fetch(:units, [])&.find { |u| u[:key] == sub_val[sub_field['id']]['value_system'] }&.fetch(:label, '') val = sub_val[sub_field['id']]['value'].to_s + ' ' + unit val = Sablon.content(:html, "<div>" + val + "</div>") if val.include? '<' val else sub_val[sub_field['id']] end end |
#build_table_header_row(sub_fields, width, font_size) ⇒ Object
369 370 371 372 |
# File 'lib/labimotion/libs/export_element.rb', line 369 def build_table_header_row(sub_fields, width, font_size) cells = sub_fields.map { |sf| build_wordml_cell(sf['col_name'].to_s, width, font_size, true) }.join "<w:tr>#{cells}</w:tr>" end |
#build_table_wordml(field) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/labimotion/libs/export_element.rb', line 212 def build_table_wordml(field) sub_fields = field.fetch('sub_fields', []) return Sablon.content(:word_ml, TABLE_BLANK_WORDML) if sub_fields.empty? width = (9000.0 / sub_fields.length).round font_size = sub_fields.length > 6 ? 16 : 20 grid = sub_fields.map { %(<w:gridCol w:w="#{width}"/>) }.join header = build_table_header_row(sub_fields, width, font_size) rows = build_table_body_rows(field.fetch('sub_values', []), sub_fields, width, font_size) tbl = '<w:tbl><w:tblPr><w:tblW w:w="5000" w:type="pct"/>' \ "#{TABLE_BORDER}</w:tblPr><w:tblGrid>#{grid}</w:tblGrid>" \ "#{header}#{rows}</w:tbl>" Sablon.content(:word_ml, tbl) rescue StandardError => e Labimotion.log_exception(e) Sablon.content(:word_ml, TABLE_BLANK_WORDML) end |
#build_utc_from_parts(parts) ⇒ Object
321 322 323 324 |
# File 'lib/labimotion/libs/export_element.rb', line 321 def build_utc_from_parts(parts) Time.utc(parts[:year], parts[:mon], parts[:mday], parts[:hour] || 0, parts[:min] || 0, parts[:sec] || 0) end |
#build_wordml_cell(text, width, font_size, bold) ⇒ Object
381 382 383 384 385 386 387 388 389 390 |
# File 'lib/labimotion/libs/export_element.rb', line 381 def build_wordml_cell(text, width, font_size, bold) bold_xml = bold ? '<w:b/>' : '' rpr = "<w:rPr>#{bold_xml}<w:sz w:val=\"#{font_size}\"/></w:rPr>" lines = text.to_s.split(/\r?\n/) lines = [''] if lines.empty? paragraphs = lines.map do |line| "<w:p><w:r>#{rpr}<w:t xml:space=\"preserve\">#{xml_escape(line)}</w:t></w:r></w:p>" end.join "<w:tc><w:tcPr><w:tcW w:w=\"#{width}\" w:type=\"dxa\"/></w:tcPr>#{paragraphs}</w:tc>" end |
#compute_duration_calc(time_start, time_stop) ⇒ Object
297 298 299 300 301 302 303 |
# File 'lib/labimotion/libs/export_element.rb', line 297 def compute_duration_calc(time_start, time_stop) start_t = parse_datetime_range_value(time_start) stop_t = parse_datetime_range_value(time_stop) return '' unless start_t && stop_t && stop_t > start_t precise_diff_humanize(start_t, stop_t) end |
#datetime_range_parts_complete?(parts) ⇒ Boolean
317 318 319 |
# File 'lib/labimotion/libs/export_element.rb', line 317 def datetime_range_parts_complete?(parts) %i[year mon mday].all? { |k| parts[k] } end |
#datetime_range_values(by_col) ⇒ Object
249 250 251 252 253 254 255 256 257 258 |
# File 'lib/labimotion/libs/export_element.rb', line 249 def datetime_range_values(by_col) time_start = by_col['timeStart']&.dig('value').to_s time_stop = by_col['timeStop']&.dig('value').to_s [ time_start, time_stop, format_duration_calc(by_col['durationCalc'], time_start, time_stop), format_duration_value(by_col['duration']) ] end |
#datetime_range_wordml_table(values) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/labimotion/libs/export_element.rb', line 260 def datetime_range_wordml_table(values) width = (9000.0 / DATETIME_RANGE_HEADERS.length).round font_size = 20 grid = DATETIME_RANGE_HEADERS.map { %(<w:gridCol w:w="#{width}"/>) }.join header_cells = DATETIME_RANGE_HEADERS.map { |h| build_wordml_cell(h, width, font_size, true) }.join body_cells = values.map { |v| build_wordml_cell(v, width, font_size, false) }.join tbl = '<w:tbl><w:tblPr><w:tblW w:w="5000" w:type="pct"/>' \ "#{TABLE_BORDER}</w:tblPr><w:tblGrid>#{grid}</w:tblGrid>" \ "<w:tr>#{header_cells}</w:tr><w:tr>#{body_cells}</w:tr></w:tbl>" Sablon.content(:word_ml, tbl) end |
#duration_unit_label(value_system, value) ⇒ Object
279 280 281 282 283 284 |
# File 'lib/labimotion/libs/export_element.rb', line 279 def duration_unit_label(value_system, value) base = DURATION_UNIT_LABELS[value_system.to_s] return value_system.to_s if base.nil? pluralize_unit?(value) ? "#{base}s" : base end |
#format_duration_calc(_duration_calc_sf, time_start, time_stop) ⇒ Object
293 294 295 |
# File 'lib/labimotion/libs/export_element.rb', line 293 def format_duration_calc(_duration_calc_sf, time_start, time_stop) compute_duration_calc(time_start, time_stop) end |
#format_duration_value(duration_sf) ⇒ Object
272 273 274 275 276 277 |
# File 'lib/labimotion/libs/export_element.rb', line 272 def format_duration_value(duration_sf) val = duration_sf&.dig('value') return '' if val.nil? || val.to_s.empty? "#{val} #{duration_unit_label(duration_sf['value_system'], val)}".strip end |
#format_precise_part(count, label) ⇒ Object
334 335 336 337 |
# File 'lib/labimotion/libs/export_element.rb', line 334 def format_precise_part(count, label) suffix = count == 1 ? '' : 's' "#{count} #{label}#{suffix}" end |
#html_labimotion ⇒ Object
553 554 555 556 557 558 559 560 561 |
# File 'lib/labimotion/libs/export_element.rb', line 553 def html_labimotion layers_html = build_layers_html html_body = <<-HTML.strip #{layers_html} HTML html_body rescue StandardError => e Labimotion.log_exception(e) end |
#parse_datetime_range_value(str) ⇒ Object
305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/labimotion/libs/export_element.rb', line 305 def parse_datetime_range_value(str) cleaned = str.to_s.strip return nil if cleaned.empty? parts = Date._parse(cleaned) return nil unless datetime_range_parts_complete?(parts) build_utc_from_parts(parts) rescue ArgumentError nil end |
#pluralize_unit?(value) ⇒ Boolean
286 287 288 289 290 291 |
# File 'lib/labimotion/libs/export_element.rb', line 286 def pluralize_unit?(value) numeric = Float(value.to_s) (numeric - 1.0).abs > Float::EPSILON rescue ArgumentError, TypeError true end |
#precise_diff_components(start_t, stop_t) ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/labimotion/libs/export_element.rb', line 339 def precise_diff_components(start_t, stop_t) y, m, d, h, mi, s = precise_diff_raw(start_t, stop_t) mi, s = borrow_unit(mi, s, 60) h, mi = borrow_unit(h, mi, 60) d, h = borrow_unit(d, h, 24) if d.negative? m -= 1 d += (Date.new(stop_t.year, stop_t.month, 1) - 1).day end y, m = borrow_unit(y, m, 12) [y, m, d, h, mi, s] end |
#precise_diff_humanize(start_t, stop_t) ⇒ Object
326 327 328 329 330 331 332 |
# File 'lib/labimotion/libs/export_element.rb', line 326 def precise_diff_humanize(start_t, stop_t) components = precise_diff_components(start_t, stop_t) parts = components.zip(DATETIME_RANGE_PRECISE_LABELS).reject { |n, _| n <= 0 } return '0 seconds' if parts.empty? parts.map { |n, label| format_precise_part(n, label) }.join(' ') end |
#precise_diff_raw(start_t, stop_t) ⇒ Object
358 359 360 361 362 363 364 365 366 367 |
# File 'lib/labimotion/libs/export_element.rb', line 358 def precise_diff_raw(start_t, stop_t) [ stop_t.year - start_t.year, stop_t.month - start_t.month, stop_t.day - start_t.day, stop_t.hour - start_t.hour, stop_t.min - start_t.min, stop_t.sec - start_t.sec ] end |
#res_name ⇒ Object
483 484 485 486 487 |
# File 'lib/labimotion/libs/export_element.rb', line 483 def res_name "#{@element_name}_#{Time.now.strftime('%Y%m%d%H%M')}.docx" rescue StandardError => e Labimotion.log_exception(e) end |
#sample_url ⇒ Object
40 41 42 43 44 |
# File 'lib/labimotion/libs/export_element.rb', line 40 def sample_url host = ENV['PUBLIC_URL'] || 'http://localhost:3000' api = 'mydb/collection/all/sample' "#{host}/#{api}" end |
#table_cell_molecule_text(val) ⇒ Object
424 425 426 427 428 429 430 431 |
# File 'lib/labimotion/libs/export_element.rb', line 424 def table_cell_molecule_text(val) parts = [] parts << "SMILES: [#{val['el_smiles']}]" if val['el_smiles'].present? parts << "InChiKey: [#{val['el_inchikey']}]" if val['el_inchikey'].present? parts << "IUPAC: [#{val['el_iupac']}]" if val['el_iupac'].present? parts << "MASS: [#{val['el_molecular_weight']}]" if val['el_molecular_weight'].present? parts.join("\n") end |
#table_cell_sample_text(val) ⇒ Object
414 415 416 417 418 419 420 421 422 |
# File 'lib/labimotion/libs/export_element.rb', line 414 def table_cell_sample_text(val) parts = [] parts << "Short Label: [#{val['el_label']}]" if val['el_label'].present? parts << "Name: [#{val['el_name']}]" if val['el_name'].present? parts << "Ext. Label: [#{val['el_external_label']}]" if val['el_external_label'].present? parts << "Mass: [#{val['el_molecular_weight']}]" if val['el_molecular_weight'].present? parts << "#{sample_url}/#{val['el_id']}" if val['el_id'].present? parts.join("\n") end |
#table_cell_sysdef_text(sub_field, cell) ⇒ Object
433 434 435 436 437 |
# File 'lib/labimotion/libs/export_element.rb', line 433 def table_cell_sysdef_text(sub_field, cell) fdef = Labimotion::Units::FIELDS.find { |o| o[:field] == sub_field['option_layers'] } unit = fdef&.fetch(:units, [])&.find { |u| u[:key] == cell['value_system'] }&.fetch(:label, '') "#{cell['value']} #{unit}".strip end |
#table_cell_text(sub_val, sub_field) ⇒ Object
403 404 405 406 407 408 409 410 411 412 |
# File 'lib/labimotion/libs/export_element.rb', line 403 def table_cell_text(sub_val, sub_field) return '' if sub_field.fetch('id', nil).nil? || sub_val[sub_field['id']].nil? cell = sub_val[sub_field['id']] renderer = TABLE_CELL_TEXT_RENDERERS[sub_field['type']] return renderer.call(sub_field, cell, self) if renderer raw = cell.is_a?(Hash) ? cell['value'] : cell raw.to_s end |
#to_docx ⇒ Object
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'lib/labimotion/libs/export_element.rb', line 453 def to_docx # location = Rails.root.join('lib', 'template', 'Labimotion.docx') location = Rails.root.join('lib', 'template', 'Labimotion_lines.docx') # location = Rails.root.join('lib', 'template', 'Labimotion_img.docx') File.exist?(location) template = Sablon.template(location) layers = build_layers context = { label: @element_klass.label, desc: @element_klass.desc, name: @name, parent_klass: @parent.present? ? "#{@parent&.class&.name&.split('::')&.last}: " : '', short_label: @short_label, date: Time.now.strftime('%d/%m/%Y'), author: @current_user.name, layers: layers } tempfile = Tempfile.new('labimotion.docx') template.render_to_file File.(tempfile), context content = File.read(tempfile) content rescue StandardError => e Labimotion.log_exception(e) ensure # Close and delete the temporary file tempfile&.close tempfile&.unlink end |
#xml_escape(str) ⇒ Object
392 393 394 |
# File 'lib/labimotion/libs/export_element.rb', line 392 def xml_escape(str) str.to_s.gsub('&', '&').gsub('<', '<').gsub('>', '>') end |