Class: Eldr::App
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
-
.after_filters ⇒ Object
Returns the value of attribute after_filters.
-
.before_filters ⇒ Object
Returns the value of attribute before_filters.
-
.builder ⇒ Object
Returns the value of attribute builder.
-
.configuration ⇒ Object
(also: config)
Returns the value of attribute configuration.
-
.recognizer ⇒ Object
Returns the value of attribute recognizer.
-
.routes ⇒ Object
Returns the value of attribute routes.
Instance Attribute Summary collapse
-
#configuration ⇒ Object
rubocop:disable Metrics/ClassLength.
-
#env ⇒ Object
rubocop:disable Metrics/ClassLength.
Class Method Summary collapse
- ._new ⇒ Object
- .add(verb: :get, path: '/', options: {}, handler: nil) ⇒ Object (also: <<)
- .after(*keys, &block) ⇒ Object
- .before(*keys, &block) ⇒ Object
- .call(env) ⇒ Object
- .inherited(base) ⇒ Object
- .new(*args, &block) ⇒ Object
- .set(key, value) ⇒ Object
Instance Method Summary collapse
- #call(env) ⇒ Object
- #call!(env) ⇒ Object
-
#initialize(configuration = nil) ⇒ App
constructor
A new instance of App.
- #params ⇒ Object
- #recognize(env) ⇒ Object
- #request ⇒ Object
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_filters ⇒ Object
Returns the value of attribute after_filters.
22 23 24 |
# File 'lib/eldr/app.rb', line 22 def after_filters @after_filters end |
.before_filters ⇒ Object
Returns the value of attribute before_filters.
22 23 24 |
# File 'lib/eldr/app.rb', line 22 def before_filters @before_filters end |
.builder ⇒ Object
Returns the value of attribute builder.
22 23 24 |
# File 'lib/eldr/app.rb', line 22 def builder @builder end |
.configuration ⇒ Object Also known as: config
Returns the value of attribute configuration.
22 23 24 |
# File 'lib/eldr/app.rb', line 22 def configuration @configuration end |
.recognizer ⇒ Object
Returns the value of attribute recognizer.
22 23 24 |
# File 'lib/eldr/app.rb', line 22 def recognizer @recognizer end |
.routes ⇒ Object
Returns the value of attribute routes.
22 23 24 |
# File 'lib/eldr/app.rb', line 22 def routes @routes end |
Instance Attribute Details
#configuration ⇒ Object
rubocop:disable Metrics/ClassLength
18 19 20 |
# File 'lib/eldr/app.rb', line 18 def configuration @configuration end |
#env ⇒ Object
rubocop:disable Metrics/ClassLength
18 19 20 |
# File 'lib/eldr/app.rb', line 18 def env @env end |
Class Method Details
._new ⇒ Object
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: , 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 |
#params ⇒ Object
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 |
#request ⇒ Object
134 135 136 |
# File 'lib/eldr/app.rb', line 134 def request env['eldr.request'] ||= Rack::Request.new(env) end |