Class: RailsStateMachine::StateManager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, state_machine, state_attribute) ⇒ StateManager

Returns a new instance of StateManager.



6
7
8
9
10
# File 'lib/rails_state_machine/state_manager.rb', line 6

def initialize(record, state_machine, state_attribute)
  @record = record
  @state_machine = state_machine
  @state_attribute = state_attribute
end

Instance Attribute Details

#next_eventObject

Returns the value of attribute next_event.



3
4
5
# File 'lib/rails_state_machine/state_manager.rb', line 3

def next_event
  @next_event
end

#state_attributeObject (readonly)

Returns the value of attribute state_attribute.



4
5
6
# File 'lib/rails_state_machine/state_manager.rb', line 4

def state_attribute
  @state_attribute
end

#state_before_state_eventObject

Returns the value of attribute state_before_state_event.



3
4
5
# File 'lib/rails_state_machine/state_manager.rb', line 3

def state_before_state_event
  @state_before_state_event
end

Instance Method Details

#revertObject



24
25
26
27
28
29
# File 'lib/rails_state_machine/state_manager.rb', line 24

def revert
  if @next_event
    self.state = @state_before_state_event
    self.state_event = @next_event.name
  end
end

#source_stateObject



31
32
33
34
35
36
37
# File 'lib/rails_state_machine/state_manager.rb', line 31

def source_state
  if @record.new_record?
    state
  else
    state_in_database
  end
end

#stateObject



12
13
14
# File 'lib/rails_state_machine/state_manager.rb', line 12

def state
  @record.public_send(@state_attribute)
end

#state=(value) ⇒ Object



20
21
22
# File 'lib/rails_state_machine/state_manager.rb', line 20

def state=(value)
  @record.public_send(:"#{@state_attribute}=", value)
end

#state_eventObject



39
40
41
# File 'lib/rails_state_machine/state_manager.rb', line 39

def state_event
  @record.public_send(:"#{@state_machine.state_attribute}_event")
end

#state_event=(value) ⇒ Object



43
44
45
# File 'lib/rails_state_machine/state_manager.rb', line 43

def state_event=(value)
  @record.public_send(:"#{@state_machine.state_attribute}_event=", value)
end

#state_in_databaseObject



16
17
18
# File 'lib/rails_state_machine/state_manager.rb', line 16

def state_in_database
  @record.public_send(:"#{@state_attribute}_in_database").to_s
end

#transition_allowed_for?(event_name) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/rails_state_machine/state_manager.rb', line 47

def transition_allowed_for?(event_name)
  !!@state_machine.find_event(event_name)&.allowed_from?(state)
end

#transition_to(event_name) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rails_state_machine/state_manager.rb', line 51

def transition_to(event_name)
  if transition_allowed_for?(event_name)
    self.state_before_state_event = source_state
    event = @state_machine.find_event(event_name)
    self.state = event.future_state_name(state).to_s
    self.state_event = nil
    @next_event = event

    true
  else
    false
  end
end