Class: BlockKit::Types::Blocks
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- BlockKit::Types::Blocks
- 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
-
#block_classes ⇒ Object
readonly
Returns the value of attribute block_classes.
-
#block_types ⇒ Object
readonly
Returns the value of attribute block_types.
Instance Method Summary collapse
- #cast(value) ⇒ Object
-
#initialize(*block_classes) ⇒ Blocks
constructor
A new instance of Blocks.
- #type ⇒ Object
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_classes ⇒ Object (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_types ⇒ Object (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 |
#type ⇒ Object
16 17 18 |
# File 'lib/block_kit/types/blocks.rb', line 16 def type :block_kit_block end |