Class: StructuredParams::Type::Array
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- StructuredParams::Type::Array
- 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
-
#item_type ⇒ Object
readonly
: ActiveModel::Type::Value.
Instance Method Summary collapse
-
#cast(value) ⇒ Object
Cast value to array and convert each element to appropriate type : (untyped) -> ::Array?.
-
#initialize(value_class: nil, value_type: nil, **options) ⇒ Array
constructor
value_class or value_type is required : (?value_class: singleton(StructuredParams::Params)?, ?value_type: Symbol?, **untyped) -> void.
-
#item_type_is_structured_params_object? ⇒ Boolean
Determine if item type is StructuredParams::Object : () -> bool.
-
#permit_attribute_names ⇒ Object
Get permitted parameter names for use with Strong Parameters : () -> ::Array.
- #serialize(value) ⇒ Object
-
#type ⇒ Object
: () -> Symbol.
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, **) super() validate_parameters!(value_class, value_type) @item_type = build_item_type(value_class, value_type, ) end |
Instance Attribute Details
#item_type ⇒ Object (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
57 58 59 |
# File 'lib/structured_params/type/array.rb', line 57 def item_type_is_structured_params_object? @item_type.is_a?(StructuredParams::Type::Object) end |
#permit_attribute_names ⇒ Object
Get permitted parameter names for use with Strong Parameters : () -> ::Array
49 50 51 52 53 |
# File 'lib/structured_params/type/array.rb', line 49 def permit_attribute_names return [] unless item_type_is_structured_params_object? @item_type.permit_attribute_names end |
#serialize(value) ⇒ Object
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 |
#type ⇒ Object
: () -> Symbol
26 27 28 |
# File 'lib/structured_params/type/array.rb', line 26 def type :array end |