Class: Eldr::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/eldr/route.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb: :get, path: '/', name: nil, order: 0, handler: nil, to: nil) ⇒ Route

Returns a new instance of Route.



5
6
7
8
9
10
# File 'lib/eldr/route.rb', line 5

def initialize(verb: :get, path: '/', name: nil, order: 0, handler: nil, to: nil)
  @verb, @path, @name, @order = verb.to_s.upcase, path, name, order
  @before_filters, @after_filters = [], []
  handler  ||= to
  @handler = create_handler(handler)
end

Instance Attribute Details

#after_filtersObject

Returns the value of attribute after_filters.



3
4
5
# File 'lib/eldr/route.rb', line 3

def after_filters
  @after_filters
end

#appObject

Returns the value of attribute app.



3
4
5
# File 'lib/eldr/route.rb', line 3

def app
  @app
end

#before_filtersObject

Returns the value of attribute before_filters.



3
4
5
# File 'lib/eldr/route.rb', line 3

def before_filters
  @before_filters
end

#handlerObject

Returns the value of attribute handler.



3
4
5
# File 'lib/eldr/route.rb', line 3

def handler
  @handler
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/eldr/route.rb', line 3

def name
  @name
end

#orderObject

Returns the value of attribute order.



3
4
5
# File 'lib/eldr/route.rb', line 3

def order
  @order
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/eldr/route.rb', line 3

def path
  @path
end

#verbObject

Returns the value of attribute verb.



3
4
5
# File 'lib/eldr/route.rb', line 3

def verb
  @verb
end

Instance Method Details

#call(env, app: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/eldr/route.rb', line 23

def call(env, app: nil)
  @app = app

  call_before_filters(env)

  resp = call_handler(env)

  call_after_filters(env)

  resp
end

#call_after_filters(env) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/eldr/route.rb', line 44

def call_after_filters(env)
  if app
    app.class.after_filters[:all].each { |filter| app.instance_exec(env, &filter) }
    after_filters.each                 { |filter| app.instance_exec(env, &filter) }
  else
    after_filters.each { |filter| filter.call(env) }
  end
end

#call_before_filters(env) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/eldr/route.rb', line 35

def call_before_filters(env)
  if app
    app.class.before_filters[:all].each { |filter| app.instance_exec(env, &filter) }
    before_filters.each                 { |filter| app.instance_exec(env, &filter) }
  else
    before_filters.each { |filter| filter.call(env) }
  end
end

#call_handler(env) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/eldr/route.rb', line 53

def call_handler(env)
  if app and handler.is_a? Proc
    app.instance_exec(env, &handler)
  else
    handler.call(env)
  end
end

#create_handler(handler) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/eldr/route.rb', line 12

def create_handler(handler)
  return handler unless handler.is_a? String

  controller, method = handler.split('#')

  proc do |env|
    obj = Object.const_get(controller).new
    obj.send(method.to_sym, env)
  end
end

#match(pattern) ⇒ Object



65
66
67
# File 'lib/eldr/route.rb', line 65

def match(pattern)
  matcher.match(pattern)
end

#matcherObject



61
62
63
# File 'lib/eldr/route.rb', line 61

def matcher
  @matcher ||= Matcher.new(path, capture: @capture)
end

#params(pattern) ⇒ Object



69
70
71
72
# File 'lib/eldr/route.rb', line 69

def params(pattern)
  params   = matcher.handler.params(pattern)
  params ||= {} # rubocop:disable Lint/UselessAssignment
end