Class: Micro::Cases::Flow

Inherits:
Object
  • Object
show all
Defined in:
lib/micro/cases/flow.rb

Direct Known Subclasses

Safe::Flow

Constant Summary collapse

IsAUseCaseWithDefaults =
-> arg { arg.is_a?(Array) && Micro.case?(arg[0]) && arg[1].is_a?(Hash) }
IsAValidUseCase =
-> use_case { Micro.case?(use_case) || IsAUseCaseWithDefaults[use_case] }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(use_cases) ⇒ Flow

Returns a new instance of Flow.



19
20
21
22
23
# File 'lib/micro/cases/flow.rb', line 19

def initialize(use_cases)
  @use_cases = use_cases.dup.freeze
  @next_ones = use_cases.dup
  @first = @next_ones.shift
end

Instance Attribute Details

#use_casesObject (readonly)

Returns the value of attribute use_cases.



9
10
11
# File 'lib/micro/cases/flow.rb', line 9

def use_cases
  @use_cases
end

Class Method Details

.build(args) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/micro/cases/flow.rb', line 11

def self.build(args)
  use_cases = Utils.map_use_cases(args)

  raise Error::InvalidUseCases if use_cases.none?(&IsAValidUseCase)

  new(use_cases)
end

Instance Method Details

#call(input = Kind::Empty::HASH) {|result_wrapper| ... } ⇒ Object Also known as: __call__

Yields:

  • (result_wrapper)


37
38
39
40
41
42
43
44
45
46
47
# File 'lib/micro/cases/flow.rb', line 37

def call(input = Kind::Empty::HASH)
  result = call!(input: input, result: Case::Result.new)

  return result unless block_given?

  result_wrapper = ::Micro::Case::Result::Wrapper.new(result)

  yield(result_wrapper)

  result_wrapper.output
end

#call!(input:, result:) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/micro/cases/flow.rb', line 29

def call!(input:, result:)
  first_result = __call_use_case(@first, result, input)

  return first_result if @next_ones.empty?

  __call_next_use_cases(first_result)
end

#inspectObject



25
26
27
# File 'lib/micro/cases/flow.rb', line 25

def inspect
  '#<(%s) use_cases=%s>' % [self.class, @use_cases]
end

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/micro/cases/flow.rb', line 55

def 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



51
52
53
# File 'lib/micro/cases/flow.rb', line 51

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