Class: Meta::RouteDSL::ChainBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/meta/route_dsl/chain_builder.rb

Instance Method Summary collapse

Constructor Details

#initializeChainBuilder

Returns a new instance of ChainBuilder.



6
7
8
# File 'lib/meta/route_dsl/chain_builder.rb', line 6

def initialize
  @blocks = []
end

Instance Method Details

#authorize(&block) ⇒ Object



34
35
36
37
38
39
# File 'lib/meta/route_dsl/chain_builder.rb', line 34

def authorize(&block)
  do_any {
    permitted = instance_eval(&block)
    raise Errors::NotAuthorized unless permitted
  }
end

#buildObject



10
11
12
13
14
15
# File 'lib/meta/route_dsl/chain_builder.rb', line 10

def build
  blocks = @blocks
  proc do
    blocks.each { |b| instance_exec &b }
  end
end

#do_any(&block) ⇒ Object



17
18
19
20
21
# File 'lib/meta/route_dsl/chain_builder.rb', line 17

def do_any(&block)
  @blocks << block

  self
end

#resource(&block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/meta/route_dsl/chain_builder.rb', line 23

def resource(&block)
  do_any {
    resource = instance_exec(&block)

    raise Errors::ResourceNotFound if resource.nil?

    # 为 execution 添加一个 resource 方法
    define_singleton_method(:resource) { resource }
  }
end

#set_status(&block) ⇒ Object



41
42
43
44
45
# File 'lib/meta/route_dsl/chain_builder.rb', line 41

def set_status(&block)
  do_any {
    response.status = instance_exec(&block)
  }
end