Class: Dryer::Routes::Registry
- Inherits:
-
Object
- Object
- Dryer::Routes::Registry
- Defined in:
- lib/dryer/routes/registry.rb
Instance Attribute Summary collapse
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
- #get_validated_values(request) ⇒ Object
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #register(*resources) ⇒ Object
- #route_for(controller:, method:) ⇒ Object
- #to_rails_routes(router) ⇒ Object
- #validate_request(request) ⇒ Object
- #validate_response(controller:, method:, status:, body:) ⇒ Object
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
5 6 7 8 |
# File 'lib/dryer/routes/registry.rb', line 5 def initialize @resources = [] @routes = [] end |
Instance Attribute Details
#resources ⇒ Object
Returns the value of attribute resources.
70 71 72 |
# File 'lib/dryer/routes/registry.rb', line 70 def resources @resources end |
#routes ⇒ Object
Returns the value of attribute routes.
70 71 72 |
# File 'lib/dryer/routes/registry.rb', line 70 def routes @routes end |
Instance Method Details
#get_validated_values(request) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/dryer/routes/registry.rb', line 58 def get_validated_values(request) route_for( controller: request.controller_class, method: request.request_method_symbol ).then do |route| ExtractValidatedKeys.call( payload: request.params, contract: route.request_contract ) end end |
#register(*resources) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/dryer/routes/registry.rb', line 10 def register(*resources) resources = resources[0].is_a?(Array) ? resources[0] : resources validate_resources!(resources) @resources = resources @routes = resources.map do |r| BuildFromResource.call(r) end.flatten ResourceAccessors.call(object: self, resources: resources) @routes end |
#route_for(controller:, method:) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/dryer/routes/registry.rb', line 51 def route_for(controller:, method:) @routes.filter do |r| r.controller == controller && r.method == method end.first end |
#to_rails_routes(router) ⇒ Object
21 22 23 |
# File 'lib/dryer/routes/registry.rb', line 21 def to_rails_routes(router) @routes.map { |r| r.to_rails_route(router) } end |
#validate_request(request) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/dryer/routes/registry.rb', line 25 def validate_request(request) route_for( controller: request.controller_class, method: request.request_method_symbol ).then do |route| if route && route.request_contract route.request_contract.new.call(request.params).errors else [] end end end |
#validate_response(controller:, method:, status:, body:) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dryer/routes/registry.rb', line 38 def validate_response(controller:, method:, status:, body:) route_for( controller: controller.class, method: method.to_sym ).then do |route| if route && route.response_contract_for(status) route.response_contract_for(status).new.call(body).errors else [] end end end |