Class: LifecycleVM::Then

Inherits:
Object
  • Object
show all
Defined in:
lib/lifecycle_vm/then.rb

Defined Under Namespace

Classes: AnonymousState, InvalidBranch, InvalidCond, InvalidThen, Simple

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Then

Returns a new instance of Then.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lifecycle_vm/then.rb', line 52

def initialize(options)
  if !(options.kind_of?(Hash) && options.key?(:case) && options.key?(:when))
    raise InvalidThen.new(options)
  end

  @cond = options[:case]
  @branches = options[:when].transform_values do |value|
    case value
    when Hash
      if value.key?(:case)
        Then.new(value)
      elsif value.key?(:then)
        AnonymousState.new(value)
      else
        raise InvalidBranch.new(value, options)
      end
    when Symbol
      Simple.new(value)
    else
      raise InvalidBranch.new(value, options)
    end
  end
end

Class Method Details

.new(options) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/lifecycle_vm/then.rb', line 44

def self.new(options)
  if options.kind_of?(Symbol)
    return Simple.new(options)
  else
    super
  end
end

Instance Method Details

#call(vm) ⇒ Object

Raises:



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/lifecycle_vm/then.rb', line 110

def call(vm)
  state_name = vm.current_state.name
  conditional_name = @cond.name
  logger = vm.logger

  logger&.debug(:conditional_check, state: state_name, ctx: vm, conditional: conditional_name)
  value = @cond.call(vm.memory)
  logger&.debug(:conditional_result, state: state_name, ctx: vm, conditional: conditional_name, result: value)

  branch = @branches[value]
  raise InvalidCond.new(value, @cond, @branches, vm) unless branch.respond_to?(:call)

  branch.call(vm)
end