wires 0.3.1 → 0.3.3

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
2
  SHA1:
3
- metadata.gz: 8ba5a18e82d8030466a788523b4cf1a35444a7ee
4
- data.tar.gz: 358f486714e9b7b2d91103966a08c6591d6b4b88
3
+ metadata.gz: 5fdeb24aaafdf868235513ef33e917af84f0b592
4
+ data.tar.gz: cbc000d9911fd5e6cb76210468a7ff5e685e3f2a
5
5
  SHA512:
6
- metadata.gz: d04c8d15e06683af78f41167c30443209063ff38561229006ad2d35e94cfb924c570daee44344092e95b81dc9515399f10509fdfdaea5d00dc3ea1375ed4a564
7
- data.tar.gz: ee87a782456ba1f9cd1572edda652cde60e7e4d66e1721e59e21fe8eb660b043f5f2bdd0f90c8bf1b3f68665196c3271906528d55715bc07c563b73892f0f87e
6
+ metadata.gz: 32ffa931974f65688b1d3fad28f9b6700f0fea89db796a476148e9f30962c617fcc9954e7971f7cc8247da544edc281401071e4c7b5c56aa5b2c2b40af6daf0f
7
+ data.tar.gz: c7ae3f0a167d127928ecce570c4f069c735f497f3803de97dde13e15ec8e8f4502190a2df58e65565ff51940d4e156fd385099cf10458264dada31bba0631455
data/lib/wires/channel.rb CHANGED
@@ -1,28 +1,4 @@
1
1
 
2
- module Wires
3
- module Convenience
4
-
5
- def on(events, channels='*', &codeblock)
6
- channels = [channels] unless channels.is_a? Array
7
- for channel in channels
8
- Wires::Channel.new(channel).register(events, codeblock)
9
- end
10
- nil end
11
-
12
- def fire(event, channel='*')
13
- Wires::Channel.new(channel).fire(event, blocking:false)
14
- nil end
15
-
16
- def fire_and_wait(event, channel='*')
17
- Wires::Channel.new(channel).fire(event, blocking:true)
18
- nil end
19
-
20
- def Channel(*args) Wires::Channel.new(*args) end
21
-
22
- end
23
- end
24
-
25
-
26
2
  module Wires
27
3
 
28
4
  class Channel
@@ -40,25 +16,20 @@ module Wires
40
16
  # Don't redefine this instance method!
41
17
  def hub; self.class.hub; end
42
18
 
43
- # Channel registry hash and star channel reference are values
44
- # In this Hash with the key being the reference to the Hub
45
- @@channel_hash = Hash.new
46
- @@channel_star = Hash.new
47
-
48
19
  # Give out references to the star channel
49
- def self.channel_star; @@channel_star[self.hub]; end
50
- def channel_star; @@channel_star[self.hub]; end
20
+ def self.channel_star; @@channel_star; end
21
+ def channel_star; @@channel_star; end
51
22
 
52
23
  # Ensure that there is only one instance of Channel per name
53
24
  @@new_lock = Mutex.new
54
25
  def self.new(*args, &block)
55
26
  (args.include? :recursion_guard) ?
56
27
  (args.delete :recursion_guard) :
57
- (@@channel_star[self.hub] ||= self.new('*', :recursion_guard))
28
+ (@@channel_star ||= self.new('*', :recursion_guard))
58
29
 
59
30
  @@new_lock.synchronize do
60
- @@channel_hash[self.hub] ||= Hash.new
61
- @@channel_hash[self.hub][args[0]] ||= super(*args, &block)
31
+ @@channel_hash ||= Hash.new
32
+ @@channel_hash[args[0]] ||= super(*args, &block)
62
33
  end
63
34
  end
64
35
 
@@ -77,13 +48,42 @@ module Wires
77
48
  @target_list << [events, proc]
78
49
  nil end
79
50
 
51
+ # Register hook to execute before fire - can call multiple times
52
+ def self.before_fire(retain=false, &block)
53
+ @before_fires ||= []
54
+ @before_fires << [block, retain]
55
+ nil end
56
+
57
+ # Register hook to execute after fire - can call multiple times
58
+ def self.after_fire(retain=false, &block)
59
+ @after_fires ||= []
60
+ @after_fires << [block, retain]
61
+ nil end
62
+
63
+ def self.run_hooks(hooks_sym, *exc_args)
64
+ hooks = self.instance_variable_get(hooks_sym.to_sym)
65
+ for hook in hooks
66
+ proc, retain = hook
67
+ proc.call(*exc_args)
68
+ end if hooks
69
+ nil end
70
+
71
+ def self.clear_hooks(hooks_sym, force=false)
72
+ hooks = self.instance_variable_get(hooks_sym.to_sym)
73
+ self.instance_variable_set(hooks_sym.to_sym,
74
+ (force ? [] : hooks.select{|h| h[1]})) if hooks
75
+ nil end
76
+
80
77
  # Fire an event on this channel
81
78
  def fire(event, blocking:false)
79
+
82
80
  backtrace = caller
83
81
 
84
82
  # Create an instance object from one of several acceptable input forms
85
83
  event = Event.new_from event
86
84
 
85
+ self.class.run_hooks(:@before_fires, event, self)
86
+
87
87
  # Fire to each relevant target on each channel
88
88
  for chan in relevant_channels()
89
89
  for target in chan.target_list
@@ -91,10 +91,12 @@ module Wires
91
91
  self.class.hub.spawn(event, string, *target[1], blocking, backtrace)
92
92
  end end end
93
93
 
94
+ self.class.run_hooks(:@after_fires, event, self)
95
+
94
96
  nil end
95
97
 
96
98
  def relevant_channels
97
- return @@channel_hash[hub].values if self==channel_star
99
+ return @@channel_hash.values if self==channel_star
98
100
 
99
101
  relevant = [channel_star]
100
102
  my_names = (self.name.is_a? Array) ? self.name : [self.name]
@@ -107,7 +109,7 @@ module Wires
107
109
  "Cannot fire on Regexp channel: #{self.name}."\
108
110
  " Regexp channels can only used in event handlers." end
109
111
 
110
- for other_chan in @@channel_hash[hub].values
112
+ for other_chan in @@channel_hash.values
111
113
 
112
114
  other_name = other_chan.name
113
115
  other_name = (other_name.respond_to? :channel_name) ? \
@@ -127,6 +129,16 @@ module Wires
127
129
  return relevant.uniq
128
130
  end
129
131
 
132
+ # Compare matching with another Channel
133
+ def =~(other)
134
+ (other.is_a? Channel) ? (other.relevant_channels.include? self) : super
135
+ end
136
+
137
+ hub.before_kill(true) do
138
+ self.clear_hooks(:@before_fires)
139
+ self.clear_hooks(:@after_fires)
140
+ end
141
+
130
142
  end
131
143
 
132
144
  end
@@ -0,0 +1,48 @@
1
+
2
+ module Wires
3
+
4
+ module Convenience
5
+
6
+ def on(events, channels='*', &codeblock)
7
+ channels = [channels] unless channels.is_a? Array
8
+ for channel in channels
9
+ Channel.new(channel).register(events, codeblock)
10
+ end
11
+ nil end
12
+
13
+ def fire(event, channel='*')
14
+ Channel.new(channel).fire(event, blocking:false)
15
+ nil end
16
+
17
+ def fire_and_wait(event, channel='*')
18
+ Channel.new(channel).fire(event, blocking:true)
19
+ nil end
20
+
21
+ def fire_every(interval, event, channel='*', **kwargs)
22
+ Wires::TimeScheduler << \
23
+ Wires::TimeSchedulerItem.new(self, event, channel, **kwargs)
24
+ end
25
+
26
+ def Channel(*args) Channel.new(*args) end
27
+
28
+ class << self
29
+ def prefix_methods(prefix)
30
+
31
+ return unless prefix
32
+ prefix = prefix.to_s
33
+
34
+ instance_methods.each do |thing|
35
+ thing = thing.to_s
36
+ f2 = (prefix+'_'+thing)
37
+ f2 = (thing[0]=~/[[:lower:]]/) ? f2.underscore : f2.camelcase
38
+ f2 = f2.to_sym; thing = thing.to_sym
39
+ alias_method f2, thing
40
+ remove_method thing
41
+ end
42
+
43
+ end
44
+ end
45
+
46
+ end
47
+
48
+ end
data/lib/wires/event.rb CHANGED
@@ -1,30 +1,27 @@
1
1
 
2
2
  module Wires
3
- # Store a list of all Event classes that get loaded.
4
- class EventRegistry
5
- @@registry = []
6
-
7
- def self.<<(cls)
8
- @@registry << cls
9
- @@registry.uniq!
10
- end
11
-
12
- def self.list
13
- @@registry
3
+
4
+ class Event
5
+ class << self
6
+ def event_registry_create
7
+ @@registry = []
8
+ event_registry_register
9
+ end
10
+ def event_registry_register(cls=self)
11
+ @@registry << cls
12
+ @@registry.uniq!
13
+ end
14
14
  end
15
+ event_registry_create
15
16
  end
16
17
 
17
18
  # All Event classes should inherit from this one
18
- class Event < Object # explicit for the sake of Event.ancestry
19
-
20
- # Register with the EventRegistry and make subclasses do the same
21
- EventRegistry << self
19
+ class Event
22
20
 
23
21
  # Operate on the metaclass as a type of singleton pattern
24
22
  class << self
25
23
 
26
24
  def inherited(subcls)
27
-
28
25
  # Be sure codestring doesn't conflict
29
26
  existing = _from_codestring(subcls.codestring)
30
27
  if existing then raise NameError, \
@@ -32,10 +29,9 @@ module Wires
32
29
  " existing Event subclass '#{existing}'."\
33
30
  " The generated codestring '#{subcls.codestring}'"\
34
31
  " must be unique for each Event subclass." end
35
-
36
- # Register, then call super
37
- EventRegistry << subcls
32
+
38
33
  super
34
+ event_registry_register(subcls)
39
35
  end
40
36
 
41
37
  # List of class inheritance lineage back to but excluding Object
@@ -60,8 +56,7 @@ module Wires
60
56
  # Pull class from registry by codestring
61
57
  # (more reliable than crafting a reverse regexp)
62
58
  def _from_codestring(str)
63
- return EventRegistry.list
64
- .select{|e| e.codestring==str}[0]
59
+ return @@registry.select{|e| e.codestring==str}[0]
65
60
  end; private :_from_codestring
66
61
 
67
62
  def from_codestring(str)
@@ -84,7 +79,7 @@ module Wires
84
79
  when Event
85
80
  input
86
81
  when Class
87
- input.new(*args) if input < Event
82
+ input.new(*args) if input <= Event
88
83
  else
89
84
  Event.from_codestring(input.to_s).new(*args)
90
85
  end
@@ -96,9 +91,9 @@ module Wires
96
91
  def new(*args, &block)
97
92
  obj = super
98
93
 
99
- kwargs = args[-1].is_a?(Hash) ? args.pop : Hash.new
100
- kwargs[:kwargs] = kwargs.clone.freeze
101
- kwargs[:args] = args
94
+ kwargs = args[-1].is_a?(Hash) ? args.pop.dup : Hash.new
95
+ kwargs[:kwargs] = kwargs.dup.freeze
96
+ kwargs[:args] = args.dup.freeze
102
97
  kwargs[:codeblock] = block if block
103
98
  for key in kwargs.keys
104
99
  att = key.to_s
@@ -114,15 +109,24 @@ module Wires
114
109
 
115
110
  # Calling super in new with *args will complain if this isn't here
116
111
  def initialize(*args, &block) end
112
+
117
113
  end
118
114
 
119
-
120
115
  #
121
116
  # Comparison support for Events and Symbols/Strings
122
117
  #
123
118
 
124
119
  # Reopen Event and add comparison functions
125
120
  class Event
121
+
122
+ def =~(other)
123
+ (other.is_a? Event) ?
124
+ ((self.class >= other.class) \
125
+ and (not self.kwargs.each_pair.detect{|k,v| other.kwargs[k]!=v}) \
126
+ and (not self.args.each_with_index.detect{|a,i| other.args[i]!=a})) :
127
+ super
128
+ end
129
+
126
130
  class << self
127
131
  def ==(other)
128
132
  other.is_a?(Class) ?
data/lib/wires/hub.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  module Wires
3
3
  # An Event Hub. Event/proc associations come in, and the procs
4
4
  # get called in new threads in the order received
5
- class Hub
5
+ class self::Hub
6
6
  # Operate on the metaclass as a type of singleton pattern
7
7
  class << self
8
8
 
@@ -25,10 +25,12 @@ module Wires
25
25
  @spawning_count = 0
26
26
  @spawning_count_lock = Monitor.new
27
27
 
28
- @before_runs = Queue.new
29
- @after_runs = Queue.new
28
+ @before_runs = Queue.new
29
+ @after_runs = Queue.new
30
30
  @before_kills = Queue.new
31
31
  @after_kills = Queue.new
32
+ @before_fires = []
33
+ @after_fires = []
32
34
 
33
35
  @please_finish_all = false
34
36
 
@@ -119,7 +121,7 @@ module Wires
119
121
 
120
122
  # Spawn a task
121
123
  def spawn(*args) # :args: event, ch_string, proc, blocking, fire_bt
122
-
124
+
123
125
  @spawning_count_lock.synchronize { @spawning_count += 1 }
124
126
 
125
127
  return neglect(*args) if dead?
@@ -240,19 +242,34 @@ module Wires
240
242
  nil end
241
243
 
242
244
  # Flush/run queue of [proc, retain]s, retaining those with retain==true
243
- def run_hooks(hooks)
245
+ def run_hooks(hooks, *args)
244
246
  retained = Queue.new
245
247
  while not hooks.empty?
246
248
  proc, retain = hooks.shift
247
249
  retained << [proc, retain] if retain
248
- proc.call
249
- # flush_queue if alive?
250
+ proc.call(*args)
250
251
  end
251
252
  while not retained.empty?
252
253
  hooks << retained.shift
253
254
  end
254
255
  nil end
255
256
 
257
+ # Run queue of [proc, retain]s, retaining all
258
+ def run_fire_hooks(hooks, *args)
259
+ for hook in hooks
260
+ proc, retain = hook
261
+ proc.call(*args)
262
+ end
263
+ nil end
264
+
265
+ # Clear queue of [proc, retain]s, retaining those with retain==true
266
+ def clear_fire_hooks(hooks, *args)
267
+ hooks.select! do |hook|
268
+ proc, retain = hook
269
+ retain
270
+ end
271
+ nil end
272
+
256
273
  end
257
274
 
258
275
 
@@ -292,6 +309,8 @@ module Wires
292
309
  before { join_children if @please_finish_all }
293
310
  after { @please_finish_all = false }
294
311
  after { run_hooks @after_kills }
312
+ after { clear_fire_hooks @before_fires }
313
+ after { clear_fire_hooks @after_fires }
295
314
  end
296
315
  end
297
316
 
data/lib/wires/time.rb CHANGED
@@ -177,13 +177,13 @@ module Wires
177
177
  end
178
178
 
179
179
  # Start the main loop upon run of Hub
180
- Hub.after_run(retain:true) do
180
+ Hub.after_run(true) do
181
181
  @keepgoing = true
182
182
  @thread = Thread.new { main_loop }
183
183
  end
184
184
 
185
185
  # Stop the main loop upon death of Hub
186
- Hub.before_kill(retain:true) do
186
+ Hub.before_kill(true) do
187
187
  Thread.exclusive do
188
188
  @keepgoing=false
189
189
  @next_pass=Time.now
data/lib/wires.rb CHANGED
@@ -10,4 +10,6 @@ require 'wires/hub'
10
10
  require 'wires/channel'
11
11
  require 'wires/time'
12
12
 
13
- include Wires::Convenience # require 'wires/clean' to uninclude Convenience
13
+ require 'wires/convenience'
14
+ include Wires::Convenience # require 'wires/clean' to uninclude Convenience
15
+
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wires
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe McIlvain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-23 00:00:00.000000000 Z
11
+ date: 2013-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activesupport
14
+ name: activesupport-core-ext
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ~>
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: minitest
56
+ name: wires-test
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - '>='
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: turn
70
+ name: jemc-reporter
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - '>='
@@ -106,6 +106,7 @@ files:
106
106
  - lib/wires/hub.rb
107
107
  - lib/wires/expect_type.rb
108
108
  - lib/wires/clean.rb
109
+ - lib/wires/convenience.rb
109
110
  - lib/wires/channel.rb
110
111
  - lib/wires/event.rb
111
112
  - LICENSE
@@ -130,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
131
  version: '0'
131
132
  requirements: []
132
133
  rubyforge_project:
133
- rubygems_version: 2.0.2
134
+ rubygems_version: 2.0.3
134
135
  signing_key:
135
136
  specification_version: 4
136
137
  summary: wires