Class: Pluggaloid::Filter

Inherits:
Handler show all
Defined in:
lib/pluggaloid/filter.rb

Constant Summary collapse

NotConverted =
Class.new
THROUGH =
NotConverted.new.freeze
CANCEL_PROC =
method(:cancel!)

Constants inherited from Handler

Handler::Lock

Instance Attribute Summary

Attributes inherited from Handler

#tags

Attributes inherited from Identity

#name, #slug

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Handler

#add_tag, #inspect, #remove_tag

Methods inherited from Identity

#inspect

Constructor Details

#initialize(event, **kwrest, &callback) ⇒ Filter

Args

event

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

name:

名前(String | nil)

slug:

フィルタスラッグ(Symbol | nil)

tags:

Pluggaloid::HandlerTag|Array フィルタのタグ

&callback

コールバック



21
22
23
24
25
# File 'lib/pluggaloid/filter.rb', line 21

def initialize(event, **kwrest, &callback)
kwrest[:name] ||= '%s line %i' % callback.source_location
super(event, **kwrest)
@callback = callback
event.add_filter self end

Class Method Details

.cancel!(result = false) ⇒ Object

フィルタ内部で使う。フィルタの実行をキャンセルする。Plugin#filtering はfalseを返し、イベントのフィルタの場合は、そのイベントの実行自体をキャンセルする。また、 result が渡された場合、Event#filtering の戻り値は result になる。



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

def self.cancel!(result=false)
throw :filter_exit, result end

Instance Method Details

#detachObject

このリスナを削除する

Return

self



48
49
50
# File 'lib/pluggaloid/filter.rb', line 48

def detach
@event.delete_filter(self)
self end

#filtering(*args) ⇒ Object

イベントを実行する

Args

*args

イベントの引数

Return

加工後の引数の配列



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pluggaloid/filter.rb', line 32

def filtering(*args)
  length = args.size
  result = @callback.call(*args, &CANCEL_PROC)
  case
  when THROUGH == result
    args
  when length != result.size
    raise Pluggaloid::FilterError, "filter changes arguments length (#{length} to #{result.size})"
  else
    result
  end
end