Class: Micro::Case

Inherits:
Object
  • Object
show all
Includes:
Attributes::Features::ActiveModelValidations
Defined in:
lib/micro/case.rb,
lib/micro/case/flow.rb,
lib/micro/case/safe.rb,
lib/micro/case/error.rb,
lib/micro/case/utils.rb,
lib/micro/case/result.rb,
lib/micro/case/strict.rb,
lib/micro/case/version.rb,
lib/micro/case/safe/flow.rb,
lib/micro/case/flow/reducer.rb,
lib/micro/case/with_activemodel_validation.rb

Direct Known Subclasses

Safe, Strict

Defined Under Namespace

Modules: Error, Flow, Utils Classes: Result, Safe, Strict

Constant Summary collapse

VERSION =
'2.5.0'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Case

Returns a new instance of Case.



119
120
121
# File 'lib/micro/case.rb', line 119

def initialize(input)
  __setup_use_case(input)
end

Class Method Details

.&(use_case) ⇒ Object



32
33
34
# File 'lib/micro/case.rb', line 32

def self.&(use_case)
  Safe::Flow([self, use_case])
end

.>>(use_case) ⇒ Object



28
29
30
# File 'lib/micro/case.rb', line 28

def self.>>(use_case)
  Flow([self, use_case])
end

.__call!Object



58
59
60
61
62
# File 'lib/micro/case.rb', line 58

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")
end

.__call_and_set_transition__(result, arg) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/micro/case.rb', line 46

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

.__flow_getObject



84
85
86
# File 'lib/micro/case.rb', line 84

def self.__flow_get
  return @__flow if defined?(@__flow)
end

.__flow_reducerObject



80
81
82
# File 'lib/micro/case.rb', line 80

def self.__flow_reducer
  Flow::Reducer
end

.__flow_set!Object



111
112
113
# File 'lib/micro/case.rb', line 111

def self.__flow_set!
  __flow_set(__flow_use_cases_get) if !__flow_get && __flow_use_cases
end

.__new__(result, arg) ⇒ Object



40
41
42
43
44
# File 'lib/micro/case.rb', line 40

def self.__new__(result, arg)
  instance = new(arg)
  instance.__set_result__(result)
  instance
end

.auto_validation_disabled?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/micro/case/with_activemodel_validation.rb', line 9

def self.auto_validation_disabled?
  return @disable_auto_validation if defined?(@disable_auto_validation)
end

.call(options = {}) ⇒ Object



36
37
38
# File 'lib/micro/case.rb', line 36

def self.call(options = {})
  new(options).call
end

.call!Object



64
65
66
# File 'lib/micro/case.rb', line 64

def self.call!
  self
end

.disable_auto_validationObject



13
14
15
# File 'lib/micro/case/with_activemodel_validation.rb', line 13

def self.disable_auto_validation
  @disable_auto_validation = true
end

.Flow(args) ⇒ Object



24
25
26
# File 'lib/micro/case.rb', line 24

def self.Flow(args)
  Flow::Reducer.build(Array(args))
end

.flow(*args) ⇒ Object



115
116
117
# File 'lib/micro/case.rb', line 115

def self.flow(*args)
  __flow_use_cases_set(args)
end

.inherited(subclass) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/micro/case.rb', line 68

def self.inherited(subclass)
  subclass.attributes(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

.to_procObject



20
21
22
# File 'lib/micro/case.rb', line 20

def self.to_proc
  Proc.new { |arg| call(arg) }
end

.use_casesObject



104
# File 'lib/micro/case.rb', line 104

def self.use_cases; __flow_get.use_cases; end

Instance Method Details

#__set_result__(result) ⇒ Object



131
132
133
134
135
136
# File 'lib/micro/case.rb', line 131

def __set_result__(result)
  raise Error::InvalidResultInstance unless result.is_a?(Result)
  raise Error::ResultIsAlreadyDefined if defined?(@__result)

  @__result = result
end

#callObject



127
128
129
# File 'lib/micro/case.rb', line 127

def call
  __call
end

#call!Object

Raises:

  • (NotImplementedError)


123
124
125
# File 'lib/micro/case.rb', line 123

def call!
  raise NotImplementedError
end