From bfe66a20e427d45c0fc461b88b6771fe0088da9c Mon Sep 17 00:00:00 2001 From: pjht Date: Sun, 8 Jul 2018 17:19:58 -0500 Subject: [PATCH] Start work on Port class --- port.rb | 83 +++++++++++++++++++++++++++++++++++++++++++++++ spec/port_spec.rb | 39 ++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 port.rb create mode 100644 spec/port_spec.rb diff --git a/port.rb b/port.rb new file mode 100644 index 0000000..cc533f4 --- /dev/null +++ b/port.rb @@ -0,0 +1,83 @@ +## Represents an IO port of a device + +class Port + include Comparable + + # @!attribute [r] val + # @return [Integer] the current value of the port + attr_reader :val + + # Returns a new instance of Port + # @param name [String] The string name of the port + def initialize(name="") + @connected=[] + @propagating=false + @val=0 + @strname=name + end + + # Sets the port's value + # @param val [Integer] The new value for the port + # @return [void] + def setval(val) + # Prevent infinite loops when the connected port calls back when propagating. + if !@propagating + @val=val + @propagating=true + propagate() + @propagating=false + end + end + + # Connects this port to another port + # @param port [Port] The port to connect to + # @return [void] + def connect(port) + @connected.push port + port.connect_back(self) + end + + # Returns a string representation for debugging + # @return [String] String name of the port, or super if none + def to_s() + return super if @strname=="" + return @strname + end + + # Method for Comparable module to use for comparasions. + # @param obj [Object] Object to compare to + # @return [void] + def <=>(obj) + if obj.class==Port + return @val<=>obj.val + elsif obj.is_a? Integer + return @val<=>obj + end + end + + # Used by connect when setting up bidirectional connection. + # @api private + # @param (see #connect) + # @return [void] + def connect_back(port) + @connected.push port + end + + private + + # Propagates the value to all connected ports + # @return [void] + def propagate() + @connected.each do |port| + port.setval(@val) + end + end + +end + +a=Port.new("a") +b=Port.new("b") +c=Port.new("c") +a.connect(b) +b.connect(c) +a.setval(10) diff --git a/spec/port_spec.rb b/spec/port_spec.rb new file mode 100644 index 0000000..24a96f6 --- /dev/null +++ b/spec/port_spec.rb @@ -0,0 +1,39 @@ +require_relative "../port.rb" + +describe Port do + it "should set the port's value when we call setval" do + a=Port.new + a.setval(1) + expect(a).to eq 1 + end + it "should propagate values when we call setval" do + a=Port.new + b=Port.new + c=Port.new + d=Port.new + + #connect A-B and C-D + a.connect(b) + c.connect(d) + + a.setval(1) + c.setval(2) + expect(a).to eq 1 + expect(b).to eq 1 + expect(c).to eq 2 + expect(d).to eq 2 + + #connect them all together + b.connect(c) + a.setval(3) + expect(a).to eq 3 + expect(b).to eq 3 + expect(c).to eq 3 + expect(d).to eq 3 + d.setval(4) + expect(a).to eq 4 + expect(b).to eq 4 + expect(c).to eq 4 + expect(d).to eq 4 + end +end