Class: Castkit::Types::Collection

Inherits:
Base
  • Object
show all
Defined in:
lib/castkit/types/collection.rb

Overview

Type definition for ‘:array` attributes.

Wraps any value in an array using ‘Array(value)` coercion. This ensures consistent array representation even if the input is a single value or nil.

This class is used internally by Castkit when an attribute is defined with:

`array :tags, of: :string`

Instance Method Summary collapse

Methods inherited from Base

cast!, deserialize, serialize, #serialize, validate!

Instance Method Details

#deserialize(value) ⇒ ::Array

Deserializes the value into an array using ‘Array(value)`.

Parameters:

  • value (Object)

Returns:

  • (::Array)
[View source]

20
21
22
# File 'lib/castkit/types/collection.rb', line 20

def deserialize(value)
  Array(value)
end

#validate!(value, options: {}, context: nil) ⇒ void

This method returns an undefined value.

Validates the Array value.

Parameters:

  • value (Object)
  • options (Hash) (defaults to: {})

    validation options

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

    attribute context for error messages

[View source]

30
31
32
# File 'lib/castkit/types/collection.rb', line 30

def validate!(value, options: {}, context: nil)
  Castkit::Validators::CollectionValidator.call(value, options: options, context: context)
end