Class: BlockKit::Layout::Context

Inherits:
Base
  • Object
show all
Defined in:
lib/block_kit/layout/context.rb

Constant Summary collapse

MAX_ELEMENTS =
10
SUPPORTED_ELEMENTS =
[
  Elements::Image,
  Composition::Mrkdwn,
  Composition::PlainText
].freeze

Constants inherited from Base

Base::MAX_BLOCK_ID_LENGTH

Instance Method Summary collapse

Methods inherited from Base

inherited

Methods inherited from Base

#==, fix, #fix_validation_errors, #fix_validation_errors!, fixes, inherited, inspect, #inspect, #pretty_print, #to_json

Constructor Details

#initialize(attributes = {}) ⇒ Context

Returns a new instance of Context.



22
23
24
25
26
27
# File 'lib/block_kit/layout/context.rb', line 22

def initialize(attributes = {})
  attributes = attributes.with_indifferent_access
  attributes[:elements] ||= []

  super
end

Instance Method Details

#append(element) ⇒ Object



37
38
39
40
41
# File 'lib/block_kit/layout/context.rb', line 37

def append(element)
  elements << element

  self
end

#as_jsonObject



43
44
45
# File 'lib/block_kit/layout/context.rb', line 43

def as_json(*)
  super.merge(elements: elements&.map(&:as_json)).compact
end

#image(alt_text:, image_url: nil, slack_file: nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/block_kit/layout/context.rb', line 29

def image(alt_text:, image_url: nil, slack_file: nil)
  if (image_url.nil? && slack_file.nil?) || (image_url && slack_file)
    raise ArgumentError, "Must provide either image_url or slack_file, but not both."
  end

  append(Elements::Image.new(alt_text: alt_text, image_url: image_url, slack_file: slack_file))
end