(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

View File

@ -6,6 +6,7 @@ require_dependency 'enum'
require_dependency 'trashable'
require_dependency 'post_analyzer'
require_dependency 'validators/post_validator'
require_dependency 'plugin/filter'
require 'archetype'
require 'digest/sha1'
@ -108,7 +109,7 @@ class Post < ActiveRecord::Base
end
def cook(*args)
post_analyzer.cook(*args)
Plugin::Filter.apply(:after_post_cook, self, post_analyzer.cook(*args))
end

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

View File

@ -0,0 +1,35 @@
require 'spec_helper'
require_dependency 'plugin/filter_manager'
describe Plugin::FilterManager do
let(:instance){ Plugin::FilterManager.new }
it "calls registered filters correctly" do
instance.register(:added_numbers) do |context,result|
context + result + 1
end
instance.register(:added_numbers) do |context,result|
context + result + 2
end
instance.apply(:added_numbers, 1, 0).should == 5
end
it "should raise an exception if wrong arity is passed in" do
lambda do
instance.register(:test) do
end
end.should raise_exception
end
it "should return the original if no filters exist" do
instance.apply(:foo, nil, 42).should == 42
end
it "should raise an exception if no block is passed in" do
lambda do
instance.register(:test)
end.should raise_exception
end
end