Module: SSM::Event::ClassMethods

Defined in:
lib/ssm/event.rb

Instance Method Summary collapse

Instance Method Details

#guard(resource, &block) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/ssm/event.rb', line 12

def guard(resource, &block)
  success = @guards&.reduce(true) do |memo, guard|
    resource.send(guard) && memo
  end
  return(yield) if success || @guards.blank?
  resource.reload
  false
end

#guards(*guards) ⇒ Object



8
9
10
# File 'lib/ssm/event.rb', line 8

def guards(*guards)
  @guards = guards
end

#transition!(resource, to:, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ssm/event.rb', line 21

def transition!(resource, to:, &block)
  @from = resource.state
  ActiveRecord::Base.transaction do
    resource.state = to
    resource.save!
    yield if block_given?
  end
rescue ActiveRecord::RecordInvalid
  resource.state = @from
  raise SSM::InvalidTransition
end