Module: Plutonium::StructuredInputs::ParamCleaner

Defined in:
lib/plutonium/structured_inputs/param_cleaner.rb

Overview

Normalises an extracted structured-input value before it is stored.

The form’s ‘extract_input` already yields a `Hash` (single) or an `Array<Hash>` (repeater), so the only work left is to symbolise keys and, for repeaters, drop rows the user left entirely blank.

Class Method Summary collapse

Class Method Details

.call(value, repeat:) ⇒ Hash+

Parameters:

  • value (Hash, Array, nil)

    the extracted param for this input

  • repeat (Boolean, Integer)

    truthy => array (repeater), else hash

Returns:

  • (Hash, Array<Hash>)


16
17
18
# File 'lib/plutonium/structured_inputs/param_cleaner.rb', line 16

def call(value, repeat:)
  repeat ? clean_collection(value) : clean_one(value)
end

.clean_collection(value) ⇒ Object



24
25
26
27
28
29
# File 'lib/plutonium/structured_inputs/param_cleaner.rb', line 24

def clean_collection(value)
  Array(value)
    .select { |row| row.is_a?(Hash) }
    .map { |row| symbolize(row) }
    .reject { |row| row.values.all? { |v| v.to_s.strip.empty? } }
end

.clean_one(value) ⇒ Object



20
21
22
# File 'lib/plutonium/structured_inputs/param_cleaner.rb', line 20

def clean_one(value)
  value.is_a?(Hash) ? symbolize(value) : {}
end

.symbolize(row) ⇒ Object



31
32
33
# File 'lib/plutonium/structured_inputs/param_cleaner.rb', line 31

def symbolize(row)
  row.to_h.transform_keys(&:to_sym)
end