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.



6
7
8
9
10
11
# File 'lib/state_machina/machine.rb', line 6

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



51
52
53
# File 'lib/state_machina/machine.rb', line 51

def current_state
  states.find_by_name(current_state_name)
end

#current_state_nameObject



47
48
49
# File 'lib/state_machina/machine.rb', line 47

def current_state_name
  model.public_send(column_name)
end

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

Yields:



19
20
21
22
23
# File 'lib/state_machina/machine.rb', line 19

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



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

def events(possible: false, permitted: false, guard_context: model)
  events = StateMachina::Registry.find_events(model_name, name).values
  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



13
14
15
16
17
# File 'lib/state_machina/machine.rb', line 13

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

  StateMachina::Registry.register_state(state)
end

#statesObject



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

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

  StateMachina::StatesCollection.new(states)
end

#update_state(new_state) ⇒ Object



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

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