class Slack::BlockKit::Block::SectionBlock

Attributes

accessory[R]
fields[R]
text[R]

Public Class Methods

new() click to toggle source
# File lib/slack/block_kit/block/section_block.rb, line 17
def initialize
  @fields = TypeRestrictedArray.new(CompositionObjects::Text)
end
populate(hash, object) click to toggle source
# File lib/slack/block_kit/block/section_block.rb, line 10
def self.populate(hash, object)
  object.accessory = hash[:accessory] if hash.key?(:accessory)
  if hash.key?(:text) then object.text = hash[:text]
  elsif hash.key?(:fields) then hash[:fields].each(&object.fields.method(:<<))
  end
end

Public Instance Methods

accessory=(elem) click to toggle source
# File lib/slack/block_kit/block/section_block.rb, line 34
def accessory=(elem)
  raise TypeError, 'accessory must be a block element' unless elem.is_a?(Element)

  @accessory = elem
end
text=(obj) click to toggle source
# File lib/slack/block_kit/block/section_block.rb, line 28
def text=(obj)
  raise TypeError, 'text must be a Text Object' unless obj.is_a?(CompositionObjects::Text)

  @text = obj
end
to_h() click to toggle source
Calls superclass method Slack::BlockKit::Block#to_h
# File lib/slack/block_kit/block/section_block.rb, line 40
def to_h
  if text && text.text.size > 3000
    raise RangeError, 'text in SectionBlock has max 3000 characters'
  end

  super.merge(
    block_id: block_id,
    text: text&.to_h,
    fields: fields.map(&:to_h),
    accessory: accessory&.to_h
  ).compact
end
valid?() click to toggle source

Either text or fields must exist and be non-empty.

# File lib/slack/block_kit/block/section_block.rb, line 22
def valid?
  if @text.nil? || @text.empty? then !@fields.empty?
  else !@text&.empty?
  end
end