Class: Rackr
- Inherits:
-
Object
- Object
- Rackr
- Includes:
- Action
- Defined in:
- lib/rackr.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/build_request.rb,
lib/rackr/router/errors/dev_html.rb
Defined Under Namespace
Modules: Action, Callback Classes: NotFound, Router
Constant Summary collapse
- HTTP_METHODS =
%w[GET POST DELETE PUT TRACE OPTIONS PATCH].freeze
Instance Method Summary collapse
- #call(&block) ⇒ Object
- #config ⇒ Object
- #db ⇒ Object
- #deps ⇒ Object
- #error(endpoint = -> {}, &block) ⇒ Object
-
#initialize(config = {}, before: [], after: []) ⇒ Rackr
constructor
A new instance of Rackr.
- #not_found(endpoint = -> {}, &block) ⇒ Object
- #resources(name, id: :id, before: [], after: [], &block) ⇒ Object
- #routes ⇒ Object
- #scope(name = '', before: [], after: [], &block) ⇒ Object
Methods included from Action
Constructor Details
#initialize(config = {}, before: [], after: []) ⇒ Rackr
Returns a new instance of Rackr.
15 16 17 |
# File 'lib/rackr.rb', line 15 def initialize(config = {}, before: [], after: []) @router = Router.new(config, before: before, after: after) end |
Instance Method Details
#call(&block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rackr.rb', line 19 def call(&block) instance_eval(&block) #puts "\n= Routes ==============" #routes.each_pair { |v| p v } #puts "\n= Config ==============" #puts config #puts "\n" @router end |
#config ⇒ Object
34 35 36 |
# File 'lib/rackr.rb', line 34 def config @router.config end |
#db ⇒ Object
42 43 44 |
# File 'lib/rackr.rb', line 42 def db @router.config.dig(:deps, :db) end |
#deps ⇒ Object
38 39 40 |
# File 'lib/rackr.rb', line 38 def deps @router.config[:deps] end |
#error(endpoint = -> {}, &block) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/rackr.rb', line 65 def error(endpoint = -> {}, &block) if block_given? @router.add_error(block) else @router.add_error(endpoint) end end |
#not_found(endpoint = -> {}, &block) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/rackr.rb', line 57 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, before: [], after: [], &block) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rackr.rb', line 73 def resources(name, id: :id, before: [], after: [], &block) @resource_namespace = (@resource_namespace || []).push([name.to_s.capitalize]) get_const = ->(type, action) do if Object.const_defined?("#{type}::#{@resource_namespace.join('::')}::#{action}") Object.const_get("#{type}::#{@resource_namespace.join('::')}::#{action}") end end actions = { index: { method: :get, path: nil, action: get_const.call('Actions', 'Index') }, new: { method: :get, path: 'new', action: get_const.call('Actions', 'New') }, create: { method: :post, path: nil, action: get_const.call('Actions', 'Create') }, } actions_for_id = { show: { method: :get, path: nil, action: get_const.call('Actions', 'Show') }, edit: { method: :get, path: "edit", action: get_const.call('Actions', 'Edit') }, update: { method: :put, path: nil, action: get_const.call('Actions', 'Update') }, delete: { method: :delete, path: nil, action: get_const.call('Actions', 'Delete') } } block_for_id = proc do actions_for_id.each do |_, definition| send(definition[:method], definition[:path], definition[:action]) if definition[:action] end instance_eval(&block) if block_given? end scope(name.to_s, before:, after:) do actions.each do |_, definition| send(definition[:method], definition[:path], definition[:action]) if definition[:action] end assign_callback = get_const.call('Callbacks', 'Assign') if assign_callback scope(id.to_sym, before: assign_callback, &block_for_id) else scope(id.to_sym, &block_for_id) end end @resource_namespace = @resource_namespace.first(@resource_namespace.size - 1) end |
#routes ⇒ Object
30 31 32 |
# File 'lib/rackr.rb', line 30 def routes @router.routes end |
#scope(name = '', before: [], after: [], &block) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/rackr.rb', line 46 def scope(name = '', before: [], after: [], &block) @router.append_scope( name, scope_befores: before, scope_afters: after ) instance_eval(&block) @router.clear_last_scope end |