提交 87378b0b 编写于 作者: A Aaron Patterson

polymorphic around callbacks

上级 e8ddd4f9
......@@ -245,6 +245,84 @@ def self.simple(next_callback, user_callback)
}
end
end
class Around
def self.build(next_callback, user_callback, user_conditions, chain_config)
if chain_config.key?(:terminator) && user_conditions.any?
halting_and_conditional(next_callback, user_callback, user_conditions)
elsif chain_config.key? :terminator
halting(next_callback, user_callback)
elsif user_conditions.any?
conditional(next_callback, user_callback, user_conditions)
else
simple(next_callback, user_callback)
end
end
private
def self.halting_and_conditional(next_callback, user_callback, user_conditions)
lambda { |env|
target = env.target
value = env.value
halted = env.halted
if !halted && user_conditions.all? { |c| c.call(target, value) }
user_callback.call(target, value) {
env = next_callback.call env
env.value
}
env
else
next_callback.call env
end
}
end
def self.halting(next_callback, user_callback)
lambda { |env|
target = env.target
value = env.value
if !env.halted
user_callback.call(target, value) {
env = next_callback.call env
env.value
}
env
else
next_callback.call env
end
}
end
def self.conditional(next_callback, user_callback, user_conditions)
lambda { |env|
target = env.target
value = env.value
if user_conditions.all? { |c| c.call(target, value) }
user_callback.call(target, value) {
env = next_callback.call env
env.value
}
env
else
next_callback.call env
end
}
end
def self.simple(next_callback, user_callback)
lambda { |env|
user_callback.call(env.target, env.value) {
env = next_callback.call env
env.value
}
env
}
end
end
end
class Callback #:nodoc:#
......@@ -319,21 +397,7 @@ def apply(next_callback)
when :after
Filters::After.build(next_callback, user_callback, user_conditions, chain_config)
when :around
lambda { |env|
target = env.target
value = env.value
halted = env.halted
if !halted && user_conditions.all? { |c| c.call(target, value) }
user_callback.call(target, value) {
env = next_callback.call env
env.value
}
env
else
next_callback.call env
end
}
Filters::Around.build(next_callback, user_callback, user_conditions, chain_config)
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册