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 CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .idea
@@ -76,7 +76,7 @@ class Mediator
76
76
  def sub subj, data, options, &block
77
77
  return if empty? data, options or subj.nil?
78
78
 
79
- Mediator[subj, mediator].parse data
79
+ Mediator[subj, context: mediator].parse data
80
80
  block[subj] if block
81
81
 
82
82
  subj
@@ -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
- def for subject, context = nil
30
- map.keys.reverse.each do |criteria|
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
- # Sugar for `for`.
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
- def [] subject, context = nil
40
- self.for subject, context
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
- # A map from subject class or block to Mediator subclass.
63
+ # Sugar for `for`.
64
+
65
+ def [] subject, opts = {}
66
+ self.for subject, opts
67
+ end
44
68
 
45
- def map
46
- @@map ||= {}
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
- map[block] = mklass
100
+ reg_map[block] = mklass
101
+ registries[mklass] = reg_map
69
102
  end
70
103
 
71
- subjects.each { |k| map[k] = mklass }
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
@@ -74,7 +74,7 @@ class Mediator
74
74
  private
75
75
 
76
76
  def sub value, options, &block
77
- rendered = Mediator[value, mediator].render
77
+ rendered = Mediator[value, context: mediator].render
78
78
  block ? block[rendered] : rendered
79
79
  end
80
80
 
data/mediator.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |gem|
9
9
  gem.test_files = `git ls-files -- test/*`.split "\n"
10
10
  gem.name = "mediator"
11
11
  gem.require_paths = ["lib"]
12
- gem.version = "0.2.0"
12
+ gem.version = "0.3.0"
13
13
 
14
14
  gem.required_ruby_version = ">= 1.9.2"
15
15
  end
@@ -5,7 +5,7 @@ require "ostruct"
5
5
 
6
6
  describe Mediator::Parser do
7
7
  before do
8
- Mediator.map.clear
8
+ Mediator.registries.clear
9
9
 
10
10
  @subject = OpenStruct.new
11
11
  @mediator = Mediator.new @subject
@@ -5,7 +5,7 @@ require "ostruct"
5
5
 
6
6
  describe Mediator::Renderer do
7
7
  before do
8
- Mediator.map.clear
8
+ Mediator.registries.clear
9
9
  end
10
10
 
11
11
  it "has data" do
@@ -4,7 +4,7 @@ require "ostruct"
4
4
 
5
5
  describe Mediator do
6
6
  before do
7
- Mediator.map.clear
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.map[Symbol]
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.map[String]
277
- assert_equal c, Mediator.map[Symbol]
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.map.keys.first
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.2.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-06-12 00:00:00.000000000Z
12
+ date: 2012-07-05 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: A go-between for models.
15
15
  email: