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, options: {}, handler: nil, app: nil) ⇒ Route

Returns a new instance of Route.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/eldr/route.rb', line 6

def initialize(verb: :get, path: '/', name: nil, options: {}, handler: nil, app: nil)
  @path, @verb = path, verb.to_s.upcase

  @app            = app
  @capture        = {}
  @before_filters = []
  @after_filters  = []
  @name           = name

  merge_with_options!(options)

  @handler   = handler
  @handler ||= @to
end

Instance Attribute Details

#after_filtersObject

Returns the value of attribute after_filters.



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

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.



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

def before_filters
  @before_filters
end

#captureObject

Returns the value of attribute capture.



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

def capture
  @capture
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

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#orderObject

Returns the value of attribute order.



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

def order
  @order
end

#toObject

Returns the value of attribute to.



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

def to
  @to
end

Instance Method Details

#accessor?(key) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/eldr/route.rb', line 67

def accessor?(key)
  respond_to?("#{key}=") && respond_to?(key)
end

#call(env, app: nil) ⇒ Object



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

def call(env, app: nil)
  # TODO: Investigate
  # maybe if we passed this around between methods it would be more perfomant
  # than setting the accessor?
  @app = app

  app.class.before_filters[:all].each { |filter| app.instance_exec(env, &filter) } if app
  @before_filters.each                { |filter| app.instance_exec(env, &filter) }

  resp = response(env)

  app.class.after_filters[:all].each { |filter| app.instance_exec(env, &filter) } if app
  @after_filters.each                { |filter| app.instance_exec(env, &filter) }

  resp
end

#match(pattern) ⇒ Object



75
76
77
# File 'lib/eldr/route.rb', line 75

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

#matcherObject



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

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

#merge_with_options!(options) ⇒ Object



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

def merge_with_options!(options)
  @options = {} unless @options
  options.each_pair do |key, value|
    accessor?(key) ? __send__("#{key}=", value) : (@options[key] = value)
  end
end

#params(pattern) ⇒ Object



86
87
88
89
90
# File 'lib/eldr/route.rb', line 86

def params(pattern)
  params   = matcher.handler.params(pattern)
  params ||= {}
  params
end

#path(*args) ⇒ Object



79
80
81
82
83
84
# File 'lib/eldr/route.rb', line 79

def path(*args)
  return @path if args.empty?
  params = args[0]
  params.delete(:captures)
  matcher.expand(params)
end

#rails_style_response(env) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/eldr/route.rb', line 48

def rails_style_response(env)
  controller, method = @handler.split('#')

  obj = Object.const_get(controller).new

  if method
    obj.send(method.to_sym, env)
  else
    obj.call(env)
  end
end

#response(env) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/eldr/route.rb', line 38

def response(env)
  if @handler.is_a? Proc
    app.instance_exec(env, &@handler)
  elsif @handler.is_a? String
    rails_style_response(env)
  else
    @handler.call(env)
  end
end