Class: Discorb::View::Base Abstract

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

Overview

This class is abstract.
Note:

You should not use this class directly.

Base class for the view.

Defined Under Namespace

Modules: Prepend

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.componentsHash{Symbol => Discorb::Component}

Returns The components.

Returns:

  • (Hash{Symbol => Discorb::Component})

    The components.



33
34
35
# File 'lib/discorb/view/view.rb', line 33

def components
  @components
end

.viewsArray<ViewHandler>

Returns The view handlers.

Returns:



35
36
37
# File 'lib/discorb/view/view.rb', line 35

def views
  @views
end

Class Method Details

.button(id, label, style = :secondary, emoji: nil) {|interaction| ... } ⇒ Object

Adds button component to the view.

Parameters:

  • id (Symbol)

    The id of the button.

  • label (String)

    The label of the button.

  • style (:primary, :secondary, :success, :danger) (defaults to: :secondary)

    The style of the button.

  • emoji (Discorb::Emoji, nil) (defaults to: nil)

    The emoji of the button.

Yields:

  • The block to execute when the button is clicked.

Yield Parameters:

  • interaction (Discorb::MessageComponentInteraction)

    The interaction.

Raises:

  • (ArgumentError)


54
55
56
57
58
# File 'lib/discorb/view/view.rb', line 54

def button(id, label, style = :secondary, emoji: nil, &block)
  raise ArgumentError, "block required" unless block_given?
  button = Discorb::Button.new(label, style, emoji: emoji, custom_id: id)
  @components[id] = ComponentHandler.new(button, block)
end

.inherited(base) ⇒ Object



38
39
40
41
42
# File 'lib/discorb/view/view.rb', line 38

def inherited(base)
  base.prepend(Discorb::View::Base::Prepend)
  base.components = {}
  base.views = []
end

.select_menu(id, options, placeholder = nil, min_values: nil, max_values: nil) {|interaction| ... } ⇒ Object

Adds select menu component to the view.

Parameters:

  • id (Symbol)

    The id of the button.

  • label (String)

    The label of the button.

  • placeholder (String, nil) (defaults to: nil)

    The placeholder of the select menu.

  • min_length (Integer, nil)

    The minimum length of the select menu.

  • max_length (Integer, nil)

    The max length of the select menu.

Yields:

  • The block to execute when the menu is changed.

Yield Parameters:

  • interaction (Discorb::MessageComponentInteraction)

    The interaction.

Raises:

  • (ArgumentError)


71
72
73
74
75
76
# File 'lib/discorb/view/view.rb', line 71

def select_menu(id, options, placeholder = nil, min_values: nil, max_values: nil, &block)
  raise ArgumentError, "block required" unless block_given?
  options.map! { |option| option.is_a?(Discorb::SelectMenu::Option) ? option : Discorb::SelectMenu::Option.new(*option) }
  menu = Discorb::SelectMenu.new(id, options, placeholder: placeholder, min_values: min_values, max_values: max_values)
  @components[id] = ComponentHandler.new(menu, block)
end

.start(channel) ⇒ Object

Starts the view.

Parameters:

  • channel (Discorb::Messageable)

    The channel to send the message to.



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/discorb/view/view.rb', line 98

def start(channel, ...)
  client = channel.instance_variable_get(:@client)
  if @views.empty?
    raise "No views defined"
  elsif not @views.any? { |v| v.check.nil? }
    raise "No fallback view defined"
  elsif @views.filter { |v| v.check.nil? }.count > 1
    raise "Multiple fallback views defined"
  end
  view = new(client, channel, ...)
  view.start
end

.view(check = nil) {|result| ... } ⇒ Object

Note:

There must be one handler with no check.

Add view handler to the view.

Parameters:

  • check (Proc, nil) (defaults to: nil)

    The check of the view handler. The view handler will be executed when the check returns true, or check is nil.

Yields:

  • The block to execute when the view handler is executed.

Yield Parameters:

Raises:

  • (ArgumentError)


88
89
90
91
# File 'lib/discorb/view/view.rb', line 88

def view(check = nil, &block)
  raise ArgumentError, "block required" unless block_given?
  @views.insert(0, ViewHandler.new(check, block))
end