Class: Eldr::App

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/eldr/app.rb

Overview

The main class all Eldr apps extend

Constant Summary collapse

HTTP_VERBS =
%w(DELETE GET HEAD OPTIONS PATCH POST PUT)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = nil) ⇒ App

Returns a new instance of App.



109
110
111
112
# File 'lib/eldr/app.rb', line 109

def initialize(configuration = nil)
  configuration ||= self.class.configuration
  @configuration = configuration
end

Class Attribute Details

.after_filtersObject

Returns the value of attribute after_filters.



22
23
24
# File 'lib/eldr/app.rb', line 22

def after_filters
  @after_filters
end

.before_filtersObject

Returns the value of attribute before_filters.



22
23
24
# File 'lib/eldr/app.rb', line 22

def before_filters
  @before_filters
end

.builderObject

Returns the value of attribute builder.



22
23
24
# File 'lib/eldr/app.rb', line 22

def builder
  @builder
end

.configurationObject Also known as: config

Returns the value of attribute configuration.



22
23
24
# File 'lib/eldr/app.rb', line 22

def configuration
  @configuration
end

.recognizerObject

Returns the value of attribute recognizer.



22
23
24
# File 'lib/eldr/app.rb', line 22

def recognizer
  @recognizer
end

.routesObject

Returns the value of attribute routes.



22
23
24
# File 'lib/eldr/app.rb', line 22

def routes
  @routes
end

Instance Attribute Details

#configurationObject

rubocop:disable Metrics/ClassLength



18
19
20
# File 'lib/eldr/app.rb', line 18

def configuration
  @configuration
end

#envObject

rubocop:disable Metrics/ClassLength



18
19
20
# File 'lib/eldr/app.rb', line 18

def env
  @env
end

Class Method Details

._newObject



26
# File 'lib/eldr/app.rb', line 26

alias_method :_new, :new

.add(verb: :get, path: '/', options: {}, handler: nil) ⇒ Object Also known as: <<



96
97
98
99
100
101
102
103
104
105
# File 'lib/eldr/app.rb', line 96

def add(verb: :get, path: '/', options: {}, handler: nil)
  handler = Proc.new if block_given?
  route = Route.new(verb: verb, path: path, options: options, handler: handler)

  route.before_filters.push(*before_filters[route.name]) if before_filters.include? route.name
  route.after_filters.push(*after_filters[route.name]) if after_filters.include? route.name

  routes[verb] << route
  route
end

.after(*keys, &block) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/eldr/app.rb', line 70

def after(*keys, &block)
  *keys = [:all] if keys.empty?
  keys.each do |key|
    after_filters[key] ||= []
    after_filters[key] << block
  end
end

.before(*keys, &block) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/eldr/app.rb', line 62

def before(*keys, &block)
  *keys = [:all] if keys.empty?
  keys.each do |key|
    before_filters[key] ||= []
    before_filters[key] << block
  end
end

.call(env) ⇒ Object



32
33
34
# File 'lib/eldr/app.rb', line 32

def call(env)
  new.call env
end

.inherited(base) ⇒ Object



36
37
38
39
# File 'lib/eldr/app.rb', line 36

def inherited(base)
  base.builder.use builder.middleware
  base.configuration.merge!(configuration)
end

.new(*args, &block) ⇒ Object



27
28
29
30
# File 'lib/eldr/app.rb', line 27

def new(*args, &block)
  builder.run _new(*args, &block)
  builder
end

.set(key, value) ⇒ Object



50
51
52
# File 'lib/eldr/app.rb', line 50

def set(key, value)
  configuration.set(key, value)
end

Instance Method Details

#call(env) ⇒ Object



114
115
116
# File 'lib/eldr/app.rb', line 114

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/eldr/app.rb', line 118

def call!(env)
  @env = env

  recognize(env).each do |route|
    env['eldr.route'] = route
    params.merge!(route.params(env['PATH_INFO']))
    catch(:pass) { return route.call(env, app: self) }
  end
  rescue => error
    if error.respond_to? :call
      error.call(env)
    else
      raise error
    end
end

#paramsObject



138
139
140
# File 'lib/eldr/app.rb', line 138

def params
  env['eldr.params'] ||= request.params
end

#recognize(env) ⇒ Object



142
143
144
# File 'lib/eldr/app.rb', line 142

def recognize(env)
  self.class.recognizer.call(env)
end

#requestObject



134
135
136
# File 'lib/eldr/app.rb', line 134

def request
  env['eldr.request'] ||= Rack::Request.new(env)
end