Module: Eco::API::UseCases::GraphQL::Helpers::Pages::OozeHandlers

Included in:
Samples::Pages::Register::Base
Defined in:
lib/eco/api/usecases/graphql/helpers/pages/ooze_handlers.rb

Overview

Native GraphQL re-expression of OozeSamples::Helpers::OozeHandlers.

merge_values combines two values of a data field of the same type and label into one, matching the v2 semantics but dispatching on the GraphQL DataField TYPE STRING (e.g. 'PlainText', 'Select', 'Date') rather than on Ecoportal::API::V2::Page::Component::* classes. The type strings are the __typename-derived values in Ecoportal::API::GraphQL::Base::Page::DataField::TYPE_MAP.

merge_arrays / array_indexes are pure array utilities — ported verbatim.

Constant Summary collapse

MERGE_STRATEGY =

Per-type merge strategy. :join → concatenate origin + append with a delimiter (text, select, reference, people, checklist, action list, files, images, geo, law) :origin → keep the origin value untouched (date, number, gauge — no sensible textual concatenation; the v2 case returned origin for these)

Text-ish types always merge with a NEWLINE delimiter in v2 (RichText / PlainText) regardless of the caller's delimiter:; the rest honour it.

{
  'PlainText'      => %i[join newline],
  'RichText'       => %i[join newline],
  'Select'         => %i[join delimiter],
  'CrossReference' => %i[join delimiter],
  'People'         => %i[join delimiter],
  'Checklist'      => %i[join delimiter],
  'ActionsList'    => %i[join delimiter],
  'FileField'      => %i[join delimiter],
  'File'           => %i[join delimiter],
  'ImageGallery'   => %i[join delimiter],
  'Geo'            => %i[join delimiter],
  'Law'            => %i[join delimiter],
  'DateField'      => %i[origin],
  'Date'           => %i[origin],
  'Number'         => %i[origin],
  'Gauge'          => %i[origin]
}.freeze

Instance Method Summary collapse

Instance Method Details

#array_indexes(ary) ⇒ Object



98
99
100
# File 'lib/eco/api/usecases/graphql/helpers/pages/ooze_handlers.rb', line 98

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).



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
# File 'lib/eco/api/usecases/graphql/helpers/pages/ooze_handlers.rb', line 68

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)

  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, type: nil, delimiter: "\n") ⇒ Object

Merge values of fields of the same type and label name.

Parameters:

  • origin (Object)

    the value to keep as the base.

  • append (Object)

    the value to merge onto origin.

  • type (String, Symbol, nil) (defaults to: nil)

    the GraphQL DataField type string (e.g. 'PlainText', 'Select'). Nil / unknown → default join. Accepts a field object responding to #type too (convenience).

  • delimiter (String) (defaults to: "\n")

    the delimiter for :delimiter-strategy types.

Returns:

  • (Object)

    the merged value.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/eco/api/usecases/graphql/helpers/pages/ooze_handlers.rb', line 50

def merge_values(origin, append, type: nil, delimiter: "\n")
  return origin if !append || append.to_s.strip.empty?
  return append if !origin || origin.to_s.strip.empty?

  type_key = resolve_field_type(type)
  strategy, delim_kind = MERGE_STRATEGY.fetch(type_key, %i[join delimiter])

  return origin if strategy == :origin

  used_delimiter = delim_kind == :newline ? "\n" : delimiter
  [origin, append].join(used_delimiter)
end