seh 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/seh/event.rb +79 -8
  2. data/lib/seh/version.rb +1 -1
  3. data/seh.gemspec +10 -2
  4. metadata +26 -4
@@ -2,17 +2,32 @@ require 'ostruct'
2
2
 
3
3
  module Seh
4
4
  module Private
5
+ START = 0
6
+ BEFORE = 100
7
+
8
+ BEFORE_SUCCESS = 250
9
+ BEFORE_FAILURE = 250
10
+
11
+ SUCCESS = 500
12
+ FAILURE = 500
13
+
14
+ AFTER_SUCCESS = 750
15
+ AFTER_FAILURE = 750
16
+
17
+ AFTER = 900
18
+ FINISH = 1000
19
+
5
20
  class EventData
6
- attr_accessor :types, :target, :time, :start, :finish
21
+ attr_accessor :types, :target, :time, :staged_handlers, :success
7
22
 
8
23
  def initialize
9
24
  @types = []
10
25
  @target = nil
11
26
  @time = Time.now
27
+ @success = true
12
28
 
13
29
  # staged handlers
14
- @start = []
15
- @finish = []
30
+ @staged_handlers = {}
16
31
  end
17
32
  end
18
33
 
@@ -42,12 +57,19 @@ module Seh
42
57
  instance_eval(&block) if block
43
58
  end
44
59
 
60
+ def fail
61
+ @data.success = false
62
+ end
63
+
64
+ def success?
65
+ @data.success
66
+ end
67
+
45
68
  def dispatch
46
69
  raise "Event may only be dispatched once" unless @state == Private::EventStateReady
47
70
  @state = Private::EventStateInflight
48
- @data.target.each_bind { |bind| bind.block.call self if bind.event_type.match @data.types }
49
- @data.start.each { |block| block.call self }
50
- @data.finish.each { |block| block.call self }
71
+ collect_targets.each { |t| t.each_bind { |bind| bind.block.call self if bind.event_type.match @data.types } }
72
+ @data.staged_handlers.each_key.sort.each { |stage| @data.staged_handlers[stage].each { |block| block.call self } }
51
73
  @state = Private::EventStateDone
52
74
  end
53
75
 
@@ -69,11 +91,60 @@ module Seh
69
91
  end
70
92
 
71
93
  def start(&block)
72
- @data.start << block if block_given?
94
+ staged_handler Private::START, block if block_given?
95
+ end
96
+
97
+ def before(&block)
98
+ staged_handler Private::BEFORE, block if block_given?
99
+ end
100
+
101
+ def before_success(&block)
102
+ staged_handler Private::BEFORE_SUCCESS, ->e{ block.call e if e.success? } if block_given?
103
+ end
104
+
105
+ def before_failure(&block)
106
+ staged_handler Private::BEFORE_FAILURE, ->e{ block.call e unless e.success? } if block_given?
107
+ end
108
+
109
+ def success(&block)
110
+ staged_handler Private::SUCCESS, ->e{ block.call e if e.success? } if block_given?
111
+ end
112
+
113
+ def failure(&block)
114
+ staged_handler Private::FAILURE, ->e{ block.call e unless e.success? } if block_given?
115
+ end
116
+
117
+ def after_success(&block)
118
+ staged_handler Private::AFTER_SUCCESS, ->e{ block.call e if e.success? } if block_given?
119
+ end
120
+
121
+ def after_failure(&block)
122
+ staged_handler Private::AFTER_FAILURE, ->e{ block.call e unless e.success? } if block_given?
123
+ end
124
+
125
+ def after(&block)
126
+ staged_handler Private::AFTER, block if block_given?
73
127
  end
74
128
 
75
129
  def finish(&block)
76
- @data.finish << block if block_given?
130
+ staged_handler Private::FINISH, block if block_given?
131
+ end
132
+
133
+ private
134
+ def staged_handler( stage, block )
135
+ @data.staged_handlers[stage] ||= []
136
+ @data.staged_handlers[stage] << block
137
+ nil
138
+ end
139
+
140
+ def collect_targets
141
+ targets_working = [@data.target]
142
+ targets_final = []
143
+ while t = targets_working.shift do
144
+ targets_final << t
145
+ targets_working.concat t.parents if t.respond_to? :parents
146
+ end
147
+ targets_final.uniq
77
148
  end
78
149
  end
79
150
  end
@@ -1,3 +1,3 @@
1
1
  module Seh
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -8,8 +8,16 @@ Gem::Specification.new do |s|
8
8
  s.authors = ["Ryan Berckmans"]
9
9
  s.email = ["ryan.berckmans@gmail.com"]
10
10
  s.homepage = "https://github.com/ryanberckmans/seh"
11
- s.summary = "event handling similar to w3c dom events; pre-alpha wip"
12
- s.description = ""
11
+ s.summary = "pure ruby event handling similar to w3c dom events; pre-alpha wip"
12
+ s.description = "Pure ruby event handling similar to w3c dom events. Lots of bells and whistles to support complex event handling as required by stuff like video games.
13
+ + event handling in a synchronous specific order
14
+ + event targets can have multiple parents and common ancestors; event propagation does a breadth first search traversal over a directed acyclic event target graph
15
+ + staged callbacks: event.before { .. }; event.after { .. }
16
+ + staged callbacks allow for an ancestor to influence affect of event on a descendant: ancestor.before { |event| event.x = 5 }; descendant.after { |event| puts event.x }
17
+ + events can have multiple types, and types can inherit from other types
18
+ + bind callbacks using event type filtering: node.bind(overcast AND (rain OR snow)) { |event| callback! }
19
+ + optional event failure: event.success { yay! }; event.failure { oops! }
20
+ + events on stack don't care about other events above/below - event A, currently executing, can create/dispatch/finish another event B"
13
21
 
14
22
  s.rubyforge_project = "seh"
15
23
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,31 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-15 00:00:00.000000000Z
12
+ date: 2011-06-16 00:00:00.000000000Z
13
13
  dependencies: []
14
- description: ''
14
+ description: ! 'Pure ruby event handling similar to w3c dom events. Lots of bells
15
+ and whistles to support complex event handling as required by stuff like video games.
16
+
17
+ + event handling in a synchronous specific order
18
+
19
+ + event targets can have multiple parents and common ancestors; event propagation
20
+ does a breadth first search traversal over a directed acyclic event target graph
21
+
22
+ + staged callbacks: event.before { .. }; event.after { .. }
23
+
24
+ + staged callbacks allow for an ancestor to influence affect of event on a descendant:
25
+ ancestor.before { |event| event.x = 5 }; descendant.after { |event| puts event.x
26
+ }
27
+
28
+ + events can have multiple types, and types can inherit from other types
29
+
30
+ + bind callbacks using event type filtering: node.bind(overcast AND (rain OR snow))
31
+ { |event| callback! }
32
+
33
+ + optional event failure: event.success { yay! }; event.failure { oops! }
34
+
35
+ + events on stack don''t care about other events above/below - event A, currently
36
+ executing, can create/dispatch/finish another event B'
15
37
  email:
16
38
  - ryan.berckmans@gmail.com
17
39
  executables: []
@@ -53,5 +75,5 @@ rubyforge_project: seh
53
75
  rubygems_version: 1.8.5
54
76
  signing_key:
55
77
  specification_version: 3
56
- summary: event handling similar to w3c dom events; pre-alpha wip
78
+ summary: pure ruby event handling similar to w3c dom events; pre-alpha wip
57
79
  test_files: []