Sha256: d3553c987d49931f494e22f350c08bffa6a077af8c57e2e799f9658d71152ddc

Contents?: true

Size: 999 Bytes

Versions: 20

Compression:

Stored size: 999 Bytes

Contents

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'finite_machine'

class Account
  attr_accessor :number

  def verify(account_number, pin)
    return account_number == 123456 && pin == 666
  end
end

account = Account.new

atm = FiniteMachine.define do
  initial :unauthorized

  target account

  events {
    event :authorize, :unauthorized => :authorized, if: -> (account, account_number, pin) {
      account.verify(account_number, pin)
    }
    event :deauthorize, :authorized => :unauthorized
  }

  callbacks {
    on_exit :unauthorized do |event, account_number, pin|
      # if verify(account_number, pin)
        self.number = account_number
#       else
#         puts "Invalid Account and/or PIN"
#         FiniteMachine::CANCELLED
#       end
    end
  }
end

atm.authorize(111222, 666)
puts "authorized: #{atm.authorized?}"
puts "Number: #{account.number}"

atm.authorize(123456, 666)
puts "authorized: #{atm.authorized?}"
puts "Number: #{account.number}"

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
finite_machine-0.12.1 examples/atm.rb
finite_machine-0.12.0 examples/atm.rb
finite_machine-0.11.3 examples/atm.rb
finite_machine-0.11.2 examples/atm.rb
finite_machine-0.11.1 examples/atm.rb
finite_machine-0.11.0 examples/atm.rb
finite_machine-0.10.2 examples/atm.rb
finite_machine-0.10.1 examples/atm.rb
finite_machine-0.10.0 examples/atm.rb
finite_machine-0.9.2 examples/atm.rb
finite_machine-0.9.1 examples/atm.rb
finite_machine-0.9.0 examples/atm.rb
finite_machine-0.8.1 examples/atm.rb
finite_machine-0.8.0 examples/atm.rb
finite_machine-0.7.1 examples/atm.rb
finite_machine-0.7.0 examples/atm.rb
finite_machine-0.6.1 examples/atm.rb
finite_machine-0.6.0 examples/atm.rb
finite_machine-0.5.0 examples/atm.rb
finite_machine-0.4.0 examples/atm.rb