Sha256: 3e9e71377fee4c9adebfd124088e8d250af2676e3cd1e79030b0276c5cafcf20

Contents?: true

Size: 1.21 KB

Versions: 8

Compression:

Stored size: 1.21 KB

Contents

$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'end_state'

class Easy < EndState::Guard
  def will_allow?
    true
  end
end

class NoOp < EndState::Finalizer
  def call
    true
  end

  def rollback
    true
  end
end

class CustomAction < EndState::Action
  def call
    super
  end
end

class Machine < EndState::StateMachine
  transition a: :b do |t|
    t.guard Easy, important_param: 'FOO!'
    t.persistence_on
  end

  transition b: :c do |t|
    t.custom_action CustomAction
    t.persistence_on
  end

  transition [:b, :c] => :a do |t|
    t.finalizer NoOp, not_very_important_param: 'Ignore me'
    t.persistence_on
  end
end

class StatefulObject
  attr_accessor :state

  def initialize(state)
    @state = state
  end

  def save
    puts "Saved with state: #{state}"
    true
  end
end

object = StatefulObject.new(:a)
machine = Machine.new(object)

puts "The machine's class is: #{machine.class.name}"
puts "The machine's object class is: #{machine.object.class.name}"
puts

%i( b c a c).each do |state|
  puts "Attempting to move to #{state}"
  machine.transition state
  puts "State: #{machine.state}"
  predicate = "#{state}?".to_sym
  puts "#{state}?: #{machine.send(predicate)}"
  puts
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
end_state-0.4.0 examples/example1.rb
end_state-0.3.2 examples/example1.rb
end_state-0.3.1 examples/example1.rb
end_state-0.3.0 examples/example1.rb
end_state-0.2.0 examples/example1.rb
end_state-0.1.0 examples/example1.rb
end_state-0.0.2 examples/example1.rb
end_state-0.0.1 examples/example1.rb