File: handler.rb

package info (click to toggle)
ruby-pluggaloid 1.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 260 kB
  • sloc: ruby: 1,752; sh: 4; makefile: 2
file content (45 lines) | stat: -rw-r--r-- 1,452 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-

=begin rdoc
イベントのListenerやFilterのスーパクラス。
イベントに関連付けたり、タグを付けたりできる
=end
class Pluggaloid::Handler < Pluggaloid::Identity
  Lock = Mutex.new
  attr_reader :tags

  # ==== Args
  # [event] 監視するEventのインスタンス
  # [name:] 名前(String | nil)
  # [slug:] ハンドラスラッグ(Symbol | nil)
  # [tags:] Pluggaloid::HandlerTag|Array リスナのタグ
  # [&callback] コールバック
  def initialize(event, tags: [], **kwrest)
    raise Pluggaloid::TypeError, "Argument `event' must be instance of Pluggaloid::Event, but given #{event.class}." unless event.is_a? Pluggaloid::Event
    super(**kwrest)
    @event = event
    _tags = tags.is_a?(Pluggaloid::HandlerTag) ? [tags] : Array(tags)
    _tags.each{|t| raise "#{t} is not a Pluggaloid::HandlerTag" unless t.is_a?(Pluggaloid::HandlerTag) }
    @tags = Set.new(_tags).freeze
  end

  def add_tag(tag)
    raise Pluggaloid::TypeError, "Argument `tag' must be instance of Pluggaloid::HandlerTag, but given #{tag.class}." unless tag.is_a? Pluggaloid::HandlerTag
    Lock.synchronize do
      @tags = Set.new([tag, *@tags]).freeze
    end
    self
  end

  def remove_tag(tag)
    Lock.synchronize do
      @tags -= tag
      @tags.freeze
    end
    self
  end

  def inspect
    "#<#{self.class} event: #{@event.name.inspect}, slug: #{slug.inspect}, name: #{name.inspect}>"
  end
end