Class: Micro::Case
- Inherits:
-
Object
show all
- Includes:
- Attributes, Attributes::Features::Accept, Attributes::Features::ActiveModelValidations
- Defined in:
- lib/micro/case.rb,
lib/micro/case/safe.rb,
lib/micro/case/check.rb,
lib/micro/case/error.rb,
lib/micro/case/config.rb,
lib/micro/case/result.rb,
lib/micro/case/strict.rb,
lib/micro/case/version.rb,
lib/micro/case/result/wrapper.rb,
lib/micro/case/result/contract.rb,
lib/micro/case/result/transitions.rb,
lib/micro/case/with_activemodel_validation.rb
Defined Under Namespace
Modules: Check, Error, Utils
Classes: Config, Result, Safe, Strict
Constant Summary
collapse
- InspectKey =
:__inspect_key__
- VERSION =
'5.7.0'.freeze
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(input) ⇒ Case
Returns a new instance of Case.
206
207
208
|
# File 'lib/micro/case.rb', line 206
def initialize(input)
__setup_use_case(input)
end
|
Class Attribute Details
.check ⇒ Object
Returns the value of attribute check.
23
24
25
|
# File 'lib/micro/case.rb', line 23
def check
@check
end
|
Class Method Details
.__call__ {|result_wrapper| ... } ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/micro/case.rb', line 101
def self.call(input = Kind::Empty::HASH)
result = __new__(Result.new, input).__call__
return result unless block_given?
result_wrapper = Result::Wrapper.new(result)
yield(result_wrapper)
result_wrapper.output
end
|
.__call__! ⇒ Object
165
166
167
168
169
|
# File 'lib/micro/case.rb', line 165
def self.__call__!
return const_get(FLOW_STEP) if const_defined?(FLOW_STEP, false)
class_eval("class #{FLOW_STEP} < #{self.name}; private def __call; __call_use_case; end; end; #{FLOW_STEP}")
end
|
.__flow_builder__ ⇒ Object
133
134
135
|
# File 'lib/micro/case.rb', line 133
def self.__flow_builder__
Cases::Flow
end
|
.__flow_get__ ⇒ Object
137
138
139
|
# File 'lib/micro/case.rb', line 137
def self.__flow_get__
return @__flow if defined?(@__flow)
end
|
.__flow_set__! ⇒ Object
180
181
182
|
# File 'lib/micro/case.rb', line 180
def self.__flow_set__!
__flow_set(__flow_use_cases_get) if !__flow_get__ && __flow_use_cases
end
|
.__new__(result, arg) ⇒ Object
125
126
127
128
129
|
# File 'lib/micro/case.rb', line 125
def self.__new__(result, arg)
input = result.__set_accessible_attributes__(arg)
new(input).__set_result__(result)
end
|
.__results_contract__ ⇒ Object
80
81
82
83
84
85
|
# File 'lib/micro/case.rb', line 80
def self.__results_contract__
return @__results_contract if defined?(@__results_contract)
parent = superclass
parent.respond_to?(:__results_contract__) ? parent.__results_contract__ : nil
end
|
.__transaction_class__ ⇒ Object
93
94
95
96
97
98
|
# File 'lib/micro/case.rb', line 93
def self.__transaction_class__
return @__transaction_class if defined?(@__transaction_class)
parent = superclass
parent.respond_to?(:__transaction_class__) ? parent.__transaction_class__ : nil
end
|
.auto_validation_disabled? ⇒ Boolean
11
12
13
|
# File 'lib/micro/case/with_activemodel_validation.rb', line 11
def self.auto_validation_disabled?
return @disable_auto_validation if defined?(@disable_auto_validation)
end
|
.call(input = Kind::Empty::HASH) {|result_wrapper| ... } ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/micro/case.rb', line 30
def self.call(input = Kind::Empty::HASH)
result = __new__(Result.new, input).__call__
return result unless block_given?
result_wrapper = Result::Wrapper.new(result)
yield(result_wrapper)
result_wrapper.output
end
|
.call! ⇒ Object
107
108
109
|
# File 'lib/micro/case.rb', line 107
def call!
self
end
|
.config {|Config.instance| ... } ⇒ Object
103
104
105
|
# File 'lib/micro/case.rb', line 103
def config
yield(Config.instance)
end
|
.disable_auto_validation ⇒ Object
15
16
17
|
# File 'lib/micro/case/with_activemodel_validation.rb', line 15
def self.disable_auto_validation
@disable_auto_validation = true
end
|
.flow(*args, transaction: nil, steps: nil) ⇒ Object
66
67
68
69
70
71
|
# File 'lib/micro/case.rb', line 66
def self.flow(*args, transaction: nil, steps: nil)
::Micro::Case.check.flow_steps_kwarg!(args.empty? ? nil : args, steps, "#{self.name}.flow")
@__flow_use_cases = Cases::Utils.map_use_cases(steps || args)
@__flow_transaction = transaction
end
|
.inherited(subclass) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/micro/case.rb', line 112
def self.inherited(subclass)
subclass.__attributes_set_after_inherit__(self.__attributes_data__)
subclass.extend ::Micro::Attributes.const_get('Macros::ForSubclasses'.freeze)
if self.send(:__flow_use_cases) && !subclass.name.to_s.end_with?(FLOW_STEP)
raise "Wooo, you can't do this! Inherits from a use case which has an inner flow violates "\
"one of the project principles: Solve complex business logic, by allowing the composition of use cases. "\
"Instead of doing this, declare a new class/constant with the steps needed.\n\n"\
"Related issue: https://github.com/serradura/u-case/issues/19\n"
end
end
|
.inspect ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/micro/case.rb', line 186
def self.inspect
ids = (Thread.current[InspectKey] ||= [])
if ids.include?(object_id)
return sprintf('#<%s: ...>', self)
end
begin
ids << object_id
if __flow_use_cases
return '<%s (%s) use_cases=%s>' % [self, __flow_builder__, @__flow_use_cases]
else
return '<%s (%s) attributes=%s>' % [self, self.superclass, attributes]
end
ensure
ids.pop
end
end
|
.results(&block) ⇒ Object
73
74
75
76
77
78
|
# File 'lib/micro/case.rb', line 73
def self.results(&block)
raise ArgumentError, 'a block is required'.freeze unless block
raise ArgumentError, 'must be called on a Micro::Case subclass, not on Micro::Case itself'.freeze if self == ::Micro::Case
@__results_contract = Result::Contract.define(&block)
end
|
.then(use_case = nil, &block) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/micro/case.rb', line 45
def self.then(use_case = nil, &block)
can_yield_self = respond_to?(:yield_self)
if block
raise INVALID_INVOCATION_OF_THE_THEN_METHOD if use_case
raise NotImplementedError if !can_yield_self
yield_self(&block)
else
return yield_self if !use_case && can_yield_self
::Micro::Case.check.then_use_case_or_flow!(use_case, 'Micro::Case.')
self.call.then(use_case)
end
end
|
.to_proc ⇒ Object
62
63
64
|
# File 'lib/micro/case.rb', line 62
def self.to_proc
Proc.new { |arg| call(arg) }
end
|
.transaction(with:) ⇒ Object
87
88
89
90
91
|
# File 'lib/micro/case.rb', line 87
def self.transaction(with:)
::Micro::Case.check.transaction_owner!(with)
@__transaction_class = with
end
|
.use_cases ⇒ Object
144
|
# File 'lib/micro/case.rb', line 144
def self.use_cases; __flow_get__.use_cases; end
|
Instance Method Details
#__call__ ⇒ Object
214
215
216
|
# File 'lib/micro/case.rb', line 214
def __call__
__call_the_use_case_or_its_flow
end
|
#__set_result__(result) ⇒ Object
218
219
220
221
222
223
224
225
|
# File 'lib/micro/case.rb', line 218
def __set_result__(result)
::Micro::Case.check.result_instance!(result)
::Micro::Case.check.result_not_defined!(defined?(@__result))
@__result = result
self
end
|
#call! ⇒ Object
210
211
212
|
# File 'lib/micro/case.rb', line 210
def call!
raise NotImplementedError
end
|