Module: Eco::API::UseCases::OozeSamples::Helpers::OozeHandlers
- Included in:
- ExportableOoze, ExportableRegister
- Defined in:
- lib/eco/api/usecases/ooze_samples/helpers/ooze_handlers.rb
Instance Method Summary collapse
- #array_indexes(ary) ⇒ Object
-
#merge_arrays(ref, plus) ⇒ Object
Merges arrays
ref
andplus
by keeping the order ofref
but inserting additional elements ofplus
in the correct position (i.e. when those elements are between two elements that exist inref
). -
#merge_values(origin, append, klass: nil, delimiter: "\n") ⇒ Object
Helper to merge values of fields of same type and label name.
Instance Method Details
#array_indexes(ary) ⇒ Object
85 86 87 |
# File 'lib/eco/api/usecases/ooze_samples/helpers/ooze_handlers.rb', line 85 def array_indexes(ary) ary.each_with_index.to_h end |
#merge_arrays(ref, plus) ⇒ Object
Note:
this can be used to combine two different header sequences
by using ref
as a reference.
Merges arrays ref
and plus
by keeping the order of ref
but inserting additional elements of plus
in the correct position
(i.e. when those elements are between two elements that exist in ref
)
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 |
# File 'lib/eco/api/usecases/ooze_samples/helpers/ooze_handlers.rb', line 51 def merge_arrays(ref, plus) # rubocop:disable Metrics/AbcSize return ref if ref == plus href_idx = array_indexes(ref) hplus_idx = array_indexes(plus) shared = ref & plus hshared_ref_idx = href_idx.slice(*shared) # hidx_plus_shared = hplus_idx.slice(*shared).each_with_object({}) do |(e, idx), reversed| # reversed[idx] = e # end # shared_plus_idxs = hidx_plus_shared.keys.reverse shared_plus_idxs = hplus_idx.values_at(*shared).sort.reverse only_plus = plus - ref honly_plus_idx = hplus_idx.slice(*only_plus) hplus_ref_idx = honly_plus_idx.each_with_object({}) do |(e, idx), href_idx_plus| ps_idx = shared_plus_idxs.bsearch {|i| idx >= i} rs_idx = ps_idx ? hshared_ref_idx[plus[ps_idx]] : -1 href_idx_plus[e] = rs_idx end.group_by do |_e, idx| idx end.to_a.sort_by do |(idx, _pairs)| idx end.reverse.to_h.transform_values do |pairs| pairs.map(&:first) end hplus_ref_idx.each_with_object(ref.dup) do |(idx, elements), rf| rf.insert(idx + 1, *elements) end end |
#merge_values(origin, append, klass: nil, delimiter: "\n") ⇒ Object
Helper to merge values of fields of same type and label name
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 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/eco/api/usecases/ooze_samples/helpers/ooze_handlers.rb', line 9 def merge_values(origin, append, klass: nil, delimiter: "\n") return origin if !append || append.to_s.strip.empty? return append if !origin || origin.to_s.strip.empty? case klass when Ecoportal::API::V2::Page::Component::RichTextField merge_values(origin, append, delimiter: "\n") when Ecoportal::API::V2::Page::Component::PlainTextField merge_values(origin, append, delimiter: "\n") when Ecoportal::API::V2::Page::Component::SelectionField merge_values(origin, append, delimiter: delimiter) when Ecoportal::API::V2::Page::Component::ReferenceField merge_values(origin, append, delimiter: delimiter) when Ecoportal::API::V2::Page::Component::PeopleField merge_values(origin, append, delimiter: delimiter) when Ecoportal::API::V2::Page::Component::DateField origin when Ecoportal::API::V2::Page::Component::NumberField origin when Ecoportal::API::V2::Page::Component::GaugeField origin when Ecoportal::API::V2::Page::Component::ChecklistField merge_values(origin, append, delimiter: delimiter) when Ecoportal::API::V2::Page::Component::ActionField merge_values(origin, append, delimiter: delimiter) when Ecoportal::API::V2::Page::Component::FilesField merge_values(origin, append, delimiter: delimiter) when Ecoportal::API::V2::Page::Component::ImagesField merge_values(origin, append, delimiter: delimiter) when Ecoportal::API::V2::Page::Component::GeoField merge_values(origin, append, delimiter: delimiter) when Ecoportal::API::V2::Page::Component::LawField merge_values(origin, append, delimiter: delimiter) else [origin, append].join(delimiter) end end |