Class: Protein::Router
- Inherits:
-
Object
- Object
- Protein::Router
- Defined in:
- lib/protein/router.rb
Class Method Summary collapse
- .define(router) ⇒ Object
- .from_hash(hash) ⇒ Object
- .resolve_by_name(service_name) ⇒ Object
- .resolve_by_request(request) ⇒ Object
- .service(service_class) ⇒ Object
- .services ⇒ Object
Class Method Details
.define(router) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/protein/router.rb', line 4 def define(router) if router.is_a?(Class) || router.is_a?(String) router elsif router.is_a?(Hash) router_class = Class.new(Router) router_class.from_hash(router) router_class else raise(DefinitionError, "invalid router definition") end end |
.from_hash(hash) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/protein/router.rb', line 16 def from_hash(hash) if hash[:services] hash[:services].each do |each_service| service(each_service) end end if hash[:protos] hash[:protos].each do |each_proto| service_class = Class.new(Service) service_class.proto(each_proto) service(service_class) end end end |
.resolve_by_name(service_name) ⇒ Object
42 43 44 45 46 |
# File 'lib/protein/router.rb', line 42 def resolve_by_name(service_name) services.find do |service| service.service_name == service_name.to_s end || raise(RoutingError, "service #{service_name.inspect} not found") end |
.resolve_by_request(request) ⇒ Object
48 49 50 51 52 |
# File 'lib/protein/router.rb', line 48 def resolve_by_request(request) services.find do |service| request.is_a?(service.request_class) end || raise(RoutingError, "service for #{request.class} not found") end |
.service(service_class) ⇒ Object
33 34 35 36 |
# File 'lib/protein/router.rb', line 33 def service(service_class) @services ||= [] @services << service_class end |