Class: Protein::Service
- Inherits:
-
Object
- Object
- Protein::Service
- Defined in:
- lib/protein/service.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Class Method Summary collapse
- .proto(proto_module = nil) ⇒ Object
- .proto_module ⇒ Object
- .request(request_class) ⇒ Object
- .request_class ⇒ Object
- .response(response_class) ⇒ Object
- .response? ⇒ Boolean
- .response_class ⇒ Object
- .service(service_name) ⇒ Object
- .service_name ⇒ Object
Instance Method Summary collapse
- #add_error(*args) ⇒ Object
- #failure? ⇒ Boolean
-
#initialize(request) ⇒ Service
constructor
A new instance of Service.
- #process ⇒ Object
- #reject(*args) ⇒ Object
- #resolve(response = nil) ⇒ Object
- #response? ⇒ Boolean
- #response_class ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(request) ⇒ Service
Returns a new instance of Service.
50 51 52 |
# File 'lib/protein/service.rb', line 50 def initialize(request) @request = request end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
48 49 50 |
# File 'lib/protein/service.rb', line 48 def errors @errors end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
48 49 50 |
# File 'lib/protein/service.rb', line 48 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
48 49 50 |
# File 'lib/protein/service.rb', line 48 def response @response end |
Class Method Details
.proto(proto_module = nil) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/protein/service.rb', line 12 def proto(proto_module = nil) @proto_module = proto_module @service_name ||= proto_module.to_s.split("::").last. scan(/[A-Z][^A-Z]*/).map(&:downcase).join("_") @request_class ||= "#{proto_module}::Request" @response_class ||= "#{proto_module}::Response" end |
.proto_module ⇒ Object
20 21 22 |
# File 'lib/protein/service.rb', line 20 def proto_module GetConst.call(@proto_module) end |
.request(request_class) ⇒ Object
24 25 26 |
# File 'lib/protein/service.rb', line 24 def request(request_class) @request_class = request_class end |
.request_class ⇒ Object
28 29 30 |
# File 'lib/protein/service.rb', line 28 def request_class GetConst.call(@request_class) end |
.response(response_class) ⇒ Object
39 40 41 |
# File 'lib/protein/service.rb', line 39 def response(response_class) @response_class = response_class end |
.response? ⇒ Boolean
32 33 34 35 36 37 |
# File 'lib/protein/service.rb', line 32 def response? response_class != false && (!response_class.is_a?(String) || !(Object.const_get(response_class) rescue nil).nil?) rescue NameError false end |
.response_class ⇒ Object
43 44 45 |
# File 'lib/protein/service.rb', line 43 def response_class GetConst.call(@response_class) end |
.service(service_name) ⇒ Object
4 5 6 |
# File 'lib/protein/service.rb', line 4 def service(service_name) @service_name = service_name end |
.service_name ⇒ Object
8 9 10 |
# File 'lib/protein/service.rb', line 8 def service_name @service_name || raise(DefinitionError, "service name is not defined") end |
Instance Method Details
#add_error(*args) ⇒ Object
116 117 118 |
# File 'lib/protein/service.rb', line 116 def add_error(*args) @errors << build_error(*args) end |
#failure? ⇒ Boolean
112 113 114 |
# File 'lib/protein/service.rb', line 112 def failure? !success? end |
#process ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/protein/service.rb', line 62 def process @success = nil @response = response_class.new if response? @errors = [] call raise(ProcessingError, "resolve/reject must be called") if response? && @success.nil? end |
#reject(*args) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/protein/service.rb', line 87 def reject(*args) raise(ProcessingError, "can't reject non-responding service") unless response? if args.any? && @errors && @errors.any? raise(ProcessingError, "unable to reject with both rejection value and previous errors") end @success = false @errors = if args.empty? && @errors && @errors.any? @errors elsif args.empty? [build_error(:error)] elsif args.length == 1 && args[0].is_a?(Array) && args[0].any? args[0].map { |error| build_error(error) } else [build_error(*args)] end end |
#resolve(response = nil) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/protein/service.rb', line 72 def resolve(response = nil) raise(ProcessingError, "can't resolve non-responding service") unless response? raise(ProcessingError, "unable to resolve with previous errors") if @errors && @errors.any? @success = true @response = if !response @response elsif response.is_a?(response_class) response else response_class.new(response) end end |
#response? ⇒ Boolean
54 55 56 |
# File 'lib/protein/service.rb', line 54 def response? self.class.response? end |
#response_class ⇒ Object
58 59 60 |
# File 'lib/protein/service.rb', line 58 def response_class self.class.response_class end |
#success? ⇒ Boolean
106 107 108 109 110 |
# File 'lib/protein/service.rb', line 106 def success? raise(ProcessingError, "resolve/reject must be called first") if @success.nil? @success end |