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/utils.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.1.1'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Case

Returns a new instance of Case.



154
155
156
# File 'lib/micro/case.rb', line 154

def initialize(input)
  __setup_use_case(input)
end

Class Method Details

.__call__ {|Result::Wrapper.new(result)| ... } ⇒ Object

Yields:



59
60
61
62
63
64
65
# File 'lib/micro/case.rb', line 59

def self.call(input = Kind::Empty::HASH)
  result = __new__(Result.new, input).__call__

  return result unless block_given?

  yield Result::Wrapper.new(result)
end

.__call__!Object



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

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

.__flow_builder__Object



91
92
93
# File 'lib/micro/case.rb', line 91

def self.__flow_builder__
  Cases::Flow
end

.__flow_get__Object



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

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

.__flow_set__!Object



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

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

.__new__(result, arg) ⇒ Object



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

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.new(result)| ... } ⇒ Object

Yields:



22
23
24
25
26
27
28
# 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?

  yield Result::Wrapper.new(result)
end

.call!Object



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

def call!
  self
end

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

Yields:



61
62
63
# File 'lib/micro/case.rb', line 61

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



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

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

.inherited(subclass) ⇒ Object



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

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/micro/case.rb', line 134

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/micro/case.rb', line 33

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



50
51
52
# File 'lib/micro/case.rb', line 50

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

.use_casesObject



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

def self.use_cases; __flow_get__.use_cases; end

Instance Method Details

#__call__Object



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

def __call__
  __call_the_use_case_or_its_flow
end

#__set_result__(result) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/micro/case.rb', line 166

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)


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

def call!
  raise NotImplementedError
end