Class: Rackr
- Inherits:
-
Object
- Object
- Rackr
- Defined in:
- lib/rackr.rb,
lib/rackr/utils.rb,
lib/rackr/action.rb,
lib/rackr/router.rb,
lib/rackr/callback.rb,
lib/rackr/router/route.rb,
lib/rackr/router/errors.rb,
lib/rackr/router/endpoint.rb,
lib/rackr/action/callbacks.rb,
lib/rackr/router/path_route.rb,
lib/rackr/router/build_request.rb,
lib/rackr/router/dev_html/dump.rb,
lib/rackr/router/dev_html/errors.rb
Overview
Rackr is a simple router for Rack.
Defined Under Namespace
Modules: Action, Callback, Utils Classes: Dump, NotFound, Router
Constant Summary collapse
- VERSION =
'0.0.72'- HTTP_METHODS =
%w[GET POST DELETE PUT TRACE OPTIONS PATCH].freeze
Instance Method Summary collapse
- #app ⇒ Object
- #cache ⇒ Object
- #config ⇒ Object
- #db ⇒ Object
- #deps ⇒ Object
- #error(error_class_or_endpoint = nil, endpoint = nil, &block) ⇒ Object
- #extend(mod) ⇒ Object
- #include(mod) ⇒ Object
-
#initialize(config = {}, before: [], after: []) ⇒ Rackr
constructor
A new instance of Rackr.
- #log ⇒ Object
- #not_found(endpoint = -> {}, &block) ⇒ Object
- #resources(name, id: :id, path: nil, paths: {}, callbacks: [], before: [], after: [], &block) ⇒ Object
- #routes ⇒ Object
- #scope(name = '', before: [], after: [], &block) ⇒ Object
Methods included from Utils
#deep_hash_push, #deep_hash_set, #ensure_array
Methods included from Callback
Constructor Details
Instance Method Details
#app ⇒ Object
42 43 44 45 46 |
# File 'lib/rackr.rb', line 42 def app(&) instance_eval(&) @router end |
#cache ⇒ Object
68 69 70 |
# File 'lib/rackr.rb', line 68 def cache @router.config&.dig(:deps, :cache) end |
#config ⇒ Object
52 53 54 |
# File 'lib/rackr.rb', line 52 def config @router.config end |
#db ⇒ Object
60 61 62 |
# File 'lib/rackr.rb', line 60 def db @router.config&.dig(:deps, :db) end |
#deps ⇒ Object
56 57 58 |
# File 'lib/rackr.rb', line 56 def deps @router.config[:deps] end |
#error(error_class_or_endpoint = nil, endpoint = nil, &block) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/rackr.rb', line 91 def error(error_class_or_endpoint = nil, endpoint = nil, &block) if block_given? if error_class_or_endpoint @router.add_error(block, error_class_or_endpoint) else @router.add_error(block) end elsif endpoint @router.add_error(endpoint, error_class_or_endpoint) else @router.add_error(error_class_or_endpoint) end end |
#extend(mod) ⇒ Object
38 39 40 |
# File 'lib/rackr.rb', line 38 def extend(mod) self.class.extend(mod) end |
#include(mod) ⇒ Object
34 35 36 |
# File 'lib/rackr.rb', line 34 def include(mod) self.class.include(mod) end |
#log ⇒ Object
64 65 66 |
# File 'lib/rackr.rb', line 64 def log @router.config&.dig(:deps, :log) end |
#not_found(endpoint = -> {}, &block) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/rackr.rb', line 83 def not_found(endpoint = -> {}, &block) if block_given? @router.add_not_found(block) else @router.add_not_found(endpoint) end end |
#resources(name, id: :id, path: nil, paths: {}, callbacks: [], before: [], after: [], &block) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/rackr.rb', line 105 def resources(name, id: :id, path: nil, paths: {}, callbacks: [], before: [], after: [], &block) @nested_resources ||= [] @nested_resources.push(name) infer_action_const = lambda do |action| scope_parts = @router.not_empty_scopes .map(&:to_s) .reject { |s| s.start_with?(':') } .map(&:capitalize) parts = ['Actions'] + scope_parts + [name.to_s.capitalize, action] const_path = parts.join('::') Object.const_get(const_path) if Object.const_defined?(const_path) end infer_assign_const = lambda do parts = @nested_resources.map { |s| s.to_s.capitalize } const_path = "Callbacks::#{parts.join('::')}::Assign" Object.const_get(const_path) if Object.const_defined?(const_path) end actions = { index: { method: :get, path: nil, action: infer_action_const.call('Index') }, new: { method: :get, path: 'new', action: infer_action_const.call('New') }, create: { method: :post, path: nil, action: infer_action_const.call('Create') } } actions_for_id = { show: { method: :get, path: nil, action: infer_action_const.call('Show') }, edit: { method: :get, path: 'edit', action: infer_action_const.call('Edit') }, update: { method: :put, path: nil, action: infer_action_const.call('Update') }, delete: { method: :delete, path: nil, action: infer_action_const.call('Delete') } } received_callbacks = Hash.new { |h, k| h[k] = { before: [], after: [] } } (callbacks || []).each do |callback_config| ensure_array(callback_config[:actions]).each do |action| received_callbacks[action][:before].concat(ensure_array(callback_config[:before])) received_callbacks[action][:after].concat(ensure_array(callback_config[:after])) end end (paths || {}).each do |action, new_path| if actions[action] actions[action][:path] = new_path elsif actions_for_id[action] actions_for_id[action][:path] = new_path end end block_for_id = proc do actions_for_id.each do |action_name, definition| next unless definition[:action] action_callbacks = received_callbacks[action_name] send( definition[:method], definition[:path], definition[:action], before: action_callbacks[:before], after: action_callbacks[:after] ) end instance_eval(&block) if block_given? end scope_name = path || name.to_s assign_callback = infer_assign_const.call scope(scope_name, before:, after:) do actions.each do |action_name, definition| next unless definition[:action] action_callbacks = received_callbacks[action_name] send( definition[:method], definition[:path], definition[:action], before: action_callbacks[:before], after: action_callbacks[:after] ) end if assign_callback scope(id.to_sym, before: assign_callback, &block_for_id) else scope(id.to_sym, &block_for_id) end end @nested_resources.pop end |
#routes ⇒ Object
48 49 50 |
# File 'lib/rackr.rb', line 48 def routes @router.routes end |
#scope(name = '', before: [], after: [], &block) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rackr.rb', line 72 def scope(name = '', before: [], after: [], &block) @router.append_scope( name, scope_befores: before, scope_afters: after ) instance_eval(&block) @router.clear_last_scope end |