Class: Discorb::Component Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/discorb/components.rb

Overview

This class is abstract.

Represents a Discord component.

Direct Known Subclasses

Button, SelectMenu, TextInput

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_hash(data) ⇒ Component

Create a new component from hash data.

Parameters:

  • data (Hash)

    Hash data.

Returns:

See Also:



22
23
24
25
26
27
28
29
30
31
# File 'lib/discorb/components.rb', line 22

def from_hash(data)
  case data[:type]
  when 2
    Button
  when 3
    SelectMenu
  when 4
    TextInput
  end.from_hash(data)
end

.to_payload(components) ⇒ Array<Hash>

Convert components to a hash.

Parameters:

Returns:

  • (Array<Hash>)

    Hash data.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/discorb/components.rb', line 40

def to_payload(components)
  tmp_components = []
  tmp_row = []
  components.each do |c|
    case c
    when Array
      tmp_components << tmp_row
      tmp_row = []
      tmp_components << c
    when SelectMenu, TextInput
      tmp_components << tmp_row
      tmp_row = []
      tmp_components << [c]
    else
      tmp_row << c
    end
  end
  tmp_components << tmp_row
  tmp_components
    .filter { |c| c.length.positive? }
    .map { |c| { type: 1, components: c.map(&:to_hash) } }
end

Instance Method Details

#inspectObject



9
10
11
# File 'lib/discorb/components.rb', line 9

def inspect
  "#<#{self.class}>"
end