Class: BlockKit::Types::Blocks

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
lib/block_kit/types/blocks.rb

Overview

Provides a way to generate an ActiveModel type for multiple block types.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*block_classes) ⇒ Blocks

Returns a new instance of Blocks.



11
12
13
14
# File 'lib/block_kit/types/blocks.rb', line 11

def initialize(*block_classes)
  @block_classes = block_classes
  @block_types = block_classes.map { |block_class| Types::Generic.of_type(block_class) }
end

Instance Attribute Details

#block_classesObject (readonly)

Returns the value of attribute block_classes.



9
10
11
# File 'lib/block_kit/types/blocks.rb', line 9

def block_classes
  @block_classes
end

#block_typesObject (readonly)

Returns the value of attribute block_types.



9
10
11
# File 'lib/block_kit/types/blocks.rb', line 9

def block_types
  @block_types
end

Instance Method Details

#cast(value) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/block_kit/types/blocks.rb', line 20

def cast(value)
  case value
  when *@block_classes
    value
  when Hash
    value = value.with_indifferent_access
    type_name = value[:type]&.to_sym

    matching_type = @block_types.find do |type_class|
      type_class.respond_to?(:type) && type_class.type == :"block_kit_#{type_name}"
    end

    matching_type&.cast(value)
  end
end

#typeObject



16
17
18
# File 'lib/block_kit/types/blocks.rb', line 16

def type
  :block_kit_block
end