Class: BlockKit::Types::Array

Inherits:
ActiveModel::Type::Value
  • Object
show all
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

Set

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_typeObject (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

Returns:

  • (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

#typeObject



24
25
26
# File 'lib/block_kit/types/array.rb', line 24

def type
  :array
end