Class: ServicePattern::Service
- Inherits:
-
Object
- Object
- ServicePattern::Service
- Defined in:
- lib/service_pattern/service.rb
Class Method Summary collapse
- .argument(argument_name, **args) ⇒ Object
- .arguments(*arguments) ⇒ Object
- .call(*args, **opts, &blk) ⇒ Object
-
.chain(*args, **opts, &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, **opts, &blk) ⇒ Object
- .execute!(*args, **opts, &blk) ⇒ Object
- .fail!(errors) ⇒ Object
Instance Method Summary collapse
- #can_execute? ⇒ Boolean
- #execute ⇒ Object
- #execute! ⇒ Object
- #fail!(errors, type: nil) ⇒ Object
-
#initialize(**args) ⇒ Service
constructor
A new instance of Service.
- #perform(*_args) ⇒ Object
- #succeed!(result = nil) ⇒ Object
Constructor Details
#initialize(**args) ⇒ Service
Returns a new instance of Service.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/service_pattern/service.rb', line 63 def initialize(**args) arguments = self.class.instance_variable_get(:@arguments) arguments&.each do |argument_name, | raise ArgumentError, "missing keyword: #{argument_name}" if !args.key?(argument_name) && !.key?(:default) end args.each do |key, value| raise ArgumentError, "unknown keyword: #{key}" unless arguments&.key?(key) __send__("#{key}=", value) end end |
Class Method Details
.argument(argument_name, **args) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/service_pattern/service.rb', line 48 def self.argument(argument_name, **args) attr_accessor argument_name @arguments ||= {} @arguments[argument_name] ||= {} args.each do |key, value| if key == :default @arguments[argument_name][key] = value else raise ArgumentError, "Invalid argument: #{argument_name}" end end end |
.arguments(*arguments) ⇒ Object
42 43 44 45 46 |
# File 'lib/service_pattern/service.rb', line 42 def self.arguments(*arguments) arguments.each do |argument_name| argument argument_name end end |
.call(*args, **opts, &blk) ⇒ Object
12 13 14 |
# File 'lib/service_pattern/service.rb', line 12 def self.call(*args, **opts, &blk) execute(*args, **opts, &blk) end |
.chain(*args, **opts, &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, **opts, &blk) service = new(*args, **opts, &blk) can_execute_response = service.can_execute? ServicePattern::Service.fail!(can_execute_response.errors) unless can_execute_response.success? service.perform end |
.convert_errors(errors) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/service_pattern/service.rb', line 24 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, **opts, &blk) ⇒ Object
16 17 18 |
# File 'lib/service_pattern/service.rb', line 16 def self.execute(*args, **opts, &blk) new(*args, **opts, &blk).execute end |
.execute!(*args, **opts, &blk) ⇒ Object
20 21 22 |
# File 'lib/service_pattern/service.rb', line 20 def self.execute!(*args, **opts, &blk) new(*args, **opts, &blk).execute! end |
.fail!(errors) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/service_pattern/service.rb', line 32 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
76 77 78 |
# File 'lib/service_pattern/service.rb', line 76 def can_execute? succeed! end |
#execute ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/service_pattern/service.rb', line 80 def execute can_execute_response = can_execute? return can_execute_response unless can_execute_response.success? response = perform ServicePattern::Response.check_response!(self, response) response rescue ServicePattern::FailedError => e ServicePattern::Response.new(errors: e.errors) end |
#execute! ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/service_pattern/service.rb', line 91 def execute! can_execute_response = can_execute? ServicePattern::Service.fail!(can_execute_response.errors) unless can_execute_response.success? response = perform ServicePattern::Response.check_response!(self, response) ServicePattern::Service.fail!(response.errors) unless response.success? response.result end |
#fail!(errors, type: nil) ⇒ Object
104 105 106 107 108 |
# File 'lib/service_pattern/service.rb', line 104 def fail!(errors, type: nil) errors = [ServicePattern::FailError.new(message: errors, type: type)] if type ServicePattern::Service.fail!(errors) end |
#perform(*_args) ⇒ Object
100 101 102 |
# File 'lib/service_pattern/service.rb', line 100 def perform(*_args) raise NoMethodError, "You should implement the `perform` method on your service" end |
#succeed!(result = nil) ⇒ Object
110 111 112 |
# File 'lib/service_pattern/service.rb', line 110 def succeed!(result = nil) ServicePattern::Response.new(result: result) end |