discorb-view 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 630986b3d66bbcd57a098f84ca0d7baed2b7cf11100b6464e8d66b4eb6fbd08f
4
- data.tar.gz: 8f830a89a62fe4a0acd98a54940ae830df2c3735061a00ba05d3408a66752a5a
3
+ metadata.gz: e4756a69740ed5a64f1ea13104a3ee0980a5f95364e93a4150ebf91559e66552
4
+ data.tar.gz: f3de79402b1aa23d4215e84d6988aacd754304c9c80958d72a60beed92158530
5
5
  SHA512:
6
- metadata.gz: 7d8259899de9b82de7ec2a58178adcdaf2097079f7feb232f39e98718aa460e27f80831862ce0726e214b0ef42e2a28b43e1f91e3eab24cabe79dcf17caa4f17
7
- data.tar.gz: 373f1ec214566f874243870bb037eb8bfb28cc292dbeba7d2497d79e2068f3c0f62e572bfa21211460b83cef255e3627220cf784fd298a7620bc4020f272af00
6
+ metadata.gz: 42b22a58f77541d9c0036402a7a33fc8e53aa1af08b3d2492bb4f8785ef9caffba65d30b1e8e4f62ecc8400feeb6aeea3c2f8d83ab8149772da7f037ac9c8eae
7
+ data.tar.gz: dc8248590703c3bf09854d61ac0851d70216d95c228dd82a53bce6a02b57fa0c3828795a901e677ce836f2b8fc3b03a7a3088cc4caa31c4b588190a1605e05b7
data/discorb-view.gemspec CHANGED
@@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
29
29
  spec.require_paths = ["lib"]
30
30
 
31
31
  # Uncomment to register a new dependency of your gem
32
- spec.add_dependency "discorb", "~> 0.9.6"
32
+ spec.add_dependency "discorb", "~> 0.10.3"
33
33
 
34
34
  # For more information and examples about making a new gem, checkout our
35
35
  # guide at: https://bundler.io/guides/creating_gem.html
data/examples/menu.rb CHANGED
@@ -3,11 +3,9 @@ require "discorb/view"
3
3
 
4
4
  client = Discorb::Client.new
5
5
 
6
- client.extend Discorb::View::Extension
7
-
8
- class MyMenu
9
- extend Discorb::View::Base
6
+ client.load_extension Discorb::View::Extension
10
7
 
8
+ class MyMenu < Discorb::View::Base
11
9
  @@texts = ["A", "B"]
12
10
 
13
11
  def initialize
@@ -3,11 +3,7 @@ module Discorb::View
3
3
  # An extension for using discorb-view.
4
4
  # @note Client must extend this class to use discorb-view.
5
5
  #
6
- module Extension
7
- attr_accessor :views
8
-
9
- extend Discorb::Extension
10
-
6
+ class Extension < Discorb::Extension
11
7
  event :button_click do |interaction|
12
8
  handle_interaction(interaction)
13
9
  end
@@ -17,25 +13,30 @@ module Discorb::View
17
13
  end
18
14
 
19
15
  # @private
20
- def self.extended(base)
16
+ def self.inherited(base)
21
17
  base.views = {}
22
18
  end
23
19
 
24
- class << self
25
- # @private
26
- def handle_interaction(interaction)
27
- unless view = @client.views[interaction.message.id.to_s]
28
- @client.log.warn "View: No handler for #{interaction.message.id.to_s}"
29
- return
30
- end
31
- handler = view.class.components[interaction.custom_id.to_sym]
32
- @client.log.debug "View: Handling #{interaction.custom_id} in #{interaction.message.id}"
33
- view.interaction = interaction
34
- update = view.instance_exec(interaction, &handler.block)
35
- return unless update
36
- @client.log.debug "View: Updating view #{interaction.message.id}"
37
- view.render
20
+ # @private
21
+ def handle_interaction(interaction)
22
+ unless view = @client.views[interaction.message.id.to_s]
23
+ @client.log.warn "View: No handler for #{interaction.message.id.to_s}"
24
+ return
25
+ end
26
+ handler = view.class.components[interaction.custom_id.to_sym]
27
+ @client.log.debug "View: Handling #{interaction.custom_id} in #{interaction.message.id}"
28
+ view.interaction = interaction
29
+ update = view.instance_exec(interaction, &handler.block)
30
+ return unless update
31
+ @client.log.debug "View: Updating view #{interaction.message.id}"
32
+ view.render
33
+ end
34
+
35
+ def self.loaded(client)
36
+ class << client
37
+ attr_accessor :views
38
38
  end
39
+ client.views = {}
39
40
  end
40
41
  end
41
42
  end
@@ -3,6 +3,6 @@
3
3
  module Discorb
4
4
  module View
5
5
  # @return [String] the version of the gem
6
- VERSION = "0.1.0"
6
+ VERSION = "0.2.0"
7
7
  end
8
8
  end
@@ -27,86 +27,87 @@ module Discorb::View
27
27
  # @note You should not use this class directly.
28
28
  # @abstract
29
29
  #
30
- module Base
31
- # @return [Hash{Symbol => Discorb::Component}] The components.
32
- attr_accessor :components
33
- # @return [Array<ViewHandler>] The view handlers.
34
- attr_accessor :views
30
+ class Base
31
+ class << self
32
+ # @return [Hash{Symbol => Discorb::Component}] The components.
33
+ attr_accessor :components
34
+ # @return [Array<ViewHandler>] The view handlers.
35
+ attr_accessor :views
35
36
 
36
- # @private
37
- def self.extended(base)
38
- base.prepend(Discorb::View::Base::Prepend)
39
- base.components = {}
40
- base.views = []
41
- end
37
+ # @private
38
+ def inherited(base)
39
+ base.prepend(Discorb::View::Base::Prepend)
40
+ base.components = {}
41
+ base.views = []
42
+ end
42
43
 
43
- #
44
- # Adds button component to the view.
45
- #
46
- # @param [Symbol] id The id of the button.
47
- # @param [String] label The label of the button.
48
- # @param [:primary, :secondary, :success, :danger] style The style of the button.
49
- # @param [Discorb::Emoji, nil] emoji The emoji of the button.
50
- # @yield The block to execute when the button is clicked.
51
- # @yieldparam [Discorb::MessageComponentInteraction] interaction The interaction.
52
- #
53
- def button(id, label, style = :secondary, emoji: nil, &block)
54
- raise ArgumentError, "block required" unless block_given?
55
- button = Discorb::Button.new(label, style, emoji: emoji, custom_id: id)
56
- @components[id] = ComponentHandler.new(button, block)
57
- end
44
+ #
45
+ # Adds button component to the view.
46
+ #
47
+ # @param [Symbol] id The id of the button.
48
+ # @param [String] label The label of the button.
49
+ # @param [:primary, :secondary, :success, :danger] style The style of the button.
50
+ # @param [Discorb::Emoji, nil] emoji The emoji of the button.
51
+ # @yield The block to execute when the button is clicked.
52
+ # @yieldparam [Discorb::MessageComponentInteraction] interaction The interaction.
53
+ #
54
+ def button(id, label, style = :secondary, emoji: nil, &block)
55
+ raise ArgumentError, "block required" unless block_given?
56
+ button = Discorb::Button.new(label, style, emoji: emoji, custom_id: id)
57
+ @components[id] = ComponentHandler.new(button, block)
58
+ end
58
59
 
59
- #
60
- # Adds select menu component to the view.
61
- #
62
- # @param [Symbol] id The id of the button.
63
- # @param [String] label The label of the button.
64
- # @param [String, nil] placeholder The placeholder of the select menu.
65
- # @param [Integer, nil] min_length The minimum length of the select menu.
66
- # @param [Integer, nil] max_length The max length of the select menu.
67
- # @yield The block to execute when the menu is changed.
68
- # @yieldparam [Discorb::MessageComponentInteraction] interaction The interaction.
69
- #
70
- def select_menu(id, options, placeholder = nil, min_values: nil, max_values: nil, &block)
71
- raise ArgumentError, "block required" unless block_given?
72
- options.map! { |option| option.is_a?(Discorb::SelectMenu::Option) ? option : Discorb::SelectMenu::Option.new(*option) }
73
- menu = Discorb::SelectMenu.new(id, options, placeholder: placeholder, min_values: min_values, max_values: max_values)
74
- @components[id] = ComponentHandler.new(menu, block)
75
- end
60
+ #
61
+ # Adds select menu component to the view.
62
+ #
63
+ # @param [Symbol] id The id of the button.
64
+ # @param [String] label The label of the button.
65
+ # @param [String, nil] placeholder The placeholder of the select menu.
66
+ # @param [Integer, nil] min_length The minimum length of the select menu.
67
+ # @param [Integer, nil] max_length The max length of the select menu.
68
+ # @yield The block to execute when the menu is changed.
69
+ # @yieldparam [Discorb::MessageComponentInteraction] interaction The interaction.
70
+ #
71
+ def select_menu(id, options, placeholder = nil, min_values: nil, max_values: nil, &block)
72
+ raise ArgumentError, "block required" unless block_given?
73
+ options.map! { |option| option.is_a?(Discorb::SelectMenu::Option) ? option : Discorb::SelectMenu::Option.new(*option) }
74
+ menu = Discorb::SelectMenu.new(id, options, placeholder: placeholder, min_values: min_values, max_values: max_values)
75
+ @components[id] = ComponentHandler.new(menu, block)
76
+ end
76
77
 
77
- #
78
- # Add view handler to the view.
79
- #
80
- # @param [Proc, nil] check The check of the view handler.
81
- # The view handler will be executed when the check returns true, or check is nil.
82
- # @yield The block to execute when the view handler is executed.
83
- # @yieldparam [Discorb::View::Base::Prepend::Result] result The result of the view.
84
- #
85
- # @note There must be one handler with no check.
86
- #
87
- def view(check = nil, &block)
88
- raise ArgumentError, "block required" unless block_given?
89
- @views.insert(0, ViewHandler.new(check, block))
90
- end
78
+ #
79
+ # Add view handler to the view.
80
+ #
81
+ # @param [Proc, nil] check The check of the view handler.
82
+ # The view handler will be executed when the check returns true, or check is nil.
83
+ # @yield The block to execute when the view handler is executed.
84
+ # @yieldparam [Discorb::View::Base::Prepend::Result] result The result of the view.
85
+ #
86
+ # @note There must be one handler with no check.
87
+ #
88
+ def view(check = nil, &block)
89
+ raise ArgumentError, "block required" unless block_given?
90
+ @views.insert(0, ViewHandler.new(check, block))
91
+ end
91
92
 
92
- #
93
- # Starts the view.
94
- #
95
- # @param [Discorb::Messageable] channel The channel to send the message to.
96
- #
97
- def start(channel, ...)
98
- client = channel.instance_variable_get(:@client)
99
- if @views.empty?
100
- raise "No views defined"
101
- elsif not @views.any? { |v| v.check.nil? }
102
- raise "No fallback view defined"
103
- elsif @views.filter { |v| v.check.nil? }.count > 1
104
- raise "Multiple fallback views defined"
93
+ #
94
+ # Starts the view.
95
+ #
96
+ # @param [Discorb::Messageable] channel The channel to send the message to.
97
+ #
98
+ def start(channel, ...)
99
+ client = channel.instance_variable_get(:@client)
100
+ if @views.empty?
101
+ raise "No views defined"
102
+ elsif not @views.any? { |v| v.check.nil? }
103
+ raise "No fallback view defined"
104
+ elsif @views.filter { |v| v.check.nil? }.count > 1
105
+ raise "Multiple fallback views defined"
106
+ end
107
+ view = new(client, channel, ...)
108
+ view.start
105
109
  end
106
- view = new(client, channel, ...)
107
- view.start
108
110
  end
109
-
110
111
  #
111
112
  # Modules for the prepend.
112
113
  #
@@ -125,6 +126,7 @@ module Discorb::View
125
126
  @channel = channel
126
127
  @message_id = nil
127
128
  @stopped = false
129
+ @components = self.class.components.dup.map { |id, component| [id, ComponentHandler.new(component.object.dup, component.block)] }.to_h
128
130
  @result = Result.new(nil, [], [])
129
131
  super(...)
130
132
  end
@@ -158,7 +160,7 @@ module Discorb::View
158
160
  components = @result.components.map do |c|
159
161
  case c
160
162
  when Symbol
161
- self.class.components[c]&.object or raise ArgumentError "Unknown component ID #{c}"
163
+ @components[c]&.object or raise ArgumentError "Unknown component ID #{c}"
162
164
  when Button
163
165
  c
164
166
  else
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discorb-view
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - sevenc-nanashi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-29 00:00:00.000000000 Z
11
+ date: 2021-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: discorb
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.9.6
19
+ version: 0.10.3
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.9.6
26
+ version: 0.10.3
27
27
  description:
28
28
  email:
29
29
  - sevenc-nanashi@sevenbot.jp