Class: BlockKit::Types::Array
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- BlockKit::Types::Array
- Defined in:
- lib/block_kit/types/array.rb
Overview
Allows declaring ActiveModel attributes that are arrays of specific types, powered by an internal TypedArray class that enforces type constraints on array elements any time the array is modified.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#item_type ⇒ Object
readonly
Returns the value of attribute item_type.
Class Method Summary collapse
Instance Method Summary collapse
- #cast(value) ⇒ Object
- #changed_in_place?(raw_old_value, new_value) ⇒ Boolean
-
#initialize(item_type) ⇒ Array
constructor
A new instance of Array.
- #serialize(value) ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(item_type) ⇒ Array
Returns a new instance of Array.
17 18 19 20 21 22 |
# File 'lib/block_kit/types/array.rb', line 17 def initialize(item_type) item_type = ActiveModel::Type.lookup(item_type) if item_type.is_a?(Symbol) item_type = Types::Generic.of_type(item_type) if item_type.is_a?(Class) && item_type < BlockKit::Base @item_type = item_type end |
Instance Attribute Details
#item_type ⇒ Object (readonly)
Returns the value of attribute item_type.
11 12 13 |
# File 'lib/block_kit/types/array.rb', line 11 def item_type @item_type end |
Class Method Details
.of(item_type) ⇒ Object
13 14 15 |
# File 'lib/block_kit/types/array.rb', line 13 def self.of(item_type) new(item_type) end |
Instance Method Details
#cast(value) ⇒ Object
28 29 30 31 32 |
# File 'lib/block_kit/types/array.rb', line 28 def cast(value) return nil if value.nil? TypedArray.new(item_type, Array(value)) end |
#changed_in_place?(raw_old_value, new_value) ⇒ Boolean
40 41 42 |
# File 'lib/block_kit/types/array.rb', line 40 def changed_in_place?(raw_old_value, new_value) cast(raw_old_value) != cast(new_value) end |
#serialize(value) ⇒ Object
34 35 36 37 38 |
# File 'lib/block_kit/types/array.rb', line 34 def serialize(value) return nil if value.nil? cast(value) end |
#type ⇒ Object
24 25 26 |
# File 'lib/block_kit/types/array.rb', line 24 def type :array end |