Class: Discorb::View::Base Abstract
- Inherits:
-
Object
- Object
- Discorb::View::Base
- 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
-
.components ⇒ Hash{Symbol => Discorb::Component}
The components.
-
.views ⇒ Array<ViewHandler>
The view handlers.
Class Method Summary collapse
-
.button(id, label, style = :secondary, emoji: nil) {|interaction| ... } ⇒ Object
Adds button component to the view.
- .inherited(base) ⇒ Object
-
.select_menu(id, options, placeholder = nil, min_values: nil, max_values: nil) {|interaction| ... } ⇒ Object
Adds select menu component to the view.
-
.start(channel) ⇒ Object
Starts the view.
-
.view(check = nil) {|result| ... } ⇒ Object
Add view handler to the view.
Class Attribute Details
.components ⇒ Hash{Symbol => Discorb::Component}
Returns The components.
33 34 35 |
# File 'lib/discorb/view/view.rb', line 33 def components @components end |
.views ⇒ Array<ViewHandler>
Returns The view handlers.
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.
54 55 56 57 58 |
# File 'lib/discorb/view/view.rb', line 54 def (id, label, style = :secondary, emoji: nil, &block) raise ArgumentError, "block required" unless block_given? = Discorb::Button.new(label, style, emoji: emoji, custom_id: id) @components[id] = ComponentHandler.new(, 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.
71 72 73 74 75 76 |
# File 'lib/discorb/view/view.rb', line 71 def (id, , placeholder = nil, min_values: nil, max_values: nil, &block) raise ArgumentError, "block required" unless block_given? .map! { |option| option.is_a?(Discorb::SelectMenu::Option) ? option : Discorb::SelectMenu::Option.new(*option) } = Discorb::SelectMenu.new(id, , placeholder: placeholder, min_values: min_values, max_values: max_values) @components[id] = ComponentHandler.new(, block) end |
.start(channel) ⇒ Object
Starts the view.
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.
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 |