Sha256: a32fbcfc37c0673e1284e624413e37fd1ace3960556dc0be9a8c839cd15d10b8

Contents?: true

Size: 981 Bytes

Versions: 1

Compression:

Stored size: 981 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 class

class Book
  include Volute

  attr_accessor :stock
  attr_accessor :discontinued

  attr_reader :state

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

#
# a volute triggered for any 'set' operation on an attribute of book

volute Book do

  # object.volute_do_set(:state, x)
  #   is equivalent to
  # object.instance_variable_set(:@state, x)

  if object.stock <= 0
    object.volute_do_set(
      :state, object.discontinued ? :discontinued : :out_of_stock)
  else
    object.volute_do_set(
      :state, :in_stock)
  end
end

#
# trying

emma = Book.new(10)

emma.stock = 2
p emma.state # => :in_stock

emma.stock = 0
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.0 examples/state_machine.rb