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.5.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.
180
181
182
|
# File 'lib/micro/case.rb', line 180
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
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/micro/case.rb', line 85
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
139
140
141
142
143
|
# File 'lib/micro/case.rb', line 139
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
117
118
119
|
# File 'lib/micro/case.rb', line 117
def self.__flow_builder__
Cases::Flow
end
|
.__flow_get__ ⇒ Object
121
122
123
|
# File 'lib/micro/case.rb', line 121
def self.__flow_get__
return @__flow if defined?(@__flow)
end
|
.__flow_set__! ⇒ Object
154
155
156
|
# File 'lib/micro/case.rb', line 154
def self.__flow_set__!
__flow_set(__flow_use_cases_get) if !__flow_get__ && __flow_use_cases
end
|
.__new__(result, arg) ⇒ Object
109
110
111
112
113
|
# File 'lib/micro/case.rb', line 109
def self.__new__(result, arg)
input = result.__set_accessible_attributes__(arg)
new(input).__set_result__(result)
end
|
.__results_contract__ ⇒ Object
77
78
79
80
81
82
|
# File 'lib/micro/case.rb', line 77
def self.__results_contract__
return @__results_contract if defined?(@__results_contract)
parent = superclass
parent.respond_to?(:__results_contract__) ? parent.__results_contract__ : 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
91
92
93
|
# File 'lib/micro/case.rb', line 91
def call!
self
end
|
.config {|Config.instance| ... } ⇒ Object
87
88
89
|
# File 'lib/micro/case.rb', line 87
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) ⇒ Object
66
67
68
|
# File 'lib/micro/case.rb', line 66
def self.flow(*args)
@__flow_use_cases = Cases::Utils.map_use_cases(args)
end
|
.inherited(subclass) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/micro/case.rb', line 96
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/micro/case.rb', line 160
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
70
71
72
73
74
75
|
# File 'lib/micro/case.rb', line 70
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
|
.use_cases ⇒ Object
128
|
# File 'lib/micro/case.rb', line 128
def self.use_cases; __flow_get__.use_cases; end
|
Instance Method Details
#__call__ ⇒ Object
188
189
190
|
# File 'lib/micro/case.rb', line 188
def __call__
__call_the_use_case_or_its_flow
end
|
#__set_result__(result) ⇒ Object
192
193
194
195
196
197
198
199
|
# File 'lib/micro/case.rb', line 192
def __set_result__(result)
::Micro::Case.check.result_instance!(result)
::Micro::Case.check.result_not_defined!(defined?(@__result))
@__result = result
self
end
|
#call! ⇒ Object
184
185
186
|
# File 'lib/micro/case.rb', line 184
def call!
raise NotImplementedError
end
|