serf 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -200,16 +200,21 @@ Example
200
200
  @logger.info "RegExp Matched #{message.inspect}"
201
201
  nil
202
202
  end
203
+
204
+ def call(message={})
205
+ @logger.info "A message matched an empty action part in the target"
206
+ nil
207
+ end
203
208
  end
204
209
 
205
210
  # my_lib/routes.rb
206
211
  ROUTES = {
207
212
  # Declare a matcher and a list of routes to endpoints.
208
213
  'my_message' => [{
209
- # Declares which handler to use. This is the tableized
210
- # name of the class. It will be constantized by the serf code.
211
- handler: 'my_handler',
212
- action: :submit_my_message,
214
+ # Declares which handler and action (method) of the handler
215
+ # to call. The handler part is the name of the handler used
216
+ # to register an actual handler object.
217
+ target: 'my_handler#submit_my_message',
213
218
 
214
219
  # Define a parser that will build up a message object.
215
220
  # Default: nil, no parsing done.
@@ -220,17 +225,19 @@ Example
220
225
  #background: false
221
226
  }],
222
227
  'other_message' => [{
223
- handler: 'my_handler',
224
- action: :submit_other_message,
228
+ target: 'my_handler#submit_other_message',
225
229
  background: true
226
230
  }, {
227
- handler: 'my_handler',
228
- action: :raises_error,
231
+ target: 'my_handler#raises_error',
229
232
  background: true
230
- }],
233
+ },
234
+ # This is just a string route defining the target, nothing else.
235
+ # The handler is 'my_handler' and an empty (or missing) action
236
+ # part defaults to the 'call' method of the handler.
237
+ 'my_handler'
238
+ ],
231
239
  /^events\/.*$/ => [{
232
- handler: 'my_handler',
233
- action: :regexp_matched,
240
+ target: 'my_handler#regexp_matched',
234
241
  background: true
235
242
  }]
236
243
  }
@@ -279,8 +286,9 @@ Example
279
286
  logger.info "In Tick, MyMessage Results: #{results.inspect}"
280
287
 
281
288
  # This will submit 'other_message' to be handled in foreground
282
- # Because we declared the 'other_message' kind to be handled async, we
283
- # should get a MessageAcceptedEvent as the results.
289
+ # Because we declared the 'other_message' kind to be handled async
290
+ # in each route config, we should get a MessageAcceptedEvent as
291
+ # the results.
284
292
  results = app.call('kind' => 'other_message')
285
293
  logger.info "In Tick, OtherMessage Results: #{results.inspect}"
286
294
 
data/lib/serf/builder.rb CHANGED
@@ -35,7 +35,12 @@ module Serf
35
35
  @route_maps = []
36
36
  @handlers = {}
37
37
  @message_parsers = {}
38
- @not_found = app
38
+ @not_found = app || proc do
39
+ raise ArgumentError, 'Handler Not Found'
40
+ end
41
+
42
+ # Default option in route_configs for background is 'false'
43
+ @background = false
39
44
 
40
45
  # Factories to build objects that wire our Serf App together.
41
46
  # Note that these default implementing classes are also factories
@@ -79,6 +84,10 @@ module Serf
79
84
  @not_found = app
80
85
  end
81
86
 
87
+ def background(run_in_background)
88
+ @background = run_in_background
89
+ end
90
+
82
91
  def serfer_factory(serfer_factory)
83
92
  @serfer_factory = serfer_factory
84
93
  end
@@ -114,28 +123,33 @@ module Serf
114
123
  @route_maps.each do |route_map|
115
124
  route_map.each do |matcher, route_configs|
116
125
  route_configs.each do |route_config|
126
+ # If the passed in route_config was a String, then we place
127
+ # it in an route config as the 'target' field and leave all
128
+ # other options as default.
129
+ config = (route_config.is_a?(String) ?
130
+ { target: route_config } :
131
+ route_config)
132
+
117
133
  # Get the required handler.
118
134
  # Raises error if handler wasn't declared in config.
135
+ target = config.fetch :target
136
+ handler_name, action = handler_and_action target
137
+
119
138
  # Raises error if handler wasn't registered with builder.
120
- handler_name = route_config.fetch :handler
121
139
  handler = @handlers.fetch handler_name
122
140
 
123
- # Get the required action/method of the handler.
124
- # Raises error if route_config doesn't have it.
125
- action = route_config.fetch :action
126
-
127
141
  # Lookup the parser if it was defined.
128
142
  # The Parser MAY be either an object or string.
129
143
  # If String, then we're going to look up in parser map.
130
144
  # Raises an error if a parser (string) was declared, but not
131
145
  # registered with the builder.
132
- parser = route_config[:message_parser]
146
+ parser = config[:message_parser]
133
147
  parser = @message_parsers.fetch(parser) if parser.is_a?(String)
134
148
 
135
149
  # We have the handler, action and parser.
136
150
  # Now we're going to add that route to either the background
137
151
  # or foreground route_set.
138
- background = route_config.fetch(:background) { false }
152
+ background = config.fetch(:background) { @background }
139
153
  (background ? bg_route_set : fg_route_set).add_route(
140
154
  matcher: matcher,
141
155
  handler: handler,
@@ -183,6 +197,28 @@ module Serf
183
197
 
184
198
  return app
185
199
  end
200
+
201
+ private
202
+
203
+ ##
204
+ # Extracts the handler_name and action from the 'target' using
205
+ # the shortcut convention similar to Rails routing.
206
+ #
207
+ # 'my_handler#my_method' => # my_method action.
208
+ # 'my_handler#' => # action defaults to 'call' method.
209
+ # 'my_handler' => # action defaults to 'call' method.
210
+ # '#my_method' => # some registered handler name with empty string.
211
+ #
212
+ # @param [String] target the handler and action description.
213
+ # @return the splat handler and action.
214
+ #
215
+ def handler_and_action(target)
216
+ handler, action = target.split '#', 2
217
+ handler = handler.to_s.strip
218
+ action = action.to_s.strip
219
+ action = :call if action.size == 0
220
+ return handler, action
221
+ end
186
222
  end
187
223
 
188
224
  end
data/lib/serf/version.rb CHANGED
@@ -2,7 +2,7 @@ module Serf
2
2
 
3
3
  module Version
4
4
  MAJOR = 0
5
- MINOR = 5
5
+ MINOR = 6
6
6
  PATCH = 0
7
7
  BUILD = nil
8
8
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join '.'
data/serf.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "serf"
8
- s.version = "0.5.0"
8
+ s.version = "0.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Benjamin Yu"]
12
- s.date = "2012-01-26"
12
+ s.date = "2012-02-09"
13
13
  s.description = "Event-Driven SOA with CQRS"
14
14
  s.email = "benjaminlyu@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -49,7 +49,7 @@ Gem::Specification.new do |s|
49
49
  s.homepage = "http://github.com/byu/serf"
50
50
  s.licenses = ["Apache 2.0"]
51
51
  s.require_paths = ["lib"]
52
- s.rubygems_version = "1.8.10"
52
+ s.rubygems_version = "1.8.15"
53
53
  s.summary = "Event-Driven SOA with CQRS"
54
54
 
55
55
  if s.respond_to? :specification_version then
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-26 00:00:00.000000000Z
12
+ date: 2012-02-09 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70145106081500 !ruby/object:Gem::Requirement
16
+ requirement: &70232356582940 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70145106081500
24
+ version_requirements: *70232356582940
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: i18n
27
- requirement: &70145106073960 !ruby/object:Gem::Requirement
27
+ requirement: &70232356582140 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.6.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70145106073960
35
+ version_requirements: *70232356582140
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: uuidtools
38
- requirement: &70145106072920 !ruby/object:Gem::Requirement
38
+ requirement: &70232356581500 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 2.1.2
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70145106072920
46
+ version_requirements: *70232356581500
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70145106071740 !ruby/object:Gem::Requirement
49
+ requirement: &70232356580820 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.3.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70145106071740
57
+ version_requirements: *70232356580820
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: yard
60
- requirement: &70145106070620 !ruby/object:Gem::Requirement
60
+ requirement: &70232356580140 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 0.6.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70145106070620
68
+ version_requirements: *70232356580140
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
- requirement: &70145106069640 !ruby/object:Gem::Requirement
71
+ requirement: &70232356579500 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 1.0.0
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70145106069640
79
+ version_requirements: *70232356579500
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: jeweler
82
- requirement: &70145106068740 !ruby/object:Gem::Requirement
82
+ requirement: &70232356578860 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 1.6.4
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70145106068740
90
+ version_requirements: *70232356578860
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: simplecov
93
- requirement: &70145106067300 !ruby/object:Gem::Requirement
93
+ requirement: &70232356578380 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70145106067300
101
+ version_requirements: *70232356578380
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: msgpack
104
- requirement: &70145106053600 !ruby/object:Gem::Requirement
104
+ requirement: &70232356577820 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: 0.4.6
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *70145106053600
112
+ version_requirements: *70232356577820
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: eventmachine
115
- requirement: &70145106052540 !ruby/object:Gem::Requirement
115
+ requirement: &70232356577300 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,7 +120,7 @@ dependencies:
120
120
  version: 0.12.10
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *70145106052540
123
+ version_requirements: *70232356577300
124
124
  description: Event-Driven SOA with CQRS
125
125
  email: benjaminlyu@gmail.com
126
126
  executables: []
@@ -172,7 +172,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
172
172
  version: '0'
173
173
  segments:
174
174
  - 0
175
- hash: -650473328553199241
175
+ hash: -296744974966677810
176
176
  required_rubygems_version: !ruby/object:Gem::Requirement
177
177
  none: false
178
178
  requirements:
@@ -181,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
181
  version: '0'
182
182
  requirements: []
183
183
  rubyforge_project:
184
- rubygems_version: 1.8.10
184
+ rubygems_version: 1.8.15
185
185
  signing_key:
186
186
  specification_version: 3
187
187
  summary: Event-Driven SOA with CQRS