Sha256: 8e7f7d5a8a6ab1620e057fd13ebf5930e729156c1700b3fbf3d925df0b5e5049

Contents?: true

Size: 1010 Bytes

Versions: 1

Compression:

Stored size: 1010 Bytes

Contents

# license is MIT
#
# a state-machine-ish example, inspired by the example at
# http://github.com/qoobaa/transitions

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

require 'volute'

#
# our classes

module Bookshop

  class Book
    include Volute

    attr_accessor :stock
    attr_accessor :discontinued
    attr_accessor :state

    def initialize (stock)
      @stock = stock
      @discontinued = false
      @state = :in_stock
    end
  end
end

#
# volutes

volute Bookshop do

  volute :stock, :discontinued do

    # anything in the module Bookshop that has an attribute :stock
    # or :discontinued

    volute :any => [ 0, true ] do
      object.state = object.discontinued ? :discontinued : :out_of_stock
      over
    end
    volute do
      object.state = :in_stock
    end
  end
end

#
# trying

emma = Bookshop::Book.new(10)

emma.stock -= 8
p emma.state # => :in_stock

emma.stock -= 2
p emma.state # => :out_of_stock

emma.discontinued = true
p emma.state # => :discontinued

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
volute-0.1.1 examples/state_machine_2.rb