slayer 0.4.0.beta4 → 0.5.0.beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,136 +0,0 @@
1
- module Slayer
2
- # Slayer Services are objects that should implement re-usable pieces of
3
- # application logic or common tasks. All methods in a service are wrapped
4
- # by default to enforce the return of a +Slayer::Result+ object.
5
- class Service
6
- include Hook
7
-
8
- skip_hook(
9
- :pass,
10
- :flunk,
11
- :flunk!,
12
- :try!,
13
- :wrap_service_methods?,
14
- :__opt_in
15
- )
16
- singleton_skip_hook(
17
- :pass,
18
- :flunk,
19
- :flunk!,
20
- :try!,
21
- :wrap_service_methods?,
22
- :wrap_service_methods!,
23
- :do_not_wrap_service_methods!,
24
- :__opt_in
25
- )
26
-
27
- attr_accessor :result
28
-
29
- class << self
30
- # Create a passing Result
31
- def pass(value: nil, status: :default, message: nil)
32
- Result.new(value, status, message)
33
- end
34
-
35
- # Create a failing Result
36
- def flunk(value: nil, status: :default, message: nil)
37
- Result.new(value, status, message).fail
38
- end
39
-
40
- # Create a failing Result and halt execution of the Command
41
- def flunk!(value: nil, status: :default, message: nil)
42
- raise ResultFailureError, flunk(value: value, status: status, message: message)
43
- end
44
-
45
- # If the block produces a successful result the value of the result will be
46
- # returned. Otherwise, this will create a failing result and halt the execution
47
- # of the Command.
48
- def try!(value: nil, status: nil, message: nil)
49
- r = yield
50
- flunk!(value: value, status: status || :default, message: message) unless r.is_a?(Result)
51
- return r.value if r.success?
52
- flunk!(value: value || r.value, status: status || r.status, message: message || r.message)
53
- end
54
-
55
- def wrap_service_methods!
56
- @__opt_in = true
57
- end
58
-
59
- def wrap_service_methods?
60
- __opt_in
61
- end
62
-
63
- private
64
-
65
- def __opt_in
66
- @__opt_in = false unless defined?(@__opt_in)
67
- @__opt_in == true
68
- end
69
-
70
- end
71
-
72
- def pass(*args)
73
- self.class.pass(*args)
74
- end
75
-
76
- def flunk(*args)
77
- self.class.flunk(*args)
78
- end
79
-
80
- def flunk!(*args)
81
- self.class.flunk!(*args)
82
- end
83
-
84
- def try!(*args, &block)
85
- self.class.try!(*args, &block)
86
- end
87
-
88
- def wrap_service_methods?
89
- self.class.wrap_service_methods?
90
- end
91
-
92
- # Make sure child classes also hook correctly
93
- def self.inherited(klass)
94
- klass.include Hook
95
- klass.hook :__service_hook
96
- end
97
-
98
- hook :__service_hook
99
-
100
- # rubocop:disable Metrics/MethodLength
101
- def self.__service_hook(_, instance, service_block)
102
- return yield unless wrap_service_methods?
103
-
104
- begin
105
- result = yield
106
- rescue ResultFailureError => error
107
- result = error.result
108
- end
109
-
110
- raise CommandNotImplementedError unless result.is_a? Result
111
-
112
- unless service_block.nil?
113
- matcher = Slayer::ResultMatcher.new(result, instance)
114
-
115
- service_block.call(matcher)
116
-
117
- # raise error if not all defaults were handled
118
- unless matcher.handled_defaults?
119
- raise(ResultNotHandledError, 'The pass or fail condition of a result was not handled')
120
- end
121
-
122
- begin
123
- matcher.execute_matching_block
124
- ensure
125
- matcher.execute_ensure_block
126
- end
127
- end
128
- return result
129
- end
130
- # rubocop:enable Metrics/MethodLength
131
-
132
- private_class_method :inherited
133
- private_class_method :__service_hook
134
-
135
- end
136
- end