Class: Meta::RouteDSL::AroundActionBuilder
- Inherits:
-
Object
- Object
- Meta::RouteDSL::AroundActionBuilder
- Defined in:
- lib/meta/route_dsl/around_action_builder.rb
Class Method Summary collapse
-
.build(before: [], after: [], around: [], action: nil) ⇒ Object
使用 before、after、around 系列和当前 action 共同构建洋葱圈模型。 Note: 该方法已被废弃!.
- .build_from_callbacks(callbacks: []) ⇒ Object
Instance Method Summary collapse
- #after(&block) ⇒ Object
- #around(&block) ⇒ Object
- #before(&block) ⇒ Object
- #build ⇒ Object
-
#initialize ⇒ AroundActionBuilder
constructor
A new instance of AroundActionBuilder.
Constructor Details
#initialize ⇒ AroundActionBuilder
Returns a new instance of AroundActionBuilder.
17 18 19 20 |
# File 'lib/meta/route_dsl/around_action_builder.rb', line 17 def initialize @before = [] @after = [] end |
Class Method Details
.build(before: [], after: [], around: [], action: nil) ⇒ Object
使用 before、after、around 系列和当前 action 共同构建洋葱圈模型。 Note: 该方法已被废弃!
构建成功后,执行顺序是:
-
before 序列
-
around 序列的前半部分
-
action
-
around 序列的后半部分
-
after 序列
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/meta/route_dsl/around_action_builder.rb', line 60 def build(before: [], after: [], around: [], action: nil) builder = AroundActionBuilder.new # 首先构建 before 序列,保证它最先执行 builder.around do |next_action| before.each { |p| self.instance_exec(&p) } next_action.execute(self) end unless before.empty? # 然后构建 after 序列,保证它最后执行 builder.around do |next_action| next_action.execute(self) after.each { |p| self.instance_exec(&p) } end unless after.empty? # 接着应用洋葱圈模型,依次构建 around 序列、action around.each { |p| builder.around(&p) } builder.around { self.instance_exec(&action) } unless action.nil? builder.build end |
.build_from_callbacks(callbacks: []) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/meta/route_dsl/around_action_builder.rb', line 80 def build_from_callbacks(callbacks: []) around_action_builder = AroundActionBuilder.new callbacks.each do |cb| around_action_builder.send(cb[:lifecycle], &cb[:proc]) if cb[:proc] end around_action_builder.build end |
Instance Method Details
#after(&block) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/meta/route_dsl/around_action_builder.rb', line 33 def after(&block) # 在洋葱圈模型中,先声明的 after 逻辑会在最后执行,因此为了保证 after 逻辑的执行顺序 @after.unshift(Proc.new do |next_action| next_action.execute(self) if next_action self.instance_exec(&block) end) end |
#around(&block) ⇒ Object
22 23 24 |
# File 'lib/meta/route_dsl/around_action_builder.rb', line 22 def around(&block) @before << block end |
#before(&block) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/meta/route_dsl/around_action_builder.rb', line 26 def before(&block) @before << Proc.new do |next_action| self.instance_exec(&block) next_action.execute(self) if next_action end end |
#build ⇒ Object
41 42 43 44 45 46 |
# File 'lib/meta/route_dsl/around_action_builder.rb', line 41 def build # 从后向前构建 (@before + @after).reverse.reduce(nil) do |following, p| LinkedAction.new(p, following) end end |