Sha256: 000cc32b37b30eb9bacb7ae00b93b8d1a5ce92ccba2182fd1d7ac42e117db23e

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module Slack
  module BlockKit
    class Element
      class ButtonElement < Element
        using Refinements::HashCompact
        attr_reader :text, :style
        attr_accessor :value

        def self.populate(hash, object)
          object.text = hash.fetch(:text)
          object.action_id = hash.fetch(:action_id) if hash[:action_id]
          object.style = hash.fetch(:style) if hash[:style]
          object.value = hash.fetch(:value) if hash[:value]

          super(hash, object)
        end

        def valid?
          !@text.empty?
        end

        def text=(obj)
          raise TypeError, 'text must be a Text Object' unless obj.is_a?(CompositionObjects::Text)

          @text = obj
        end

        def style=(obj)
          unless %i( default primary danger ).include?(obj.to_sym)
            raise ArgumentError, 'style may only be default, primary, or danger'
          end

          @style = obj.to_sym
        end

        def to_h
          super.merge(
            text: text.to_h,
            style: style,
            value: value
          ).compact
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slack-block-kit-0.1.0 lib/slack/block_kit/element/button_element.rb