Class: Pluggaloid::Event

Inherits:
Object
  • Object
show all
Includes:
InstanceStorage
Defined in:
lib/pluggaloid/event.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Event

Returns a new instance of Event.



14
15
16
17
18
# File 'lib/pluggaloid/event.rb', line 14

def initialize(*args)
super
@options = {}
@listeners = []
@filters = [] end

Class Attribute Details

.filter_another_threadObject

Returns the value of attribute filter_another_thread.



98
99
100
# File 'lib/pluggaloid/event.rb', line 98

def filter_another_thread
  @filter_another_thread
end

.vmObject

Returns the value of attribute vm.



98
99
100
# File 'lib/pluggaloid/event.rb', line 98

def vm
  @vm
end

Instance Attribute Details

#optionsObject

オプション。以下のキーを持つHash

:prototype

引数の数と型。Arrayで、type_strictが解釈できる条件を設定する

:priority

Delayerの優先順位



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

def options
  @options
end

Class Method Details

.__clear_aF4e__Object



100
# File 'lib/pluggaloid/event.rb', line 100

alias __clear_aF4e__ clear!

.clear!Object



101
102
103
104
# File 'lib/pluggaloid/event.rb', line 101

def clear!
  @filter_another_thread = false
  __clear_aF4e__()
end

Instance Method Details

#add_filter(event_filter) ⇒ Object

イベントフィルタを追加する

Args

event_filter

イベントフィルタ(Filter)

Return

self



76
77
78
79
80
# File 'lib/pluggaloid/event.rb', line 76

def add_filter(event_filter)
unless event_filter.is_a? Pluggaloid::Filter
  raise Pluggaloid::ArgumentError, "First argument must be Pluggaloid::Filter, but given #{event_filter.class}." end
@filters << event_filter
self end

#add_listener(listener) ⇒ Object



61
62
63
64
65
# File 'lib/pluggaloid/event.rb', line 61

def add_listener(listener)
unless listener.is_a? Pluggaloid::Listener
  raise Pluggaloid::ArgumentError, "First argument must be Pluggaloid::Listener, but given #{listener.class}." end
@listeners << listener
self end

#call(*args) ⇒ Object

イベントを引数 args で発生させる

Args

*args

イベントの引数

Return

Delayerか、イベントを待ち受けているリスナがない場合はnil



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pluggaloid/event.rb', line 35

def call(*args)
    if self.class.filter_another_thread
      if @filters.empty?
vm.delayer.new(*Array(priority)) do
call_all_listeners(args) end
      else
Thread.new do
  filtered_args = filtering(*args)
  if filtered_args.is_a? Array
    vm.Delayer.new(*Array(priority)) do
      call_all_listeners(filtered_args) end end end end
    else
      vm.Delayer.new(*Array(priority)) do
args = filtering(*args) if not @filters.empty?
call_all_listeners(args) if args.is_a? Array end end end

#delete_filter(event_filter) ⇒ Object

イベントフィルタを削除する

Args

event_filter

イベントフィルタ(EventFilter)

Return

self



87
88
89
# File 'lib/pluggaloid/event.rb', line 87

def delete_filter(event_filter)
@filters.delete(event_filter)
self end

#delete_listener(event_filter) ⇒ Object



67
68
69
# File 'lib/pluggaloid/event.rb', line 67

def delete_listener(event_filter)
@listeners.delete(event_filter)
self end

#filtering(*args) ⇒ Object

引数 args をフィルタリングした結果を返す

Args

*args

引数

Return

フィルタされた引数の配列



56
57
58
59
# File 'lib/pluggaloid/event.rb', line 56

def filtering(*args)
    catch(:filter_exit) {
      @filters.reduce(args){ |acm, event_filter|
event_filter.filtering(*acm) } } end

#priorityObject

イベントの優先順位を取得する

Return

プラグインの優先順位



26
27
28
# File 'lib/pluggaloid/event.rb', line 26

def priority
    if @options.has_key? :priority
@options[:priority] end end

#vmObject



20
21
# File 'lib/pluggaloid/event.rb', line 20

def vm
self.class.vm end