(experimental) added framework for filtering all sorts of internals in discourse and consuming by plugins

This commit is contained in:
Sam
2013-10-10 18:45:40 +11:00
parent 8afff108bf
commit b0465c517e
4 changed files with 78 additions and 1 deletions

18
lib/plugin/filter.rb Normal file
View 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

View 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