mediator 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/lib/mediator/parser.rb +1 -1
- data/lib/mediator/registry.rb +47 -11
- data/lib/mediator/renderer.rb +1 -1
- data/mediator.gemspec +1 -1
- data/test/mediator_parser_test.rb +1 -1
- data/test/mediator_renderer_test.rb +1 -1
- data/test/mediator_test.rb +18 -5
- metadata +2 -2
data/.gitignore
CHANGED
data/lib/mediator/parser.rb
CHANGED
data/lib/mediator/registry.rb
CHANGED
@@ -25,25 +25,53 @@ class Mediator
|
|
25
25
|
# Mediator.for B.new # => A
|
26
26
|
#
|
27
27
|
# Mediators are searched in reverse insertion order.
|
28
|
+
#
|
29
|
+
# Options:
|
30
|
+
# context - passed to new context
|
31
|
+
# registry - optional registry map to use instead of default
|
32
|
+
|
33
|
+
def for subject, opts = {}
|
34
|
+
context = opts[:context]
|
35
|
+
|
36
|
+
reg = registry(opts[:registry] || context)
|
28
37
|
|
29
|
-
|
30
|
-
|
31
|
-
return map[criteria].new subject, context if criteria === subject
|
38
|
+
reg.keys.reverse.each do |criteria|
|
39
|
+
return reg[criteria].new subject, context if criteria === subject
|
32
40
|
end
|
33
41
|
|
34
42
|
raise Error, "Can't find a Mediator for #{subject.inspect}."
|
35
43
|
end
|
36
44
|
|
37
|
-
#
|
45
|
+
# Returns a mediator registry. If no arg is passed the default registry
|
46
|
+
# is returned. Keys can be either a symbol or a Mediator instance or class
|
47
|
+
|
48
|
+
def registry key = nil
|
38
49
|
|
39
|
-
|
40
|
-
|
50
|
+
# If key exists then see if we've got an entry already and if
|
51
|
+
# not force the key to be a symbol, i.e. :default, :accounting, etc.
|
52
|
+
|
53
|
+
if key
|
54
|
+
return registries[key.class] if registries.key?(key.class)
|
55
|
+
return registries[key] if registries.key?(key)
|
56
|
+
key = :default unless key.is_a?(Symbol)
|
57
|
+
end
|
58
|
+
|
59
|
+
key ||= :default
|
60
|
+
registries[key] ||= {}
|
41
61
|
end
|
42
62
|
|
43
|
-
#
|
63
|
+
# Sugar for `for`.
|
64
|
+
|
65
|
+
def [] subject, opts = {}
|
66
|
+
self.for subject, opts
|
67
|
+
end
|
44
68
|
|
45
|
-
|
46
|
-
|
69
|
+
# Stores a map of all the different registries.
|
70
|
+
# - keys are registry symbols, i.e. :default, :account, or mediators
|
71
|
+
# - values are a simple map
|
72
|
+
|
73
|
+
def registries
|
74
|
+
@@registries ||= Hash.new
|
47
75
|
end
|
48
76
|
|
49
77
|
# Sugar for creating and registering a Mediator subclass.
|
@@ -58,17 +86,25 @@ class Mediator
|
|
58
86
|
# `subject` can take a block instead. When the mediator for a
|
59
87
|
# subject is discovered with `Mediator.for` the given block will be
|
60
88
|
# passed the subject and can return `true` or `false`.
|
89
|
+
# Can take an optional hash specifying what to register the mediator with.
|
61
90
|
|
62
91
|
def register mklass, *subjects, &block
|
92
|
+
opts = subjects.last.is_a?(Hash) ? subjects.pop : {}
|
93
|
+
reg_map = registry opts[:registry]
|
94
|
+
|
63
95
|
if block_given?
|
64
96
|
unless subjects.empty?
|
65
97
|
raise ArgumentError, "Can't provide both a subject and a block."
|
66
98
|
end
|
67
99
|
|
68
|
-
|
100
|
+
reg_map[block] = mklass
|
101
|
+
registries[mklass] = reg_map
|
69
102
|
end
|
70
103
|
|
71
|
-
subjects.each
|
104
|
+
subjects.each do |k|
|
105
|
+
reg_map[k] = mklass
|
106
|
+
registries[mklass] = reg_map
|
107
|
+
end
|
72
108
|
|
73
109
|
mklass
|
74
110
|
end
|
data/lib/mediator/renderer.rb
CHANGED
data/mediator.gemspec
CHANGED
data/test/mediator_test.rb
CHANGED
@@ -4,7 +4,7 @@ require "ostruct"
|
|
4
4
|
|
5
5
|
describe Mediator do
|
6
6
|
before do
|
7
|
-
Mediator.
|
7
|
+
Mediator.registries.clear
|
8
8
|
end
|
9
9
|
|
10
10
|
describe "initialization" do
|
@@ -266,22 +266,22 @@ describe Mediator do
|
|
266
266
|
c = Class.new Mediator
|
267
267
|
Mediator.register c, Symbol
|
268
268
|
|
269
|
-
assert_equal c, Mediator.
|
269
|
+
assert_equal c, Mediator.registry[Symbol]
|
270
270
|
end
|
271
271
|
|
272
272
|
it "can register multiple classes" do
|
273
273
|
c = Class.new Mediator
|
274
274
|
Mediator.register c, String, Symbol
|
275
275
|
|
276
|
-
assert_equal c, Mediator.
|
277
|
-
assert_equal c, Mediator.
|
276
|
+
assert_equal c, Mediator.registry[String]
|
277
|
+
assert_equal c, Mediator.registry[Symbol]
|
278
278
|
end
|
279
279
|
|
280
280
|
it "can register with a block" do
|
281
281
|
c = Class.new Mediator
|
282
282
|
Mediator.register(c) { |s| Symbol === s }
|
283
283
|
|
284
|
-
b = Mediator.
|
284
|
+
b = Mediator.registry.keys.first
|
285
285
|
refute_nil b
|
286
286
|
|
287
287
|
assert b[:foo]
|
@@ -298,9 +298,22 @@ describe Mediator do
|
|
298
298
|
assert_equal "Can't provide both a subject and a block.", ex.message
|
299
299
|
end
|
300
300
|
|
301
|
+
it "allows for alternate registry" do
|
302
|
+
c = Class.new Mediator
|
303
|
+
assert_equal c, Mediator.register(c, String, registry: :monkeys )
|
304
|
+
|
305
|
+
r = c.registries[:monkeys]
|
306
|
+
refute_nil r
|
307
|
+
|
308
|
+
assert_equal c, r[String]
|
309
|
+
assert_equal r, c.registry(c)
|
310
|
+
assert_equal r, c.registries[c]
|
311
|
+
end
|
312
|
+
|
301
313
|
it "returns the registered thing" do
|
302
314
|
c = Class.new Mediator
|
303
315
|
assert_equal c, Mediator.register(c, String)
|
304
316
|
end
|
317
|
+
|
305
318
|
end
|
306
319
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mediator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-05 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: A go-between for models.
|
15
15
|
email:
|