pluggaloid 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 22d2fff547e70f4cbdd07e07df3c632a6675e63b
4
- data.tar.gz: 2716bfd1298173ec228fb7b6abe0bca74a4ff27e
2
+ SHA256:
3
+ metadata.gz: c2e0d7c80c5e6e013d8adfd22d6e242fb15bd00e1df0b1667443132c4307e846
4
+ data.tar.gz: 647c05b3f6e5083798ec8d90852dc16c6f29ff539f110b317340eaee50b01a25
5
5
  SHA512:
6
- metadata.gz: a6ac01165703f2d295e3607ab7c0ead1647336d5f7f419f2aebdb9454f491a464a42d72ef4ae6bae9072737b733258b2abd62f8529114575797509b2cca0c890
7
- data.tar.gz: 6b496c9548ffc9e1c5dbb601a7de3b43fcacca41ad76d0660d4fd4b21c02890db2ff90549e3971bd862563cd9039418201fe690abdc6405883d1ba64272a65a9
6
+ metadata.gz: 5a629bf91f585251405c72a7e561f9de3275d4d20fd3bfad2cfc511c1c1acd47f17d365cc3a51b7417e20310d846f1fbeb7d0253aa6a61a648af5a81959a7826
7
+ data.tar.gz: d890f710693d94ba7564f1744e51fd18b83e00b4c4597085e254b6da302d7eaa1c6db53ef2129aab6f981c68c295aa5e8b4521099ff78e6e006b87dea50ab2cb
@@ -1,6 +1,8 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  class Pluggaloid::Event
4
+ Lock = Mutex.new
5
+
4
6
  include InstanceStorage
5
7
 
6
8
  # オプション。以下のキーを持つHash
@@ -14,8 +16,9 @@ class Pluggaloid::Event
14
16
  def initialize(*args)
15
17
  super
16
18
  @options = {}
17
- @listeners = []
18
- @filters = [] end
19
+ @listeners = [].freeze
20
+ @filters = [].freeze
21
+ end
19
22
 
20
23
  def vm
21
24
  self.class.vm end
@@ -60,15 +63,25 @@ class Pluggaloid::Event
60
63
 
61
64
  def add_listener(listener)
62
65
  unless listener.is_a? Pluggaloid::Listener
63
- raise Pluggaloid::ArgumentError, "First argument must be Pluggaloid::Listener, but given #{listener.class}." end
64
- if @listeners.map(&:slug).include?(listener.slug)
65
- raise Pluggaloid::DuplicateListenerSlugError, "Listener slug #{listener.slug} already exists." end
66
- @listeners << listener
67
- self end
66
+ raise Pluggaloid::ArgumentError, "First argument must be Pluggaloid::Listener, but given #{listener.class}."
67
+ end
68
+ Lock.synchronize do
69
+ if @listeners.map(&:slug).include?(listener.slug)
70
+ raise Pluggaloid::DuplicateListenerSlugError, "Listener slug #{listener.slug} already exists."
71
+ end
72
+ @listeners = [*@listeners, listener].freeze
73
+ end
74
+ self
75
+ end
68
76
 
69
- def delete_listener(event_filter)
70
- @listeners.delete(event_filter)
71
- self end
77
+ def delete_listener(listener)
78
+ Lock.synchronize do
79
+ @listeners = @listeners.dup
80
+ @listeners.delete(listener)
81
+ @listeners.freeze
82
+ end
83
+ self
84
+ end
72
85
 
73
86
  # イベントフィルタを追加する
74
87
  # ==== Args
@@ -78,8 +91,9 @@ class Pluggaloid::Event
78
91
  def add_filter(event_filter)
79
92
  unless event_filter.is_a? Pluggaloid::Filter
80
93
  raise Pluggaloid::ArgumentError, "First argument must be Pluggaloid::Filter, but given #{event_filter.class}." end
81
- @filters << event_filter
82
- self end
94
+ @filters = [*@filters, event_filter].freeze
95
+ self
96
+ end
83
97
 
84
98
  # イベントフィルタを削除する
85
99
  # ==== Args
@@ -87,14 +101,22 @@ class Pluggaloid::Event
87
101
  # ==== Return
88
102
  # self
89
103
  def delete_filter(event_filter)
90
- @filters.delete(event_filter)
91
- self end
104
+ Lock.synchronize do
105
+ @filters = @filters.dup
106
+ @filters.delete(event_filter)
107
+ @filters.freeze
108
+ end
109
+ self
110
+ end
92
111
 
93
112
  private
94
113
  def call_all_listeners(args)
95
114
  catch(:plugin_exit) do
96
115
  @listeners.each do |listener|
97
- listener.call(*args) end end end
116
+ listener.call(*args)
117
+ end
118
+ end
119
+ end
98
120
 
99
121
  class << self
100
122
  attr_accessor :filter_another_thread, :vm
@@ -1,12 +1,17 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  class Pluggaloid::Filter < Pluggaloid::Handler
4
+ NotConverted = Class.new
5
+ THROUGH = NotConverted.new.freeze
6
+
4
7
  # フィルタ内部で使う。フィルタの実行をキャンセルする。Plugin#filtering はfalseを返し、
5
8
  # イベントのフィルタの場合は、そのイベントの実行自体をキャンセルする。
6
9
  # また、 _result_ が渡された場合、Event#filtering の戻り値は _result_ になる。
7
10
  def self.cancel!(result=false)
8
11
  throw :filter_exit, result end
9
12
 
13
+ CANCEL_PROC = method(:cancel!)
14
+
10
15
  # ==== Args
11
16
  # [event] 監視するEventのインスタンス
12
17
  # [name:] 名前(String | nil)
@@ -15,7 +20,7 @@ class Pluggaloid::Filter < Pluggaloid::Handler
15
20
  # [&callback] コールバック
16
21
  def initialize(event, **kwrest, &callback)
17
22
  super
18
- @callback = Proc.new
23
+ @callback = callback
19
24
  event.add_filter self end
20
25
 
21
26
  # イベントを実行する
@@ -25,10 +30,16 @@ class Pluggaloid::Filter < Pluggaloid::Handler
25
30
  # 加工後の引数の配列
26
31
  def filtering(*args)
27
32
  length = args.size
28
- result = @callback.call(*args, &self.class.method(:cancel!))
29
- if length != result.size
30
- raise Pluggaloid::FilterError, "filter changes arguments length (#{length} to #{result.size})" end
31
- result end
33
+ result = @callback.call(*args, &CANCEL_PROC)
34
+ case
35
+ when THROUGH == result
36
+ args
37
+ when length != result.size
38
+ raise Pluggaloid::FilterError, "filter changes arguments length (#{length} to #{result.size})"
39
+ else
40
+ result
41
+ end
42
+ end
32
43
 
33
44
  # このリスナを削除する
34
45
  # ==== Return
@@ -48,12 +48,12 @@ class Pluggaloid::HandlerTag < Pluggaloid::Identity
48
48
  # このTagがついている Pluggaloid::Listener と Pluggaloid::Filter を全て列挙する
49
49
  # ==== Return
50
50
  # Enumerable
51
- def each
52
- if block_given?
51
+ def each(&block)
52
+ if block
53
53
  Enumerator.new do |y|
54
54
  listeners{|x| y << x }
55
55
  filters{|x| y << x }
56
- end.each(&Proc.new)
56
+ end.each(&block)
57
57
  else
58
58
  Enumerator.new do |y|
59
59
  listeners{|x| y << x }
@@ -65,9 +65,9 @@ class Pluggaloid::HandlerTag < Pluggaloid::Identity
65
65
  # このTagがついている Pluggaloid::Listener を全て列挙する
66
66
  # ==== Return
67
67
  # Enumerable
68
- def listeners
69
- if block_given?
70
- listeners.each(&Proc.new)
68
+ def listeners(&block)
69
+ if block
70
+ listeners.each(&block)
71
71
  else
72
72
  @plugin.to_enum(:listeners).lazy.select{|l| l.tags.include?(self) }
73
73
  end
@@ -76,9 +76,9 @@ class Pluggaloid::HandlerTag < Pluggaloid::Identity
76
76
  # このTagがついている Pluggaloid::Filter を全て列挙する
77
77
  # ==== Return
78
78
  # Enumerable
79
- def filters
80
- if block_given?
81
- filters.each(&Proc.new)
79
+ def filters(&block)
80
+ if block
81
+ filters.each(&block)
82
82
  else
83
83
  @plugin.to_enum(:filters).lazy.select{|l| l.tags.include?(self) }
84
84
  end
@@ -15,7 +15,7 @@ class Pluggaloid::Listener < Pluggaloid::Handler
15
15
  # [&callback] コールバック
16
16
  def initialize(event, **kwrest, &callback)
17
17
  super
18
- @callback = Proc.new
18
+ @callback = callback
19
19
  event.add_listener(self) end
20
20
 
21
21
  # イベントを実行する
@@ -134,7 +134,7 @@ module Pluggaloid
134
134
  # [name] タグ名
135
135
  # ==== Return
136
136
  # Pluggaloid::HandlerTag
137
- def handler_tag(slug=SecureRandom.uuid, name=slug)
137
+ def handler_tag(slug=SecureRandom.uuid, name=slug, &block)
138
138
  tag = case slug
139
139
  when String, Symbol
140
140
  vm.HandlerTag.new(slug: slug.to_sym, name: name.to_s, plugin: self)
@@ -143,9 +143,9 @@ module Pluggaloid
143
143
  else
144
144
  raise Pluggaloid::TypeError, "Argument `slug' must be instance of Symbol, String or Pluggaloid::HandlerTag, but given #{slug.class}."
145
145
  end
146
- if block_given?
146
+ if block
147
147
  handlers = @events + @filters
148
- yield tag
148
+ block.(tag)
149
149
  (@events + @filters - handlers).each do |handler|
150
150
  handler.add_tag(tag)
151
151
  end
@@ -157,9 +157,9 @@ module Pluggaloid
157
157
  # イベントリスナを列挙する
158
158
  # ==== Return
159
159
  # Set of Pluggaloid::Listener
160
- def listeners
161
- if block_given?
162
- @events.each(&Proc.new)
160
+ def listeners(&block)
161
+ if block
162
+ @events.each(&block)
163
163
  else
164
164
  @events.dup
165
165
  end
@@ -168,9 +168,9 @@ module Pluggaloid
168
168
  # フィルタを列挙する
169
169
  # ==== Return
170
170
  # Set of Pluggaloid::Filter
171
- def filters
172
- if block_given?
173
- @filters.each(&Proc.new)
171
+ def filters(&block)
172
+ if block
173
+ @filters.each(&block)
174
174
  else
175
175
  @filters.dup
176
176
  end
@@ -232,8 +232,7 @@ module Pluggaloid
232
232
  self end
233
233
 
234
234
  # プラグインが Plugin.uninstall される時に呼ばれるブロックを登録する。
235
- def onunload
236
- callback = Proc.new
235
+ def onunload(&callback)
237
236
  add_event(:unload) do |plugin_slug|
238
237
  if plugin_slug == self.name
239
238
  callback.call
@@ -246,15 +245,16 @@ module Pluggaloid
246
245
  # on_?name :: add_event(name)
247
246
  # filter_?name :: add_event_filter(name)
248
247
  def method_missing(method, *args, **kwrest, &proc)
249
- case method.to_s
250
- when /\Aon_?(.+)\Z/
251
- add_event($1.to_sym, *args, **kwrest, &proc)
252
- when /\Afilter_?(.+)\Z/
253
- add_event_filter($1.to_sym, **kwrest, &proc)
254
- when /\Ahook_?(.+)\Z/
255
- add_event_hook($1.to_sym, &proc)
256
- else
257
- super end end
248
+ method_name = method.to_s
249
+ case
250
+ when method_name.start_with?('on')
251
+ event_name = method_name[(method_name[2] == '_' ? 3 : 2)..method_name.size]
252
+ add_event(event_name.to_sym, *args, **kwrest, &proc)
253
+ when method_name.start_with?('filter')
254
+ event_name = method_name[(method_name[6] == '_' ? 7 : 6)..method_name.size]
255
+ add_event_filter(event_name.to_sym, **kwrest, &proc)
256
+ end
257
+ end
258
258
 
259
259
  private
260
260
 
@@ -1,3 +1,3 @@
1
1
  module Pluggaloid
2
- VERSION = "1.1.1"
2
+ VERSION = "1.1.2"
3
3
  end
data/pluggaloid.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["toshi.alternative@gmail.com"]
11
11
  spec.summary = %q{Extensible plugin system}
12
12
  spec.description = %q{Pluggaloid is extensible plugin system for mikutter.}
13
- spec.homepage = ""
13
+ spec.homepage = "https://rubygems.org/gems/pluggaloid"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
data/test/helper.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module PluggaloidTestHelper
4
- def eval_all_events(delayer=Delayer)
4
+ def eval_all_events(delayer=Delayer, &block)
5
5
  native = Thread.list
6
- yield if block_given?
6
+ block.() if block
7
7
  delayer.run while not(delayer.empty? and (Thread.list - native).empty?)
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pluggaloid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshiaki Asai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-09 00:00:00.000000000 Z
11
+ date: 2019-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: delayer
@@ -113,7 +113,7 @@ files:
113
113
  - test/helper.rb
114
114
  - test/multi_vm_test.rb
115
115
  - test/plugin_test.rb
116
- homepage: ''
116
+ homepage: https://rubygems.org/gems/pluggaloid
117
117
  licenses:
118
118
  - MIT
119
119
  metadata: {}
@@ -132,8 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  - !ruby/object:Gem::Version
133
133
  version: '0'
134
134
  requirements: []
135
- rubyforge_project:
136
- rubygems_version: 2.5.1
135
+ rubygems_version: 3.0.3
137
136
  signing_key:
138
137
  specification_version: 4
139
138
  summary: Extensible plugin system