Module: SEPA::StageList

Defined in:
lib/sepa_rator/profile.rb

Class Method Summary collapse

Class Method Details

.merge(old, operation) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sepa_rator/profile.rb', line 53

def merge(old, operation)
  return Array(operation).freeze if operation.is_a?(Array)

  case operation
  in { replace: klass, with: replacement }
    old.map { |stage| stage == klass ? replacement : stage }.freeze
  in { insert_after: klass, stage: stage }
    old.flat_map { |existing| existing == klass ? [existing, stage] : [existing] }.freeze
  in { insert_before: klass, stage: stage }
    old.flat_map { |existing| existing == klass ? [stage, existing] : [existing] }.freeze
  in { remove: klass }
    old.reject { |stage| stage == klass }.freeze
  else
    raise ArgumentError,
          "StageList.merge: unsupported operation #{operation.inspect}. " \
          'Expected an Array or one of: { replace:, with: }, { insert_after:, stage: }, ' \
          '{ insert_before:, stage: }, { remove: }'
  end
end