lib/micro/case.rb in u-case-2.3.1 vs lib/micro/case.rb in u-case-2.4.0
- old
+ new
@@ -1,13 +1,14 @@
# frozen_string_literal: true
+require 'kind'
require 'micro/attributes'
-# frozen_string_literal: true
module Micro
class Case
require 'micro/case/version'
+ require 'micro/case/utils'
require 'micro/case/result'
require 'micro/case/error'
require 'micro/case/safe'
require 'micro/case/strict'
require 'micro/case/flow/reducer'
@@ -40,10 +41,18 @@
instance = new(arg)
instance.__set_result__(result)
instance
end
+ def self.__call_and_set_transition__(result, arg)
+ if arg.respond_to?(:keys)
+ result.__set_transitions_accessible_attributes__(arg.keys)
+ end
+
+ __new__(result, arg).call
+ end
+
def self.__call!
return const_get(:Flow_Step) if const_defined?(:Flow_Step)
const_set(:Flow_Step, Class.new(self) do
private def __call
@@ -59,22 +68,26 @@
def self.__flow_reducer
Flow::Reducer
end
def self.__flow_get
- @__flow
+ return @__flow if defined?(@__flow)
end
- private_class_method def self.__flow_use_cases_set(args)
- @__flow_use_cases = args
+ private_class_method def self.__flow_use_cases
+ return @__flow_use_cases if defined?(@__flow_use_cases)
end
private_class_method def self.__flow_use_cases_get
- Array(@__flow_use_cases)
+ Array(__flow_use_cases)
.map { |use_case| use_case == self ? self.__call! : use_case }
end
+ private_class_method def self.__flow_use_cases_set(args)
+ @__flow_use_cases = args
+ end
+
private_class_method def self.__flow_set(args)
return if __flow_get
def self.use_cases; __flow_get.use_cases; end
@@ -82,11 +95,11 @@
@__flow = __flow_reducer.build(args)
end
def self.__flow_set!
- __flow_set(__flow_use_cases_get) if !__flow_get && @__flow_use_cases
+ __flow_set(__flow_use_cases_get) if !__flow_get && __flow_use_cases
end
def self.flow(*args)
__flow_use_cases_set(args)
end
@@ -103,11 +116,11 @@
__call
end
def __set_result__(result)
raise Error::InvalidResultInstance unless result.is_a?(Result)
- raise Error::ResultIsAlreadyDefined if @__result
+ raise Error::ResultIsAlreadyDefined if defined?(@__result)
@__result = result
end
private
@@ -143,28 +156,32 @@
end
def Success(arg = :ok)
value, type = block_given? ? [yield, arg] : [arg, :ok]
- __get_result__.__set__(true, value, type, nil)
+ __get_result_with(true, value, type)
end
def Failure(arg = :error)
value = block_given? ? yield : arg
type = __map_failure_type(value, block_given? ? arg : :error)
- __get_result__.__set__(false, value, type, self)
+ __get_result_with(false, value, type)
end
- def __get_result__
- @__result ||= Result.new
- end
-
def __map_failure_type(arg, type)
return type if type != :error
return arg if arg.is_a?(Symbol)
return :exception if arg.is_a?(Exception)
type
+ end
+
+ def __get_result__
+ @__result ||= Result.new
+ end
+
+ def __get_result_with(is_success, value, type)
+ __get_result__.__set__(is_success, value, type, self)
end
end
end