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`.



71
72
73
# File 'lib/mediator/registry.rb', line 71

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


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

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:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mediator/registry.rb', line 34

def for subject, opts = {}
  # Return a dummy mediator is subject is nil
  # (can happen with r.one :foo, empty: true)

  return OpenStruct.new render: nil if subject == nil

  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.



85
86
87
88
# File 'lib/mediator/registry.rb', line 85

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.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mediator/registry.rb', line 97

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


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

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mediator/registry.rb', line 54

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