Class: Kazoo::Router::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/kazoo/router/context.rb

Instance Method Summary collapse

Constructor Details

#initialize(router, opts = {}) ⇒ Context

Returns a new instance of Context.



5
6
7
8
# File 'lib/kazoo/router/context.rb', line 5

def initialize(router, opts = {})
  @router = router
  @opts = opts
end

Instance Method Details

#context(opts = {}) {|c| ... } ⇒ Object

Yields:

  • (c)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kazoo/router/context.rb', line 40

def context(opts = {})
  opts = opts.dup
  
  if @opts[:path_prefix] && opts[:path_prefix]
    opts[:path_prefix] = File.join(@opts[:path_prefix], opts[:path_prefix])
  end
  
  if @opts[:name_prefix] && opts[:name_prefix]
    opts[:name_prefix] = @opts[:name_prefix] + '_' + opts[:name_prefix]
  end
  
  c = self.class.new(@router, opts)
  yield c
  c
  
end

#default_app(app) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
# File 'lib/kazoo/router/context.rb', line 34

def default_app(app)
  raise ArgumentError, "default_app must be a rack application (#{app} doesn't respond to call)" unless app.respond_to?(:call)
  raise NameError, "NameError: undefined local variable or method `default_app' for #{self}" if get(:mode) == :app
  @default_app = app
end

#error_handler(app) ⇒ Object



67
68
69
# File 'lib/kazoo/router/context.rb', line 67

def error_handler(app)
  @router.error_handler(app)
end

#extract_name(opts) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/kazoo/router/context.rb', line 57

def extract_name(opts)
  if @opts[:name_prefix]  && opts[:name]
    "#{@opts[:name_prefix]}_#{opts[:name]}"
  elsif opts[:name]
    opts[:name]
  else
    nil
  end
end

#get(var) ⇒ Object



30
31
32
# File 'lib/kazoo/router/context.rb', line 30

def get(var)
  @router.get(var)
end

#match(path, opts) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/kazoo/router/context.rb', line 10

def match(path, opts)
  opts = @opts.merge(opts)
  name = extract_name(opts)
  
  if get(:mode) == :app 
    opts[:app_mode] =  true
  else
    opts[:default_app] = @default_app
  end
  
  route = Route.new(path, opts)
  @router.named_routes[name] = route if name
  @router.routes << route
  route
end

#set(var, val) ⇒ Object



26
27
28
# File 'lib/kazoo/router/context.rb', line 26

def set(var,val)
  @router.set(var,val)
end