Class: StateMachine::NotificationTransition

Inherits:
Transition
  • Object
show all
Defined in:
lib/motion-state-machine/transition.rb

Overview

Triggered on a specified NSNotification name. Created when supplying an :on_notification option in the transition definition.

On entering the source state, the transition registers itself as NSNotification observer on the default NSNotificationCenter. It deregisters when the state is exited.

Examples:

state_machine.when :awake do |state|
  state.transition_to :sleeping,
    on_notificaiton: UIApplicationDidEnterBackgroundNotification
end

Instance Attribute Summary

Attributes inherited from Transition

#destination_state, #event_trigger_value, #options, #source_state, #state_machine

Instance Method Summary collapse

Methods inherited from Transition

#allowed?, #inspect, make, types

Constructor Details

#initialize(options) ⇒ NotificationTransition

Returns a new instance of NotificationTransition.



368
369
370
# File 'lib/motion-state-machine/transition.rb', line 368

def initialize(options)
  super options
end

Instance Method Details

#armObject



376
377
378
379
380
381
382
# File 'lib/motion-state-machine/transition.rb', line 376

def arm
  NSNotificationCenter.defaultCenter.addObserver self,
    selector: :handle_in_initial_queue,
    name:event_trigger_value,
    object:nil
  @state_machine.log "Registered notification #{event_trigger_value}"
end

#event_descriptionObject



372
373
374
# File 'lib/motion-state-machine/transition.rb', line 372

def event_description
  "after getting a #{event_trigger_value}"
end

#unarmObject



384
385
386
387
# File 'lib/motion-state-machine/transition.rb', line 384

def unarm
  NSNotificationCenter.defaultCenter.removeObserver self
  @state_machine.log "Removed as observer"
end