Class: Pluggaloid::Handler

Inherits:
Identity show all
Defined in:
lib/pluggaloid/handler.rb

Overview

イベントのListenerやFilterのスーパクラス。イベントに関連付けたり、タグを付けたりできる

Direct Known Subclasses

Filter, Listener, StreamGenerator, Subscriber

Constant Summary collapse

Lock =
Mutex.new

Instance Attribute Summary collapse

Attributes inherited from Identity

#name, #slug

Instance Method Summary collapse

Constructor Details

#initialize(event, tags: [], **kwrest) ⇒ Handler

Args

event

監視するEventのインスタンス

name:

名前(String | nil)

slug:

ハンドラスラッグ(Symbol | nil)

tags:

Pluggaloid::HandlerTag|Array リスナのタグ

&callback

コールバック



17
18
19
20
21
22
23
24
# File 'lib/pluggaloid/handler.rb', line 17

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

Instance Attribute Details

#tagsObject (readonly)

Returns the value of attribute tags.



9
10
11
# File 'lib/pluggaloid/handler.rb', line 9

def tags
  @tags
end

Instance Method Details

#add_tag(tag) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/pluggaloid/handler.rb', line 26

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

#inspectObject



42
43
44
# File 'lib/pluggaloid/handler.rb', line 42

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

#remove_tag(tag) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/pluggaloid/handler.rb', line 34

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