Sha256: 4fcaff38e255556a698bed1df8704c196a0f9523035efd65c64f4d3f18c15624

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

# license is MIT

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

require 'volute'

#
# our classes

class Light
  include Volute

  attr_accessor :colour
  attr_accessor :changed_at

  def initialize(colour)
    self.colour = colour
  end

  def green?
    self.colour == :green
  end

  def red!
    self.colour = :red
  end
  def green!
    self.colour = :green
  end

  def seconds
    Time.now - @changed_at
  end
end

class Line
  include Volute

  attr_accessor :name
  attr_accessor :cars
  attr_accessor :light
  attr_accessor :complement

  def initialize(name, colour)
    @name = name
    @cars = 0
    @light = Light.new(colour)
  end

  def one_car
    self.cars = self.cars + 1
  end

  def log(message)
    puts "#{@name} : #{message} (#{@cars} cars)"
  end
end


#
# 'business logic'

volute Light do

  volute :colour do
    object.changed_at = Time.now
  end
end

volute Line do

  volute :cars do

    delta = value - object.complement.cars

    if object.light.green?
      object.vset(:cars, value - 1)
      object.log('cars pass')
    else
      if delta > 4 || object.light.seconds > 5
        object.complement.light.red!
        object.light.green!
        object.log('now green')
        object.vset(:cars, 0)
      else
        object.log('car stops')
      end
    end
  end
end

#
# initialize system

north_south = Line.new('north-south', :green)
east_west = Line.new('east-west', :red)

north_south.complement = east_west
east_west.complement = north_south

#
# play

north_south.one_car
east_west.one_car
east_west.one_car
north_south.one_car
east_west.one_car
east_west.one_car
north_south.one_car
east_west.one_car
east_west.one_car
north_south.one_car

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
volute-0.1.1 examples/traffic.rb