Class: ServicePattern::Service
- Inherits:
-
Object
- Object
- ServicePattern::Service
- Defined in:
- lib/service_pattern/service.rb
Class Method Summary collapse
- .call(*args, &blk) ⇒ Object
-
.chain(*args, &blk) ⇒ Object
The same as execute but doesn’t catch FailedError so they are passed on to the parent service call.
- .convert_errors(errors) ⇒ Object
- .execute(*args, &blk) ⇒ Object
- .execute!(*args, &blk) ⇒ Object
- .fail!(errors) ⇒ Object
Instance Method Summary collapse
- #can_execute? ⇒ Boolean
- #execute(*_args) ⇒ Object
- #fail!(errors, type: nil) ⇒ Object
- #succeed!(result = nil) ⇒ Object
Class Method Details
.call(*args, &blk) ⇒ Object
12 13 14 |
# File 'lib/service_pattern/service.rb', line 12 def self.call(*args, &blk) execute(*args, &blk) end |
.chain(*args, &blk) ⇒ Object
The same as execute but doesn’t catch FailedError so they are passed on to the parent service call
3 4 5 6 7 8 9 10 |
# File 'lib/service_pattern/service.rb', line 3 def self.chain(*args, &blk) service = new(*args, &blk) can_execute_response = service.can_execute? ServicePattern::Service.fail!(can_execute_response.errors) unless can_execute_response.success? service.execute end |
.convert_errors(errors) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/service_pattern/service.rb', line 36 def self.convert_errors(errors) errors = [errors] unless errors.is_a?(Array) errors.map do |error| error = ServicePattern::FailError.new(message: error) unless error.is_a?(ServicePattern::FailError) error end end |
.execute(*args, &blk) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/service_pattern/service.rb', line 16 def self.execute(*args, &blk) service = new(*args, &blk) can_execute_response = service.can_execute? return can_execute_response unless can_execute_response.success? service.execute rescue ServicePattern::FailedError => e ServicePattern::Response.new(errors: e.errors) end |
.execute!(*args, &blk) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/service_pattern/service.rb', line 27 def self.execute!(*args, &blk) service = new(*args, &blk) can_execute_response = service.can_execute? ServicePattern::Service.fail!(can_execute_response.errors) unless can_execute_response.success? response = service.execute ServicePattern::Service.fail!(response.errors) unless response.success? response.result end |
.fail!(errors) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/service_pattern/service.rb', line 44 def self.fail!(errors) errors = convert_errors(errors) = errors.map(&:message) error = ServicePattern::FailedError.new(.join(". ")) error.errors = errors raise error end |
Instance Method Details
#can_execute? ⇒ Boolean
54 55 56 |
# File 'lib/service_pattern/service.rb', line 54 def can_execute? succeed! end |
#execute(*_args) ⇒ Object
58 59 60 |
# File 'lib/service_pattern/service.rb', line 58 def execute(*_args) raise NoMethodError, "You should implement the `execute` method on your service" end |
#fail!(errors, type: nil) ⇒ Object
62 63 64 65 66 |
# File 'lib/service_pattern/service.rb', line 62 def fail!(errors, type: nil) errors = [ServicePattern::FailError.new(message: errors, type: type)] if type ServicePattern::Service.fail!(errors) end |
#succeed!(result = nil) ⇒ Object
68 69 70 |
# File 'lib/service_pattern/service.rb', line 68 def succeed!(result = nil) ServicePattern::Response.new(result: result) end |