Class: Pluggaloid::Handler
- Defined in:
- lib/pluggaloid/handler.rb
Overview
イベントのListenerやFilterのスーパクラス。イベントに関連付けたり、タグを付けたりできる
Direct Known Subclasses
Constant Summary collapse
- Lock =
Mutex.new
Instance Attribute Summary collapse
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Attributes inherited from Identity
Instance Method Summary collapse
- #add_tag(tag) ⇒ Object
-
#initialize(event, tags: [], **kwrest) ⇒ Handler
constructor
Args [event] 監視するEventのインスタンス [name:] 名前(String | nil) [slug:] ハンドラスラッグ(Symbol | nil) [tags:] Pluggaloid::HandlerTag|Array リスナのタグ [&callback] コールバック.
- #inspect ⇒ Object
- #remove_tag(tag) ⇒ Object
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 = .is_a?(Pluggaloid::HandlerTag) ? [] : Array() .each{|t| raise "#{t} is not a Pluggaloid::HandlerTag" unless t.is_a?(Pluggaloid::HandlerTag) } @tags = Set.new().freeze end |
Instance Attribute Details
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
9 10 11 |
# File 'lib/pluggaloid/handler.rb', line 9 def @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 |
#inspect ⇒ Object
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 |