Class: Micro::Case

Inherits:
Object
  • Object
show all
Includes:
Attributes, Attributes::Features::ActiveModelValidations
Defined in:
lib/micro/case.rb,
lib/micro/case/safe.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/transitions.rb,
lib/micro/case/with_activemodel_validation.rb

Direct Known Subclasses

Safe, Strict

Defined Under Namespace

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

Constant Summary collapse

InspectKey =

:nodoc:

:__inspect_key__
VERSION =
'4.5.2'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Case

Returns a new instance of Case.



158
159
160
# File 'lib/micro/case.rb', line 158

def initialize(input)
  __setup_use_case(input)
end

Class Method Details

.__call__ {|result_wrapper| ... } ⇒ Object

Yields:

  • (result_wrapper)


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/micro/case.rb', line 63

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



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

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



95
96
97
# File 'lib/micro/case.rb', line 95

def self.__flow_builder__
  Cases::Flow
end

.__flow_get__Object



99
100
101
# File 'lib/micro/case.rb', line 99

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

.__flow_set__!Object



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

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

.__new__(result, arg) ⇒ Object



87
88
89
90
91
# File 'lib/micro/case.rb', line 87

def self.__new__(result, arg)
  input = result.__set_accessible_attributes__(arg)

  new(input).__set_result__(result)
end

.auto_validation_disabled?Boolean

Returns:

  • (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

Yields:

  • (result_wrapper)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/micro/case.rb', line 22

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



69
70
71
# File 'lib/micro/case.rb', line 69

def call!
  self
end

.config {|Config.instance| ... } ⇒ Object

Yields:



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

def config
  yield(Config.instance)
end

.disable_auto_validationObject



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



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

def self.flow(*args)
  @__flow_use_cases = Cases::Utils.map_use_cases(args)
end

.inherited(subclass) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/micro/case.rb', line 74

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

.inspectObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/micro/case.rb', line 138

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

.then(use_case = nil, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/micro/case.rb', line 37

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

    raise INVALID_INVOCATION_OF_THE_THEN_METHOD unless ::Micro.case_or_flow?(use_case)

    self.call.then(use_case)
  end
end

.to_procObject



54
55
56
# File 'lib/micro/case.rb', line 54

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

.use_casesObject



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

def self.use_cases; __flow_get__.use_cases; end

Instance Method Details

#__call__Object



166
167
168
# File 'lib/micro/case.rb', line 166

def __call__
  __call_the_use_case_or_its_flow
end

#__set_result__(result) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/micro/case.rb', line 170

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

  @__result = result

  self
end

#call!Object

Raises:

  • (NotImplementedError)


162
163
164
# File 'lib/micro/case.rb', line 162

def call!
  raise NotImplementedError
end