manager.rb
1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Noosfero::Plugin::Manager
attr_reader :environment
attr_reader :context
def initialize(environment, context)
@environment = environment
@context = context
end
delegate :each, :to => :enabled_plugins
include Enumerable
# Dispatches +event+ to each enabled plugin and collect the results.
#
# Returns an Array containing the objects returned by the event method in
# each plugin. This array is compacted (i.e. nils are removed) and flattened
# (i.e. elements of arrays are added to the resulting array). For example, if
# the enabled plugins return 1, 0, nil, and [1,2,3], then this method will
# return [1,0,1,2,3]
#
def dispatch(event, *args)
dispatch_without_flatten(event, *args).flatten
end
def dispatch_plugins(event, *args)
map { |plugin| plugin.class if plugin.send(event, *args) }.compact.flatten
end
def dispatch_without_flatten(event, *args)
map { |plugin| plugin.send(event, *args) }.compact
end
alias :dispatch_scopes :dispatch_without_flatten
def first(event, *args)
result = nil
each do |plugin|
result = plugin.send(event, *args)
break if result.present?
end
result || Noosfero::Plugin.new.send(event, *args)
end
def first_plugin(event, *args)
result = nil
each do |plugin|
if plugin.send(event, *args)
result = plugin.class
break
end
end
result
end
def enabled_plugins
@enabled_plugins ||= (Noosfero::Plugin.all & environment.enabled_plugins).map do |plugin|
p = plugin.constantize.new
p.context = context
p
end
end
def [](name)
klass = Noosfero::Plugin.klass(name)
enabled_plugins.select do |plugin|
plugin.kind_of?(klass)
end.first
end
end