Class: RailsStateMachine::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_state_machine/event.rb

Defined Under Namespace

Classes: Transition

Constant Summary collapse

UndefinedStateError =
Class.new(StandardError)
TransitionNotFoundError =
Class.new(StandardError)
ExistingTransitionError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, state_machine) ⇒ Event

Returns a new instance of Event.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rails_state_machine/event.rb', line 11

def initialize(name, state_machine)
  @name = name
  @state_machine = state_machine

  @before_validation = []
  @before_save = []
  @after_save = []
  @after_commit = []

  @transitions_by_state_name = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/rails_state_machine/event.rb', line 9

def name
  @name
end

Instance Method Details

#allowed_from?(state_name) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rails_state_machine/event.rb', line 63

def allowed_from?(state_name)
  @transitions_by_state_name.key?(state_name&.to_sym)
end

#configure(&block) ⇒ Object



23
24
25
# File 'lib/rails_state_machine/event.rb', line 23

def configure(&block)
  instance_eval(&block)
end

#find_transition_from(state_name) ⇒ Object



59
60
61
# File 'lib/rails_state_machine/event.rb', line 59

def find_transition_from(state_name)
  @transitions_by_state_name[state_name&.to_sym] || raise(TransitionNotFoundError, "#{name} does not transition from #{state_name}; defined are #{transitions}")
end

#future_state_name(state_name) ⇒ Object



67
68
69
# File 'lib/rails_state_machine/event.rb', line 67

def future_state_name(state_name)
  find_transition_from(state_name).to
end

#run_after_commit(record) ⇒ Object



53
54
55
56
57
# File 'lib/rails_state_machine/event.rb', line 53

def run_after_commit(record)
  @after_commit.each do |block|
    record.instance_eval(&block)
  end
end

#run_after_save(record) ⇒ Object



47
48
49
50
51
# File 'lib/rails_state_machine/event.rb', line 47

def run_after_save(record)
  @after_save.each do |block|
    record.instance_eval(&block)
  end
end

#run_before_save(record) ⇒ Object



41
42
43
44
45
# File 'lib/rails_state_machine/event.rb', line 41

def run_before_save(record)
  @before_save.each do |block|
    record.instance_eval(&block)
  end
end

#run_before_validation(record) ⇒ Object



35
36
37
38
39
# File 'lib/rails_state_machine/event.rb', line 35

def run_before_validation(record)
  @before_validation.each do |block|
    record.instance_eval(&block)
  end
end

#transitions(**options) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/rails_state_machine/event.rb', line 27

def transitions(**options)
  if options.present?
    add_transitions(**options)
  else
    @transitions_by_state_name.values
  end
end