Class: ServicePattern::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/service_pattern/service.rb

Class Method Summary collapse

Instance Method Summary collapse

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?
  raise ServicePattern::FailedError, can_execute_response.errors.join(". ") unless can_execute_response.success?

  service.execute
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.message])
end

.execute!(*args, &blk) ⇒ Object



27
28
29
30
31
32
# File 'lib/service_pattern/service.rb', line 27

def self.execute!(*args, &blk)
  response = execute(*args, &blk)
  raise ServicePattern::FailedError, response.errors.join(". ") unless response.success?

  response.result
end

Instance Method Details

#can_execute?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/service_pattern/service.rb', line 34

def can_execute?
  ServicePattern::Response.new(success: true)
end

#execute(*_args) ⇒ Object

Raises:

  • (NoMethodError)


38
39
40
# File 'lib/service_pattern/service.rb', line 38

def execute(*_args)
  raise NoMethodError, "You should implement the `execute!` method on your service"
end