mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
(experimental) added framework for filtering all sorts of internals in discourse and consuming by plugins
This commit is contained in:
18
lib/plugin/filter.rb
Normal file
18
lib/plugin/filter.rb
Normal file
@ -0,0 +1,18 @@
|
||||
require_dependency 'plugin/filter_manager'
|
||||
# this concept is borrowed straight out of wordpress
|
||||
module Plugin
|
||||
class Filter
|
||||
def self.manager
|
||||
@manager ||= FilterManager.new
|
||||
end
|
||||
|
||||
def self.register(name, &blk)
|
||||
manager.register(name, &blk)
|
||||
end
|
||||
|
||||
def self.apply(name, context, result)
|
||||
manager.apply(name, context, result)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
23
lib/plugin/filter_manager.rb
Normal file
23
lib/plugin/filter_manager.rb
Normal file
@ -0,0 +1,23 @@
|
||||
module Plugin
|
||||
class FilterManager
|
||||
|
||||
def initialize
|
||||
@map = {}
|
||||
end
|
||||
|
||||
def register(name, &blk)
|
||||
raise ArgumentException unless blk && blk.arity == 2
|
||||
filters = @map[name] ||= []
|
||||
filters << blk
|
||||
end
|
||||
|
||||
def apply(name, context, result)
|
||||
if filters = @map[name]
|
||||
filters.each do |blk|
|
||||
result = blk.call(context, result)
|
||||
end
|
||||
end
|
||||
result
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user