Class: BlockKit::Types::Generic
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- BlockKit::Types::Generic
- Defined in:
- lib/block_kit/types/generic.rb
Overview
Provides a way to generate generic ActiveModel types for individual block types. Most block types can simply be cast from an object of the same type or a Hash with the object’s attributes.
Instance Attribute Summary collapse
-
#block_class ⇒ Object
readonly
Returns the value of attribute block_class.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .new(block_class) ⇒ Object (also: of_type)
Instance Method Summary collapse
- #cast(value) ⇒ Object
-
#initialize(block_class) ⇒ Generic
constructor
A new instance of Generic.
Constructor Details
#initialize(block_class) ⇒ Generic
Returns a new instance of Generic.
30 31 32 33 |
# File 'lib/block_kit/types/generic.rb', line 30 def initialize(block_class) @block_class = block_class @type = :"block_kit_#{block_class.type}" end |
Instance Attribute Details
#block_class ⇒ Object (readonly)
Returns the value of attribute block_class.
28 29 30 |
# File 'lib/block_kit/types/generic.rb', line 28 def block_class @block_class end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
28 29 30 |
# File 'lib/block_kit/types/generic.rb', line 28 def type @type end |
Class Method Details
.new(block_class) ⇒ Object Also known as: of_type
13 14 15 |
# File 'lib/block_kit/types/generic.rb', line 13 def self.new(block_class) instances[block_class] ||= super end |
Instance Method Details
#cast(value) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/block_kit/types/generic.rb', line 35 def cast(value) case value when block_class value when Hash block_class.new(**value.with_indifferent_access.slice(*block_class.attribute_names).symbolize_keys) end end |