Class: BlockKit::Types::Generic

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

Class Method Summary collapse

Instance Method Summary collapse

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

#typeObject (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