Class: TypedEAV::Field::IntegerArray

Inherits:
Base show all
Defined in:
app/models/typed_eav/field/integer_array.rb

Constant Summary

Constants inherited from Base

Base::RESERVED_NAMES

Constants included from ColumnMapping

ColumnMapping::DEFAULT_OPERATORS_BY_COLUMN, ColumnMapping::FALLBACK_OPERATORS

Instance Method Summary collapse

Methods inherited from Base

#allowed_option_values, #clear_option_cache!, #default_value, #default_value=, #field_type_name, #optionable?

Instance Method Details

#array_field?Boolean

Returns:



11
# File 'app/models/typed_eav/field/integer_array.rb', line 11

def array_field? = true

#cast(raw) ⇒ Object

Cast each element using the scalar-integer parsing rules: strings must look like integers (‘/A-?d+z/`). Fractional input like “1.9” is rejected instead of silently truncated to 1.

If ANY element fails to cast, mark the whole value invalid and store nil — don’t keep a “partially cast” array around, because a failed form re-render would drop the bad elements and confuse the user (they’d see only the good ones and not know why validation fired). This mirrors scalar ‘Integer#cast` which returns `[nil, true]` on invalid input.



23
24
25
26
27
28
29
30
31
# File 'app/models/typed_eav/field/integer_array.rb', line 23

def cast(raw)
  return [nil, false] if raw.nil?

  elements = Array(raw).reject { |v| v.nil? || (v.respond_to?(:empty?) && v.empty?) }
  casted = elements.map { |v| cast_integer(v) }
  return [nil, true] if casted.any?(&:nil?) && elements.any?

  [casted.presence, false]
end

#validate_typed_value(record, val) ⇒ Object



33
34
35
36
# File 'app/models/typed_eav/field/integer_array.rb', line 33

def validate_typed_value(record, val)
  validate_array_size(record, val)
  validate_element_range(record, val)
end