Class: StructuredParams::Type::Array

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
lib/structured_params/type/array.rb

Overview

Custom type for arrays of both StructuredParams::Params objects and primitive types

Usage examples:

# Array of nested objects
attribute :hobbies, :array, value_class: HobbyParameter

# Array of primitive types
attribute :tags, :array, value_type: :string

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value_class: nil, value_type: nil, **options) ⇒ Array

value_class or value_type is required : (?value_class: singleton(StructuredParams::Params)?, ?value_type: Symbol?, **untyped) -> void



19
20
21
22
23
# File 'lib/structured_params/type/array.rb', line 19

def initialize(value_class: nil, value_type: nil, **options)
  super()
  validate_parameters!(value_class, value_type)
  @item_type = build_item_type(value_class, value_type, options)
end

Instance Attribute Details

#item_typeObject (readonly)

: ActiveModel::Type::Value



15
16
17
# File 'lib/structured_params/type/array.rb', line 15

def item_type
  @item_type
end

Instance Method Details

#cast(value) ⇒ Object

Cast value to array and convert each element to appropriate type : (untyped) -> ::Array?



32
33
34
35
36
# File 'lib/structured_params/type/array.rb', line 32

def cast(value)
  return nil if value.nil?

  ensure_array(value).map { |item| cast_item(item) }
end

#item_type_is_structured_params_object?Boolean

Determine if item type is StructuredParams::Object : () -> bool

Returns:

  • (Boolean)


71
72
73
# File 'lib/structured_params/type/array.rb', line 71

def item_type_is_structured_params_object?
  @item_type.is_a?(StructuredParams::Type::Object)
end

#permit_attribute_namesObject

Get permitted parameter names for use with Strong Parameters

Returns:

- For object arrays (value_class): nested keys from the object
  Example: HobbyParams with [:name, :level]
  → returns [:name, :level]
  → becomes { hobbies: [:name, :level] } in Params#permit_attribute_names

- For primitive arrays (value_type): empty array
  Example: attribute :tags, :array, value_type: :string
  → returns []
  → becomes { tags: [] } in Params#permit_attribute_names
  → finally used as params.permit(:name, { tags: [] })

: () -> ::Array



62
63
64
65
66
67
# File 'lib/structured_params/type/array.rb', line 62

def permit_attribute_names
  return @item_type.permit_attribute_names if item_type_is_structured_params_object?

  # Primitive arrays return [] for Strong Parameters format
  []
end

#serialize(value) ⇒ Object

Serialize array (convert each element to Hash) : (::Array?) -> ::Array?



40
41
42
43
44
45
# File 'lib/structured_params/type/array.rb', line 40

def serialize(value)
  return nil if value.nil?
  return [] unless value.is_a?(::Array)

  value.map { |item| @item_type.serialize(item) }
end

#typeObject

: () -> Symbol



26
27
28
# File 'lib/structured_params/type/array.rb', line 26

def type
  :array
end