Module: Opencdd::ParseHelpers

Included in:
Entity
Defined in:
lib/opencdd/parse_helpers.rb

Class Method Summary collapse

Class Method Details

.brace_wrapped?(s) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/opencdd/parse_helpers.rb', line 68

def brace_wrapped?(s)
  s = s.to_s
  s.start_with?("{") && s.end_with?("}")
end

.paren_wrapped?(s) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/opencdd/parse_helpers.rb', line 63

def paren_wrapped?(s)
  s = s.to_s
  s.start_with?("(") && s.end_with?(")")
end

.parse_irdi_list(raw) ⇒ Object

Delegates to StructuredValues — single source of truth for brace/paren/comma handling. Whitespace-separated forms are a Parcel-specific quirk preserved here for back-compat.



17
18
19
20
21
22
23
24
25
# File 'lib/opencdd/parse_helpers.rb', line 17

def parse_irdi_list(raw)
  return [] if raw.nil? || raw.to_s.strip.empty?
  tokens = Opencdd::StructuredValues.unwrap_and_split(raw)
  # Some Parcel sources use whitespace as the separator inside
  # a single "set" cell. Honour both shapes.
  tokens.flat_map { |t| t.split(/\s+/) }.reject(&:empty?).filter_map do |t|
    Opencdd::IRDI.parse(t)
  end
end

.parse_pair_list(raw) ⇒ Object

Parses both the (name,lang),... structured form and the legacy flat (lang, name, lang, name, ...) form. Kept on ParseHelpers because the flat form isn't a synonym-specific concept — it's an ad-hoc legacy wire shape.



40
41
42
43
44
45
46
47
48
49
# File 'lib/opencdd/parse_helpers.rb', line 40

def parse_pair_list(raw)
  return [] if raw.nil? || raw.to_s.strip.empty?
  s = raw.to_s.strip
  return parse_synonym_tuples(s) if brace_wrapped?(s)
  return [[nil, s]] unless paren_wrapped?(s)
  unwrap_parens(s)
    .split(",")
    .each_slice(2)
    .map { |lang, name| [lang&.strip, name&.strip] }
end

.parse_string_list(raw) ⇒ Object



27
28
29
# File 'lib/opencdd/parse_helpers.rb', line 27

def parse_string_list(raw)
  Opencdd::StructuredValues.unwrap_and_split(raw)
end

.parse_synonym_tuples(raw) ⇒ Object



31
32
33
34
# File 'lib/opencdd/parse_helpers.rb', line 31

def parse_synonym_tuples(raw)
  return [] if raw.nil? || raw.to_s.strip.empty?
  Opencdd::StructuredValues.parse_synonyms(raw)
end

.unwrap_delimiters(s) ⇒ Object



57
58
59
60
61
# File 'lib/opencdd/parse_helpers.rb', line 57

def unwrap_delimiters(s)
  s = s.to_s
  return s[1..-2] if paren_wrapped?(s) || brace_wrapped?(s)
  s
end

.unwrap_parens(s) ⇒ Object

── Thin legacy predicates (kept for back-compat) ──────────



52
53
54
55
# File 'lib/opencdd/parse_helpers.rb', line 52

def unwrap_parens(s)
  s = s.to_s
  paren_wrapped?(s) ? s[1..-2] : s
end