Class: Eco::API::UseCases::UseCaseChain

Inherits:
UseCase show all
Defined in:
lib/eco/api/usecases/use_case_chain.rb

Overview

Class that enables to chain multiple UseCase

Constant Summary collapse

MAX_CHAINS =
70
@@num_chains =
0

Instance Attribute Summary

Attributes inherited from UseCase

#name, #options, #times_launched, #type

Instance Method Summary collapse

Methods inherited from UseCase

#chainer, #classed_definition, #source_object

Methods inherited from BaseCase

valid_type?, validate_type

Methods included from Common::ClassHelpers

#class_resolver, #descendants, #descendants?, #inheritable_attrs, #inheritable_class_vars, #inherited, #instance_variable_name, #new_class, #resolve_class, #to_constant

Constructor Details

#initialize(name = nil, type: nil, root:, usecase: nil, &block) ⇒ UseCaseChain

Returns a new instance of UseCaseChain.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/eco/api/usecases/use_case_chain.rb', line 10

def initialize(name = nil, type: nil, root:, usecase: nil, &block)
  if usecase
    raise "Expected Eco::API::UseCases::UseCase. Given #{usecase.class}" if !usecase.is_a?(Eco::API::UseCases::UseCase)
    type = usecase.type
    name = usecase.name
    block = usecase.callback unless block
  end

  super(name, type: type, root: root, &block)
  @chains   = []
  @resolved_chains = nil
end

Instance Method Details

#chain(usecase = nil, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/eco/api/usecases/use_case_chain.rb', line 59

def chain(usecase = nil, &block)
  @@num_chains += 1
  raise "Reached maximum number of chained use cases (#{MAX_CHAINS}). Looks like a recursive cyclic chain 'use'" if @@num_chains >= MAX_CHAINS
  raise "A UseCase can only be chained with another UseCase" if usecase && !usecase.is_a?(UseCase)
  raise "Missuse. Please use either parameter or block but not both" if block_given? && usecase
  usecase = block_given?? block : usecase
  @chains.push(usecase)
  self
end

#launch(io: nil, **kargs) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/eco/api/usecases/use_case_chain.rb', line 49

def launch(io: nil, **kargs)
  super(io: io, **kargs).tap do |uio|
    next if resolved_chains.empty?

    resolved_chains.each do |usecase|
      uio = usecase.launch(io: uio.chain(usecase: usecase))
    end
  end
end

#root=(value) ⇒ Object



23
24
25
26
# File 'lib/eco/api/usecases/use_case_chain.rb', line 23

def root=(value)
  raise "You cannot change root Eco::API::UseCases once the chains have been resolved" if @resolved_chains
  super(value)
end

#use(preserve_chains: false, recursive: false) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/eco/api/usecases/use_case_chain.rb', line 28

def use(preserve_chains: false, recursive: false)
  UseCase.new(@name, type: @type, root: @root, &@case).tap do |newcase|
    if preserve_chains
      chain_use = {preserve_chains: recursive, recursive: recursive}
      @chains = @chains.map do |usecase|
        if usecase.respond_to? :call
          Proc.new do |usegroup|
            usecase = usecase.call(usegroup)
            usecase.use(chain_use).chain_to(newcase)
            usecase
          end
        elsif usecase.is_a?(UseCase)
          usecase.use(chain_use).chain_to(newcase)
          usecase
        end
      end
    end
  end
end