Class: Decidim::Attributes::Array
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- Decidim::Attributes::Array
- Defined in:
- lib/decidim/attributes/array.rb
Overview
Custom attributes value to represent an Array.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#value_type ⇒ Object
readonly
Returns the value of attribute value_type.
Instance Method Summary collapse
-
#cast(value) ⇒ Object
The value would not be cast in case it is nil by default.
-
#initialize(value_type: ::Object, default: []) ⇒ Array
constructor
A new instance of Array.
-
#type ⇒ Object
:nodoc:.
-
#validate_nested? ⇒ Boolean
The nested validator should be only added for those attributes that inherit from the AttributeObject::Model type.
Constructor Details
#initialize(value_type: ::Object, default: []) ⇒ Array
Returns a new instance of Array.
9 10 11 12 |
# File 'lib/decidim/attributes/array.rb', line 9 def initialize(value_type: ::Object, default: []) @value_type = value_type @default = default end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
7 8 9 |
# File 'lib/decidim/attributes/array.rb', line 7 def default @default end |
#value_type ⇒ Object (readonly)
Returns the value of attribute value_type.
7 8 9 |
# File 'lib/decidim/attributes/array.rb', line 7 def value_type @value_type end |
Instance Method Details
#cast(value) ⇒ Object
The value would not be cast in case it is nil by default. We also want to cast the nil values to the default values.
20 21 22 |
# File 'lib/decidim/attributes/array.rb', line 20 def cast(value) cast_value(value) end |
#type ⇒ Object
:nodoc:
14 15 16 |
# File 'lib/decidim/attributes/array.rb', line 14 def type # :nodoc: :array end |
#validate_nested? ⇒ Boolean
The nested validator should be only added for those attributes that inherit from the AttributeObject::Model type. Otherwise this would be also added e.g. for ActiveRecord objects which would cause unexpected validation errors.
The hash type inherits from the array type, so this covers both cases.
30 31 32 33 34 |
# File 'lib/decidim/attributes/array.rb', line 30 def validate_nested? return false unless value_type.is_a?(Class) (value_type < Decidim::AttributeObject::Model) == true end |