Class: StateMachina::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/state_machina/machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_name, name, column_name: :state, metadata: {}) ⇒ Machine

Returns a new instance of Machine.



8
9
10
11
12
13
# File 'lib/state_machina/machine.rb', line 8

def initialize(model_name, name, column_name: :state, metadata: {})
  @model_name = model_name
  @name = name.to_s
  @column_name = column_name
  @metadata = 
end

Instance Attribute Details

#column_nameObject (readonly)

Returns the value of attribute column_name.



3
4
5
# File 'lib/state_machina/machine.rb', line 3

def column_name
  @column_name
end

#metadataObject (readonly)

Returns the value of attribute metadata.



3
4
5
# File 'lib/state_machina/machine.rb', line 3

def 
  @metadata
end

#modelObject

Returns the value of attribute model.



4
5
6
# File 'lib/state_machina/machine.rb', line 4

def model
  @model
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



3
4
5
# File 'lib/state_machina/machine.rb', line 3

def model_name
  @model_name
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/state_machina/machine.rb', line 3

def name
  @name
end

Instance Method Details

#current_stateObject



53
54
55
# File 'lib/state_machina/machine.rb', line 53

def current_state
  states.find_by_name(current_state_name)
end

#current_state_nameObject



49
50
51
# File 'lib/state_machina/machine.rb', line 49

def current_state_name
  model.public_send(column_name)
end

#event(event_name, metadata: {}) {|StateMachina::Registry.register_event(event)| ... } ⇒ Object

Yields:



21
22
23
24
25
# File 'lib/state_machina/machine.rb', line 21

def event(event_name, metadata: {})
  event = StateMachina::Event.new(model_name, name, event_name, metadata: )

  yield(StateMachina::Registry.register_event(event))
end

#events(possible: false, permitted: false, guard_context: model) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/state_machina/machine.rb', line 36

def events(possible: false, permitted: false, guard_context: model)
  events = StateMachina::Registry.find_events(model_name, name).values.map(&:dup)
  events.each do |event|
    event.machine = self
    event.guard_context = guard_context
  end

  events = select_possible_events(events) if possible || permitted
  events = select_permitted_events(events) if permitted

  StateMachina::EventsCollection.new(events)
end

#state(state_name, metadata: {}) ⇒ Object



15
16
17
18
19
# File 'lib/state_machina/machine.rb', line 15

def state(state_name, metadata: {})
  state = StateMachina::State.new(model_name, name, state_name, metadata: )

  StateMachina::Registry.register_state(state)
end

#statesObject



27
28
29
30
31
32
33
34
# File 'lib/state_machina/machine.rb', line 27

def states
  states = StateMachina::Registry.find_states(model_name, name).values.map(&:dup)
  states.each do |state|
    state.machine = self
  end

  StateMachina::StatesCollection.new(states)
end

#update_state(new_state) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/state_machina/machine.rb', line 57

def update_state(new_state)
  if model.respond_to?(:update)
    model.update(column_name => new_state)
  else
    model.public_send("#{column_name}=", new_state)
  end
end

#update_state!(new_state) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/state_machina/machine.rb', line 65

def update_state!(new_state)
  if model.respond_to?(:update!)
    model.update!(column_name => new_state)
  else
    model.public_send("#{column_name}=", new_state)
  end
end