Module: Mediator::Registry

Included in:
Mediator
Defined in:
lib/mediator/registry.rb

Instance Method Summary collapse

Instance Method Details

#[](subject, opts = {}) ⇒ Object

Sugar for ‘for`.



65
66
67
# File 'lib/mediator/registry.rb', line 65

def [] subject, opts = {}
  self.for subject, opts
end

#accept(*subjects, &block) ⇒ Object

Sugar for ‘register`.

class FooMediator < Mediator
  accept Foo  # same as Mediator.register FooMediator, Foo
end


12
13
14
# File 'lib/mediator/registry.rb', line 12

def accept *subjects, &block
  register self, *subjects, &block
end

#for(subject, opts = {}) ⇒ Object

Find and instantiate a mediator for ‘subject` by consulting `map`. Returns `nil` if no mediator can be found. Inheritance is respected for mediators registered by class, so:

A = Class.new
B = Class.new A
M = Class.new Mediator

M.subject A
Mediator.for B.new  #  => A

Mediators are searched in reverse insertion order.

Options:

context  - passed to new context
registry - optional registry map to use instead of default

Raises:



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mediator/registry.rb', line 33

def for subject, opts = {}
  context = opts[:context]

  reg = registry(opts[:registry] || context)

  reg.keys.reverse.each do |criteria|
    return reg[criteria].new subject, context if criteria === subject
  end

  raise Error, "Can't find a Mediator for #{subject.inspect}."
end

#mediate(*subjects, &block) ⇒ Object

Sugar for creating and registering a Mediator subclass.



79
80
81
82
# File 'lib/mediator/registry.rb', line 79

def mediate *subjects, &block
  mediator = Class.new self, &block
  register mediator, *subjects
end

#register(mklass, *subjects, &block) ⇒ Object

Register a Mediator subclass’s interest in one or more subject classes. If more detailed selection behavior is necessary, ‘subject` can take a block instead. When the mediator for a subject is discovered with `Mediator.for` the given block will be passed the subject and can return `true` or `false`. Can take an optional hash specifying what to register the mediator with.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/mediator/registry.rb', line 91

def register mklass, *subjects, &block
  opts = subjects.last.is_a?(Hash) ? subjects.pop : {}
  reg_map = registry opts[:registry]

  if block_given?
    unless subjects.empty?
      raise ArgumentError, "Can't provide both a subject and a block."
    end

    reg_map[block] = mklass
    registries[mklass] = reg_map
  end

  subjects.each do |k|
    reg_map[k] = mklass
    registries[mklass] = reg_map
  end

  mklass
end

#registriesObject

Stores a map of all the different registries.

- keys are registry symbols, i.e. :default, :account, or mediators
- values are a simple map


73
74
75
# File 'lib/mediator/registry.rb', line 73

def registries
  @@registries ||= Hash.new
end

#registry(key = nil) ⇒ Object

Returns a mediator registry. If no arg is passed the default registry is returned. Keys can be either a symbol or a Mediator instance or class



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mediator/registry.rb', line 48

def registry key = nil

  # If key exists then see if we've got an entry already and if
  # not force the key to be a symbol, i.e. :default, :accounting, etc.
  
  if key
    return registries[key.class] if registries.key?(key.class)
    return registries[key] if registries.key?(key)
    key = :default unless key.is_a?(Symbol)
  end
  
  key ||= :default
  registries[key] ||= {}
end