circuits 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NzMwZGNkMDQ1ZmY2ZTVlMjhjZjA1ODUyZTFmNDc1OTMyZTA2YmM1Mw==
4
+ YmI1YWI5MTBmZDIyNjQ3MjcxMjk4NDVmNWVjYWNjYjMyNjdmNjAzYQ==
5
5
  data.tar.gz: !binary |-
6
- M2ZjMmNmYzM5OGExZjAxMWU0MjI3NzRlZTYyY2QzYWVjNjI1MDc0Zg==
6
+ ZTBkMDBiZTA5YTRkOWMyYzRmMDJiNWYxNzc1ZTEwNjgxMGYyYTdmYQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YWJkYmY2NTE1YWYzMWU0OTk2NjIzZDNkYTY1MTE3NDEzOTkzZDg5MThlMzZh
10
- YjhiNmE5NjRmYTRhZDNmNTE4Yjc0ZDllZTY3OGIxM2Q4MjMyZmM1YWU3ZjM1
11
- N2FlNTg1YWNlNWQ5NTFjZjEwOTlhOTdjNzRmZWFkYWIwZWZlZDU=
9
+ NjA4MzkxY2Q5ODc2Mzk5YWM1YjUxYTU2OGFmYWQ4NmM4OTJhMzE4ODdjMzAz
10
+ YzBhY2YyNDhkMTQ4ZWQzODgyNmM5Njk0MGQ1ZGM2NTZhZmQ3NjY2ZmQ5Nzc4
11
+ NTc4YTcxZTgzYTdhOGI2OTFlMWRiNTI0NGY3OWE0YTcwNjhhZTQ=
12
12
  data.tar.gz: !binary |-
13
- OWNmNzVkMTQyMWQ3MWJjNDFlNjJkZGZmYjZjYzQ1OGU5NGY5Y2JjNGM2ODA4
14
- MTRjOWM5NjI3MWQ3OWJmZWRmZTBkMGUxZjI0YjRmZDg1NGE2M2I4YTE3ZjZh
15
- YjdlYTRkMDI2OGNhMmVkZWEzOTc1NzU2NTJlZDUxNjBlODc1MGY=
13
+ NGI2ZDBiYTBkNDlhNTRiNjJkYmNlNjg5OWNkMjk2YWRhMTlmMzVmNmFkNjFm
14
+ NzFiZDk4ZmMxZTFlYjc5NjI5NTRmMmIwYzRiNDQ3YjlkMDc3MDFmOTZlMmIx
15
+ ZGI4NTdiZmJkOGEzNTM5ZTVjNGZjMDRjNmI1ZmYzOGZkNzA1ZTM=
@@ -13,12 +13,9 @@ module Circuits
13
13
 
14
14
  private
15
15
 
16
- def input_count
17
- 2
18
- end
19
-
20
- def output_count
21
- 1
16
+ def set_defaults
17
+ @input_count = 2
18
+ @output_count = 1
22
19
  end
23
20
  end
24
21
  end
@@ -13,12 +13,9 @@ module Circuits
13
13
 
14
14
  private
15
15
 
16
- def input_count
17
- 2
18
- end
19
-
20
- def output_count
21
- 1
16
+ def set_defaults
17
+ @input_count = 2
18
+ @output_count = 1
22
19
  end
23
20
  end
24
21
  end
@@ -13,12 +13,9 @@ module Circuits
13
13
 
14
14
  private
15
15
 
16
- def input_count
17
- 2
18
- end
19
-
20
- def output_count
21
- 1
16
+ def set_defaults
17
+ @input_count = 2
18
+ @output_count = 1
22
19
  end
23
20
  end
24
21
  end
@@ -13,13 +13,10 @@ module Circuits
13
13
 
14
14
  private
15
15
 
16
- def input_count
17
- 1
16
+ def set_defaults
17
+ @input_count = 1
18
+ @output_count = 1
18
19
  end
19
-
20
- def output_count
21
- 1
22
- end
23
20
  end
24
21
  end
25
22
  end
@@ -13,12 +13,9 @@ module Circuits
13
13
 
14
14
  private
15
15
 
16
- def input_count
17
- 2
18
- end
19
-
20
- def output_count
21
- 1
16
+ def set_defaults
17
+ @input_count = 2
18
+ @output_count = 1
22
19
  end
23
20
  end
24
21
  end
@@ -12,28 +12,33 @@ module Circuits
12
12
  sub_components.each(&:tick)
13
13
  sub_components.each(&:tock)
14
14
  end
15
- outputs[0].set nor_1.outputs[0].get
16
- outputs[1].set nor_2.outputs[0].get
15
+ self[:q].set nand_1[:out].get
16
+ self[:not_q].set nand_2[:out].get
17
17
  end
18
18
 
19
19
  private
20
20
 
21
- attr_reader :nor_1, :nor_2, :sub_components
21
+ attr_reader :nand_1, :nand_2, :sub_components
22
22
 
23
- def input_count
24
- 2
25
- end
26
-
27
- def output_count
28
- 2
23
+ def set_defaults
24
+ @input_count = 2
25
+ @output_count = 2
26
+ @port_mappings = {
27
+ not_s: { type: :input, number: 0 },
28
+ not_r: { type: :input, number: 1 },
29
+ q: { type: :output, number: 0 },
30
+ not_q: { type: :output, number: 1 }
31
+ }
29
32
  end
30
33
 
31
34
  def setup
32
- @nor_1 = Nand.new(inputs: [inputs[0]])
33
- @nor_2 = Nand.new(inputs: [inputs[1]])
34
- @sub_components = [@nor_1, @nor_2]
35
- nor_1.inputs << nor_2.outputs[0]
36
- nor_2.inputs << nor_1.outputs[0]
35
+ @nand_1 = Nand.new
36
+ @nand_2 = Nand.new
37
+ @sub_components = [@nand_1, @nand_2]
38
+ nand_1[:a] = self[:not_s]
39
+ nand_2[:a] = self[:not_r]
40
+ nand_1[:b] = nand_2[:out]
41
+ nand_2[:b] = nand_1[:out]
37
42
  end
38
43
  end
39
44
  end
@@ -12,28 +12,33 @@ module Circuits
12
12
  sub_components.each(&:tick)
13
13
  sub_components.each(&:tock)
14
14
  end
15
- outputs[0].set nor_1.outputs[0].get
16
- outputs[1].set nor_2.outputs[0].get
15
+ self[:q].set nor_1[:out].get
16
+ self[:not_q].set nor_2[:out].get
17
17
  end
18
18
 
19
19
  private
20
20
 
21
21
  attr_reader :nor_1, :nor_2, :sub_components
22
22
 
23
- def input_count
24
- 2
25
- end
26
-
27
- def output_count
28
- 2
23
+ def set_defaults
24
+ @input_count = 2
25
+ @output_count = 2
26
+ @port_mappings = {
27
+ r: { type: :input, number: 0 },
28
+ s: { type: :input, number: 1 },
29
+ q: { type: :output, number: 0 },
30
+ not_q: { type: :output, number: 1 }
31
+ }
29
32
  end
30
33
 
31
34
  def setup
32
- @nor_1 = Nor.new(inputs: [inputs[0]])
33
- @nor_2 = Nor.new(inputs: [inputs[1]])
35
+ @nor_1 = Nor.new
36
+ @nor_2 = Nor.new
34
37
  @sub_components = [@nor_1, @nor_2]
35
- nor_1.inputs << nor_2.outputs[0]
36
- nor_2.inputs << nor_1.outputs[0]
38
+ nor_1[:a] = self[:r]
39
+ nor_2[:a] = self[:s]
40
+ nor_1[:b] = nor_2[:out]
41
+ nor_2[:b] = nor_1[:out]
37
42
  end
38
43
  end
39
44
  end
@@ -13,12 +13,9 @@ module Circuits
13
13
 
14
14
  private
15
15
 
16
- def input_count
17
- 2
18
- end
19
-
20
- def output_count
21
- 1
16
+ def set_defaults
17
+ @input_count = 2
18
+ @output_count = 1
22
19
  end
23
20
  end
24
21
  end
@@ -13,12 +13,9 @@ module Circuits
13
13
 
14
14
  private
15
15
 
16
- def input_count
17
- 2
18
- end
19
-
20
- def output_count
21
- 1
16
+ def set_defaults
17
+ @input_count = 2
18
+ @output_count = 1
22
19
  end
23
20
  end
24
21
  end
@@ -11,11 +11,10 @@ module Circuits
11
11
  # @option opts [Array<Input>, FixNum] :inputs The array of inputs to use, or
12
12
  # the number of inputs to create
13
13
  def initialize(opts = {})
14
- @inputs = opts[:inputs] if opts[:inputs].is_a? Array
15
- @inputs = create_inputs opts[:inputs] if opts[:inputs].is_a? Integer
16
- @inputs = create_inputs input_count if opts[:inputs].nil?
17
-
18
- @outputs = output_count.times.collect { Circuits::Terminal::Output.new }
14
+ set_defaults
15
+ create_inputs opts
16
+ create_outputs opts
17
+ create_port_mappings
19
18
  setup
20
19
  end
21
20
 
@@ -29,6 +28,33 @@ module Circuits
29
28
  outputs.each(&:tock)
30
29
  end
31
30
 
31
+ # Gets the teminal assigned to the port
32
+ # @param port [Symbol] The symbol that represents the terminal
33
+ # @return [Input, Output] The terminal
34
+ def [](port)
35
+ p = @port_mappings[port]
36
+ case p[:type]
37
+ when :input
38
+ @inputs[p[:number]]
39
+ when :output
40
+ @outputs[p[:number]]
41
+ end
42
+ end
43
+
44
+ # Assigns to an input or output
45
+ # @param port [Symbol] The symbol that represents the terminal
46
+ # @param terminal [Input, Output] The terminal to assign
47
+ # @return [Input, Output] The terminal that was passed in
48
+ def []=(port, terminal)
49
+ p = @port_mappings[port]
50
+ case p[:type]
51
+ when :input
52
+ @inputs[p[:number]] = terminal
53
+ when :output
54
+ @outputs[p[:number]] = terminal
55
+ end
56
+ end
57
+
32
58
  # the inputs of this component
33
59
  attr_reader :inputs
34
60
 
@@ -37,24 +63,57 @@ module Circuits
37
63
 
38
64
  private
39
65
 
40
- def input_count
41
- fail NotImplementedError
42
- end
43
-
44
- def output_count
45
- fail NotImplementedError
46
- end
66
+ def create_inputs(opts)
67
+ if opts[:inputs].class == Array
68
+ @inputs = opts[:inputs]
69
+ @input_count = @inputs.length
70
+ elsif opts[:inputs].class == Fixnum
71
+ @input_count = opts[:inputs]
72
+ end
73
+ @inputs ||= @input_count.times.collect { Circuits::Terminal::Input.new }
74
+ end
47
75
 
48
- # Creates an array of N inputs, where N is equal to or greater than the
49
- # default number of inputs for this component
50
- # @param n [FixNum] The number of inputs to create
51
- # @return [Array<Input>] An array of inputs
52
- def create_inputs(n)
53
- if n < input_count
54
- fail ArgumentError, "Invalid number of inputs, #{self.class} requires at least #{input_count} inputs"
55
- end
56
- n.times.collect { Circuits::Terminal::Input.new }
57
- end
76
+ def create_outputs(opts)
77
+ if opts[:outputs].class == Array
78
+ @outputs = opts[:outputs]
79
+ @output_count = @outputs.length
80
+ elsif opts[:outputs].class == Fixnum
81
+ @output_count = opts[:outputs]
82
+ end
83
+ @outputs ||= @output_count.times.collect do
84
+ Circuits::Terminal::Output.new
85
+ end
86
+ end
87
+
88
+ def create_port_mappings
89
+ return @port_mappings unless @port_mappings.nil?
90
+ @port_mappings = {}
91
+ input_mappings.each { |x| @port_mappings.merge!(x) }
92
+ output_mappings.each { |x| @port_mappings.merge!(x) }
93
+ @port_mappings
94
+ end
95
+
96
+ def input_mappings
97
+ return [{ in: { type: :input, number: 0 } }] if @input_count == 1
98
+ @input_count.times.collect do |i|
99
+ { num_to_port(i) => { type: :input, number: i } }
100
+ end
101
+ end
102
+
103
+ def output_mappings
104
+ return[{ out: { type: :output, number: 0 } }] if @output_count == 1
105
+ @output_count.times.collect do |i|
106
+ { num_to_port(i + @input_count) => { type: :output, number: i } }
107
+ end
108
+ end
109
+
110
+ def num_to_port(i)
111
+ (i + 'a'.ord).chr.to_sym
112
+ end
113
+
114
+ def set_defaults
115
+ fail NotImplementedError
116
+ end
58
117
 
59
118
  def setup
60
119
  end
@@ -1,6 +1,5 @@
1
1
  module Circuits
2
2
  # A terminal holds a logical state, it can be attatched to components
3
3
  module Terminal
4
-
5
4
  end
6
5
  end
@@ -1,5 +1,5 @@
1
1
  # Circuits allows you to express logical circuits in code
2
2
  module Circuits
3
3
  # The version of the Circuits gem
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
@@ -7,8 +7,8 @@ describe Circuits::Component::And do
7
7
  subject { Circuits::Component::And.new }
8
8
 
9
9
  before do
10
- subject.inputs[0].set input_1
11
- subject.inputs[1].set input_2
10
+ subject[:a].set input_1
11
+ subject[:b].set input_2
12
12
  end
13
13
 
14
14
  context 'false + false' do
@@ -18,7 +18,7 @@ describe Circuits::Component::And do
18
18
  it '= false' do
19
19
  subject.tick
20
20
  subject.tock
21
- expect(subject.outputs[0].get).to eq(false)
21
+ expect(subject[:out].get).to eq(false)
22
22
  end
23
23
  end
24
24
 
@@ -29,7 +29,7 @@ describe Circuits::Component::And do
29
29
  it '= false' do
30
30
  subject.tick
31
31
  subject.tock
32
- expect(subject.outputs[0].get).to eq(false)
32
+ expect(subject[:out].get).to eq(false)
33
33
  end
34
34
  end
35
35
 
@@ -40,7 +40,7 @@ describe Circuits::Component::And do
40
40
  it '= false' do
41
41
  subject.tick
42
42
  subject.tock
43
- expect(subject.outputs[0].get).to eq(false)
43
+ expect(subject[:out].get).to eq(false)
44
44
  end
45
45
  end
46
46
 
@@ -51,7 +51,7 @@ describe Circuits::Component::And do
51
51
  it '= true' do
52
52
  subject.tick
53
53
  subject.tock
54
- expect(subject.outputs[0].get).to eq(true)
54
+ expect(subject[:out].get).to eq(true)
55
55
  end
56
56
  end
57
57
  end
@@ -70,7 +70,7 @@ describe Circuits::Component::And do
70
70
  it '= true' do
71
71
  subject.tick
72
72
  subject.tock
73
- expect(subject.outputs[0].get).to eq(true)
73
+ expect(subject[:out].get).to eq(true)
74
74
  end
75
75
  end
76
76
 
@@ -80,7 +80,7 @@ describe Circuits::Component::And do
80
80
  it '= false' do
81
81
  subject.tick
82
82
  subject.tock
83
- expect(subject.outputs[0].get).to eq(false)
83
+ expect(subject[:out].get).to eq(false)
84
84
  end
85
85
  end
86
86
 
@@ -96,7 +96,7 @@ describe Circuits::Component::And do
96
96
  it '= false' do
97
97
  subject.tick
98
98
  subject.tock
99
- expect(subject.outputs[0].get).to eq(false)
99
+ expect(subject[:out].get).to eq(false)
100
100
  end
101
101
  end
102
102
  end
@@ -7,8 +7,8 @@ describe Circuits::Component::Nand do
7
7
  subject { Circuits::Component::Nand.new }
8
8
 
9
9
  before do
10
- subject.inputs[0].set input_1
11
- subject.inputs[1].set input_2
10
+ subject[:a].set input_1
11
+ subject[:b].set input_2
12
12
  end
13
13
 
14
14
  context 'false + false' do
@@ -18,7 +18,7 @@ describe Circuits::Component::Nand do
18
18
  it '= true' do
19
19
  subject.tick
20
20
  subject.tock
21
- expect(subject.outputs[0].get).to eq(true)
21
+ expect(subject[:out].get).to eq(true)
22
22
  end
23
23
  end
24
24
 
@@ -29,7 +29,7 @@ describe Circuits::Component::Nand do
29
29
  it '= true' do
30
30
  subject.tick
31
31
  subject.tock
32
- expect(subject.outputs[0].get).to eq(true)
32
+ expect(subject[:out].get).to eq(true)
33
33
  end
34
34
  end
35
35
 
@@ -40,7 +40,7 @@ describe Circuits::Component::Nand do
40
40
  it '= true' do
41
41
  subject.tick
42
42
  subject.tock
43
- expect(subject.outputs[0].get).to eq(true)
43
+ expect(subject[:out].get).to eq(true)
44
44
  end
45
45
  end
46
46
 
@@ -51,7 +51,7 @@ describe Circuits::Component::Nand do
51
51
  it '= false' do
52
52
  subject.tick
53
53
  subject.tock
54
- expect(subject.outputs[0].get).to eq(false)
54
+ expect(subject[:out].get).to eq(false)
55
55
  end
56
56
  end
57
57
  end
@@ -69,7 +69,7 @@ describe Circuits::Component::Nand do
69
69
  it '= false' do
70
70
  subject.tick
71
71
  subject.tock
72
- expect(subject.outputs[0].get).to eq(false)
72
+ expect(subject[:out].get).to eq(false)
73
73
  end
74
74
  end
75
75
 
@@ -79,7 +79,7 @@ describe Circuits::Component::Nand do
79
79
  it '= true' do
80
80
  subject.tick
81
81
  subject.tock
82
- expect(subject.outputs[0].get).to eq(true)
82
+ expect(subject[:out].get).to eq(true)
83
83
  end
84
84
  end
85
85
 
@@ -95,7 +95,7 @@ describe Circuits::Component::Nand do
95
95
  it '= true' do
96
96
  subject.tick
97
97
  subject.tock
98
- expect(subject.outputs[0].get).to eq(true)
98
+ expect(subject[:out].get).to eq(true)
99
99
  end
100
100
  end
101
101
  end
@@ -7,8 +7,8 @@ describe Circuits::Component::Nor do
7
7
  subject { Circuits::Component::Nor.new }
8
8
 
9
9
  before do
10
- subject.inputs[0].set input_1
11
- subject.inputs[1].set input_2
10
+ subject[:a].set input_1
11
+ subject[:b].set input_2
12
12
  end
13
13
 
14
14
  context 'false + false' do
@@ -18,7 +18,7 @@ describe Circuits::Component::Nor do
18
18
  it '= true' do
19
19
  subject.tick
20
20
  subject.tock
21
- expect(subject.outputs[0].get).to eq(true)
21
+ expect(subject[:out].get).to eq(true)
22
22
  end
23
23
  end
24
24
 
@@ -29,7 +29,7 @@ describe Circuits::Component::Nor do
29
29
  it '= false' do
30
30
  subject.tick
31
31
  subject.tock
32
- expect(subject.outputs[0].get).to eq(false)
32
+ expect(subject[:out].get).to eq(false)
33
33
  end
34
34
  end
35
35
 
@@ -40,7 +40,7 @@ describe Circuits::Component::Nor do
40
40
  it '= false' do
41
41
  subject.tick
42
42
  subject.tock
43
- expect(subject.outputs[0].get).to eq(false)
43
+ expect(subject[:out].get).to eq(false)
44
44
  end
45
45
  end
46
46
 
@@ -51,7 +51,7 @@ describe Circuits::Component::Nor do
51
51
  it '= false' do
52
52
  subject.tick
53
53
  subject.tock
54
- expect(subject.outputs[0].get).to eq(false)
54
+ expect(subject[:out].get).to eq(false)
55
55
  end
56
56
  end
57
57
  end
@@ -69,7 +69,7 @@ describe Circuits::Component::Nor do
69
69
  it '= false' do
70
70
  subject.tick
71
71
  subject.tock
72
- expect(subject.outputs[0].get).to eq(false)
72
+ expect(subject[:out].get).to eq(false)
73
73
  end
74
74
  end
75
75
 
@@ -79,7 +79,7 @@ describe Circuits::Component::Nor do
79
79
  it '= true' do
80
80
  subject.tick
81
81
  subject.tock
82
- expect(subject.outputs[0].get).to eq(true)
82
+ expect(subject[:out].get).to eq(true)
83
83
  end
84
84
  end
85
85
 
@@ -95,7 +95,7 @@ describe Circuits::Component::Nor do
95
95
  it '= false' do
96
96
  subject.tick
97
97
  subject.tock
98
- expect(subject.outputs[0].get).to eq(false)
98
+ expect(subject[:out].get).to eq(false)
99
99
  end
100
100
  end
101
101
  end
@@ -5,7 +5,7 @@ describe Circuits::Component::Not do
5
5
  describe '#tick' do
6
6
  subject { Circuits::Component::Not.new }
7
7
 
8
- before { subject.inputs[0].set input }
8
+ before { subject[:in].set input }
9
9
 
10
10
  context '!false' do
11
11
  let(:input) { false }
@@ -13,7 +13,7 @@ describe Circuits::Component::Not do
13
13
  it '= true' do
14
14
  subject.tick
15
15
  subject.tock
16
- expect(subject.outputs[0].get).to eq(true)
16
+ expect(subject[:out].get).to eq(true)
17
17
  end
18
18
  end
19
19
 
@@ -23,7 +23,7 @@ describe Circuits::Component::Not do
23
23
  it '= false' do
24
24
  subject.tick
25
25
  subject.tock
26
- expect(subject.outputs[0].get).to eq(false)
26
+ expect(subject[:out].get).to eq(false)
27
27
  end
28
28
  end
29
29
  end
@@ -7,8 +7,8 @@ describe Circuits::Component::Or do
7
7
  subject { Circuits::Component::Or.new }
8
8
 
9
9
  before do
10
- subject.inputs[0].set input_1
11
- subject.inputs[1].set input_2
10
+ subject[:a].set input_1
11
+ subject[:b].set input_2
12
12
  end
13
13
 
14
14
  context 'false + false' do
@@ -18,7 +18,7 @@ describe Circuits::Component::Or do
18
18
  it '= false' do
19
19
  subject.tick
20
20
  subject.tock
21
- expect(subject.outputs[0].get).to eq(false)
21
+ expect(subject[:out].get).to eq(false)
22
22
  end
23
23
  end
24
24
 
@@ -29,7 +29,7 @@ describe Circuits::Component::Or do
29
29
  it '= true' do
30
30
  subject.tick
31
31
  subject.tock
32
- expect(subject.outputs[0].get).to eq(true)
32
+ expect(subject[:out].get).to eq(true)
33
33
  end
34
34
  end
35
35
 
@@ -40,7 +40,7 @@ describe Circuits::Component::Or do
40
40
  it '= true' do
41
41
  subject.tick
42
42
  subject.tock
43
- expect(subject.outputs[0].get).to eq(true)
43
+ expect(subject[:out].get).to eq(true)
44
44
  end
45
45
  end
46
46
 
@@ -51,7 +51,7 @@ describe Circuits::Component::Or do
51
51
  it '= true' do
52
52
  subject.tick
53
53
  subject.tock
54
- expect(subject.outputs[0].get).to eq(true)
54
+ expect(subject[:out].get).to eq(true)
55
55
  end
56
56
  end
57
57
  end
@@ -69,7 +69,7 @@ describe Circuits::Component::Or do
69
69
  it '= true' do
70
70
  subject.tick
71
71
  subject.tock
72
- expect(subject.outputs[0].get).to eq(true)
72
+ expect(subject[:out].get).to eq(true)
73
73
  end
74
74
  end
75
75
 
@@ -79,7 +79,7 @@ describe Circuits::Component::Or do
79
79
  it '= false' do
80
80
  subject.tick
81
81
  subject.tock
82
- expect(subject.outputs[0].get).to eq(false)
82
+ expect(subject[:out].get).to eq(false)
83
83
  end
84
84
  end
85
85
 
@@ -95,7 +95,7 @@ describe Circuits::Component::Or do
95
95
  it '= true' do
96
96
  subject.tick
97
97
  subject.tock
98
- expect(subject.outputs[0].get).to eq(true)
98
+ expect(subject[:out].get).to eq(true)
99
99
  end
100
100
  end
101
101
  end
@@ -7,61 +7,61 @@ describe Circuits::Component::SrNand do
7
7
 
8
8
  context 'is set' do
9
9
  before do
10
- subject.inputs[0].set false
11
- subject.inputs[1].set true
10
+ subject[:not_s].set false
11
+ subject[:not_r].set true
12
12
  subject.tick
13
13
  subject.tock
14
- subject.inputs[0].set true
14
+ subject[:not_s].set true
15
15
  end
16
16
 
17
17
  it 'is set' do
18
- expect(subject.outputs[0].get).to eq(true)
19
- expect(subject.outputs[1].get).to eq(false)
18
+ expect(subject[:q].get).to eq(true)
19
+ expect(subject[:not_q].get).to eq(false)
20
20
  end
21
21
 
22
22
  it 'is stable' do
23
23
  subject.tick
24
24
  subject.tock
25
- expect(subject.outputs[0].get).to eq(true)
26
- expect(subject.outputs[1].get).to eq(false)
25
+ expect(subject[:q].get).to eq(true)
26
+ expect(subject[:not_q].get).to eq(false)
27
27
  end
28
28
 
29
29
  it 'can be reset' do
30
- subject.inputs[1].set false
30
+ subject[:not_r].set false
31
31
  subject.tick
32
32
  subject.tock
33
- expect(subject.outputs[0].get).to eq(false)
34
- expect(subject.outputs[1].get).to eq(true)
33
+ expect(subject[:q].get).to eq(false)
34
+ expect(subject[:not_q].get).to eq(true)
35
35
  end
36
36
  end
37
37
 
38
38
  context 'is reset' do
39
39
  before do
40
- subject.inputs[0].set true
41
- subject.inputs[1].set false
40
+ subject[:not_s].set true
41
+ subject[:not_r].set false
42
42
  subject.tick
43
43
  subject.tock
44
- subject.inputs[1].set true
44
+ subject[:not_r].set true
45
45
  end
46
46
 
47
47
  it 'is reset' do
48
- expect(subject.outputs[0].get).to eq(false)
49
- expect(subject.outputs[1].get).to eq(true)
48
+ expect(subject[:q].get).to eq(false)
49
+ expect(subject[:not_q].get).to eq(true)
50
50
  end
51
51
 
52
52
  it 'is stable' do
53
53
  subject.tick
54
54
  subject.tock
55
- expect(subject.outputs[0].get).to eq(false)
56
- expect(subject.outputs[1].get).to eq(true)
55
+ expect(subject[:q].get).to eq(false)
56
+ expect(subject[:not_q].get).to eq(true)
57
57
  end
58
58
 
59
59
  it 'can be set' do
60
- subject.inputs[0].set false
60
+ subject[:not_s].set false
61
61
  subject.tick
62
62
  subject.tock
63
- expect(subject.outputs[0].get).to eq(true)
64
- expect(subject.outputs[1].get).to eq(false)
63
+ expect(subject[:q].get).to eq(true)
64
+ expect(subject[:not_q].get).to eq(false)
65
65
  end
66
66
  end
67
67
  end
@@ -7,61 +7,61 @@ describe Circuits::Component::SrNor do
7
7
 
8
8
  context 'is set' do
9
9
  before do
10
- subject.inputs[0].set false
11
- subject.inputs[1].set true
10
+ subject[:r].set false
11
+ subject[:s].set true
12
12
  subject.tick
13
13
  subject.tock
14
- subject.inputs[1].set false
14
+ subject[:s].set false
15
15
  end
16
16
 
17
17
  it 'is set' do
18
- expect(subject.outputs[0].get).to eq(true)
19
- expect(subject.outputs[1].get).to eq(false)
18
+ expect(subject[:q].get).to eq(true)
19
+ expect(subject[:not_q].get).to eq(false)
20
20
  end
21
21
 
22
22
  it 'is stable' do
23
23
  subject.tick
24
24
  subject.tock
25
- expect(subject.outputs[0].get).to eq(true)
26
- expect(subject.outputs[1].get).to eq(false)
25
+ expect(subject[:q].get).to eq(true)
26
+ expect(subject[:not_q].get).to eq(false)
27
27
  end
28
28
 
29
29
  it 'can be reset' do
30
- subject.inputs[0].set true
30
+ subject[:r].set true
31
31
  subject.tick
32
32
  subject.tock
33
- expect(subject.outputs[0].get).to eq(false)
34
- expect(subject.outputs[1].get).to eq(true)
33
+ expect(subject[:q].get).to eq(false)
34
+ expect(subject[:not_q].get).to eq(true)
35
35
  end
36
36
  end
37
37
 
38
38
  context 'is reset' do
39
39
  before do
40
- subject.inputs[0].set true
41
- subject.inputs[1].set false
40
+ subject[:r].set true
41
+ subject[:s].set false
42
42
  subject.tick
43
43
  subject.tock
44
- subject.inputs[0].set false
44
+ subject[:r].set false
45
45
  end
46
46
 
47
47
  it 'is reset' do
48
- expect(subject.outputs[0].get).to eq(false)
49
- expect(subject.outputs[1].get).to eq(true)
48
+ expect(subject[:q].get).to eq(false)
49
+ expect(subject[:not_q].get).to eq(true)
50
50
  end
51
51
 
52
52
  it 'is stable' do
53
53
  subject.tick
54
54
  subject.tock
55
- expect(subject.outputs[0].get).to eq(false)
56
- expect(subject.outputs[1].get).to eq(true)
55
+ expect(subject[:q].get).to eq(false)
56
+ expect(subject[:not_q].get).to eq(true)
57
57
  end
58
58
 
59
59
  it 'can be set' do
60
- subject.inputs[1].set true
60
+ subject[:s].set true
61
61
  subject.tick
62
62
  subject.tock
63
- expect(subject.outputs[0].get).to eq(true)
64
- expect(subject.outputs[1].get).to eq(false)
63
+ expect(subject[:q].get).to eq(true)
64
+ expect(subject[:not_q].get).to eq(false)
65
65
  end
66
66
  end
67
67
  end
@@ -7,8 +7,8 @@ describe Circuits::Component::Xnor do
7
7
  subject { Circuits::Component::Xnor.new }
8
8
 
9
9
  before do
10
- subject.inputs[0].set input_1
11
- subject.inputs[1].set input_2
10
+ subject[:a].set input_1
11
+ subject[:b].set input_2
12
12
  end
13
13
 
14
14
  context 'false + false' do
@@ -18,7 +18,7 @@ describe Circuits::Component::Xnor do
18
18
  it '= true' do
19
19
  subject.tick
20
20
  subject.tock
21
- expect(subject.outputs[0].get).to eq(true)
21
+ expect(subject[:out].get).to eq(true)
22
22
  end
23
23
  end
24
24
 
@@ -29,7 +29,7 @@ describe Circuits::Component::Xnor do
29
29
  it '= false' do
30
30
  subject.tick
31
31
  subject.tock
32
- expect(subject.outputs[0].get).to eq(false)
32
+ expect(subject[:out].get).to eq(false)
33
33
  end
34
34
  end
35
35
 
@@ -40,7 +40,7 @@ describe Circuits::Component::Xnor do
40
40
  it '= false' do
41
41
  subject.tick
42
42
  subject.tock
43
- expect(subject.outputs[0].get).to eq(false)
43
+ expect(subject[:out].get).to eq(false)
44
44
  end
45
45
  end
46
46
 
@@ -51,7 +51,7 @@ describe Circuits::Component::Xnor do
51
51
  it '= true' do
52
52
  subject.tick
53
53
  subject.tock
54
- expect(subject.outputs[0].get).to eq(true)
54
+ expect(subject[:out].get).to eq(true)
55
55
  end
56
56
  end
57
57
  end
@@ -70,7 +70,7 @@ describe Circuits::Component::Xnor do
70
70
  it '= true' do
71
71
  subject.tick
72
72
  subject.tock
73
- expect(subject.outputs[0].get).to eq(true)
73
+ expect(subject[:out].get).to eq(true)
74
74
  end
75
75
  end
76
76
 
@@ -80,7 +80,7 @@ describe Circuits::Component::Xnor do
80
80
  it '= true' do
81
81
  subject.tick
82
82
  subject.tock
83
- expect(subject.outputs[0].get).to eq(true)
83
+ expect(subject[:out].get).to eq(true)
84
84
  end
85
85
  end
86
86
 
@@ -96,7 +96,7 @@ describe Circuits::Component::Xnor do
96
96
  it '= false' do
97
97
  subject.tick
98
98
  subject.tock
99
- expect(subject.outputs[0].get).to eq(false)
99
+ expect(subject[:out].get).to eq(false)
100
100
  end
101
101
  end
102
102
  end
@@ -119,7 +119,7 @@ describe Circuits::Component::Xnor do
119
119
  it '= false' do
120
120
  subject.tick
121
121
  subject.tock
122
- expect(subject.outputs[0].get).to eq(false)
122
+ expect(subject[:out].get).to eq(false)
123
123
  end
124
124
  end
125
125
 
@@ -129,7 +129,7 @@ describe Circuits::Component::Xnor do
129
129
  it '= true' do
130
130
  subject.tick
131
131
  subject.tock
132
- expect(subject.outputs[0].get).to eq(true)
132
+ expect(subject[:out].get).to eq(true)
133
133
  end
134
134
  end
135
135
 
@@ -145,7 +145,7 @@ describe Circuits::Component::Xnor do
145
145
  it '= true' do
146
146
  subject.tick
147
147
  subject.tock
148
- expect(subject.outputs[0].get).to eq(true)
148
+ expect(subject[:out].get).to eq(true)
149
149
  end
150
150
  end
151
151
  end
@@ -163,7 +163,7 @@ describe Circuits::Component::Xnor do
163
163
  it '= false' do
164
164
  subject.tick
165
165
  subject.tock
166
- expect(subject.outputs[0].get).to eq(false)
166
+ expect(subject[:out].get).to eq(false)
167
167
  end
168
168
  end
169
169
  end
@@ -7,8 +7,8 @@ describe Circuits::Component::Xor do
7
7
  subject { Circuits::Component::Xor.new }
8
8
 
9
9
  before do
10
- subject.inputs[0].set input_1
11
- subject.inputs[1].set input_2
10
+ subject[:a].set input_1
11
+ subject[:b].set input_2
12
12
  end
13
13
 
14
14
  context 'false + false' do
@@ -18,7 +18,7 @@ describe Circuits::Component::Xor do
18
18
  it '= false' do
19
19
  subject.tick
20
20
  subject.tock
21
- expect(subject.outputs[0].get).to eq(false)
21
+ expect(subject[:out].get).to eq(false)
22
22
  end
23
23
  end
24
24
 
@@ -29,7 +29,7 @@ describe Circuits::Component::Xor do
29
29
  it '= true' do
30
30
  subject.tick
31
31
  subject.tock
32
- expect(subject.outputs[0].get).to eq(true)
32
+ expect(subject[:out].get).to eq(true)
33
33
  end
34
34
  end
35
35
 
@@ -40,7 +40,7 @@ describe Circuits::Component::Xor do
40
40
  it '= true' do
41
41
  subject.tick
42
42
  subject.tock
43
- expect(subject.outputs[0].get).to eq(true)
43
+ expect(subject[:out].get).to eq(true)
44
44
  end
45
45
  end
46
46
 
@@ -51,7 +51,7 @@ describe Circuits::Component::Xor do
51
51
  it '= false' do
52
52
  subject.tick
53
53
  subject.tock
54
- expect(subject.outputs[0].get).to eq(false)
54
+ expect(subject[:out].get).to eq(false)
55
55
  end
56
56
  end
57
57
  end
@@ -70,7 +70,7 @@ describe Circuits::Component::Xor do
70
70
  it '= false' do
71
71
  subject.tick
72
72
  subject.tock
73
- expect(subject.outputs[0].get).to eq(false)
73
+ expect(subject[:out].get).to eq(false)
74
74
  end
75
75
  end
76
76
 
@@ -80,7 +80,7 @@ describe Circuits::Component::Xor do
80
80
  it '= false' do
81
81
  subject.tick
82
82
  subject.tock
83
- expect(subject.outputs[0].get).to eq(false)
83
+ expect(subject[:out].get).to eq(false)
84
84
  end
85
85
  end
86
86
 
@@ -96,7 +96,7 @@ describe Circuits::Component::Xor do
96
96
  it '= true' do
97
97
  subject.tick
98
98
  subject.tock
99
- expect(subject.outputs[0].get).to eq(true)
99
+ expect(subject[:out].get).to eq(true)
100
100
  end
101
101
  end
102
102
  end
@@ -119,7 +119,7 @@ describe Circuits::Component::Xor do
119
119
  it '= true' do
120
120
  subject.tick
121
121
  subject.tock
122
- expect(subject.outputs[0].get).to eq(true)
122
+ expect(subject[:out].get).to eq(true)
123
123
  end
124
124
  end
125
125
 
@@ -129,7 +129,7 @@ describe Circuits::Component::Xor do
129
129
  it '= false' do
130
130
  subject.tick
131
131
  subject.tock
132
- expect(subject.outputs[0].get).to eq(false)
132
+ expect(subject[:out].get).to eq(false)
133
133
  end
134
134
  end
135
135
 
@@ -145,7 +145,7 @@ describe Circuits::Component::Xor do
145
145
  it '= false' do
146
146
  subject.tick
147
147
  subject.tock
148
- expect(subject.outputs[0].get).to eq(false)
148
+ expect(subject[:out].get).to eq(false)
149
149
  end
150
150
  end
151
151
  end
@@ -163,7 +163,7 @@ describe Circuits::Component::Xor do
163
163
  it '= true' do
164
164
  subject.tick
165
165
  subject.tock
166
- expect(subject.outputs[0].get).to eq(true)
166
+ expect(subject[:out].get).to eq(true)
167
167
  end
168
168
  end
169
169
  end
@@ -2,73 +2,113 @@ require 'spec_helper'
2
2
  require 'circuits/component'
3
3
 
4
4
  # Mock component to include Circuits::Component for function accessability
5
- class MockComponent
5
+ class MockComponent1
6
6
  include Circuits::Component
7
7
 
8
- def initialize(opts = {})
9
- @outputs = opts[:outputs]
10
- end
11
-
12
- def mock_input_count
13
- input_count
14
- end
15
-
16
- def mock_output_count
17
- output_count
8
+ def set_defaults
9
+ @input_count = 1
10
+ @output_count = 1
18
11
  end
12
+ end
19
13
 
20
- def mock_create_inputs(n)
21
- create_inputs(n)
22
- end
14
+ # Mock component to include Circuits::Component for function accessability
15
+ class MockComponent2
16
+ include Circuits::Component
23
17
  end
24
18
 
25
19
  describe Circuits::Component do
26
- let(:outputs) { [double('output')] }
20
+ context 'when using defaults' do
21
+ subject { MockComponent1.new }
27
22
 
28
- subject { MockComponent.new(outputs: outputs) }
23
+ it 'has one input' do
24
+ expect(subject.inputs.count).to eq(1)
25
+ end
29
26
 
30
- describe '#input_count' do
31
- it 'is not implemented' do
32
- expect { subject.mock_input_count }.to raise_error(NotImplementedError)
27
+ it 'has one output' do
28
+ expect(subject.outputs.count).to eq(1)
33
29
  end
34
- end
35
30
 
36
- describe '#output_count' do
37
- it 'is not implemented' do
38
- expect { subject.mock_output_count }.to raise_error(NotImplementedError)
31
+ describe '#[]=' do
32
+ let(:new_input) { double('new_input') }
33
+ let(:new_output) { double('new_output') }
34
+
35
+ before do
36
+ subject[:in] = new_input
37
+ subject[:out] = new_output
38
+ end
39
+
40
+ it 'has the new input available as :in' do
41
+ expect(subject[:in]).to eq(new_input)
42
+ end
43
+
44
+ it 'has the new output available as :out' do
45
+ expect(subject[:out]).to eq(new_output)
46
+ end
39
47
  end
40
48
  end
41
49
 
42
- describe '#tick' do
43
- it 'is not implemented' do
44
- expect { subject.tick }.to raise_error(NotImplementedError)
50
+ context 'when specifying input and output count' do
51
+ subject { MockComponent1.new(inputs: 2, outputs: 2) }
52
+
53
+ it 'has one input' do
54
+ expect(subject.inputs.count).to eq(2)
45
55
  end
46
- end
47
56
 
48
- describe '#tock' do
49
- it 'tocks the outputs' do
50
- expect(outputs[0]).to receive(:tock)
51
- subject.tock
57
+ it 'has one output' do
58
+ expect(subject.outputs.count).to eq(2)
52
59
  end
53
60
  end
54
61
 
55
- describe '#create_inputs' do
56
- before :each do
57
- allow(subject).to receive(:input_count).and_return(2)
62
+ context 'when supplying inputs and outputs' do
63
+ let(:inputs) { [double('input')] }
64
+ let(:outputs) { [double('output')] }
65
+
66
+ subject { MockComponent1.new(inputs: inputs, outputs: outputs) }
67
+
68
+ describe '#tick' do
69
+ it 'raises NotImplementedError' do
70
+ expect { subject.tick }.to raise_error(NotImplementedError)
71
+ end
58
72
  end
59
73
 
60
- context 'when the given argument is less than the value of input count' do
61
- it 'fails with an argument error' do
62
- expect { subject.mock_create_inputs(0) }.to raise_error(ArgumentError)
74
+ describe '#tock' do
75
+ it 'tocks the outputs' do
76
+ expect(outputs[0]).to receive(:tock)
77
+ subject.tock
63
78
  end
64
79
  end
65
80
 
66
- context 'when given a valid number of inputs' do
67
- [2, 4, 6].each do |n|
68
- it "creates #{n} inputs" do
69
- expect(subject.mock_create_inputs(n).size).to eq(n)
81
+ describe '#[]' do
82
+ context 'one input and one output' do
83
+ it 'has the input available as :in' do
84
+ expect(subject[:in]).to eq(inputs[0])
85
+ end
86
+
87
+ it 'has the output available as :out' do
88
+ expect(subject[:out]).to eq(outputs[0])
89
+ end
90
+ end
91
+
92
+ context 'two inputs and two outputs' do
93
+ let(:inputs) { [double('input_1'), double('input_2')] }
94
+ let(:outputs) { [double('output_1'), double('output_2')] }
95
+
96
+ it 'has the inputs available as :a and :b' do
97
+ expect(subject[:a]).to eq(inputs[0])
98
+ expect(subject[:b]).to eq(inputs[1])
99
+ end
100
+
101
+ it 'has the outputs available as :c and :d' do
102
+ expect(subject[:c]).to eq(outputs[0])
103
+ expect(subject[:d]).to eq(outputs[1])
70
104
  end
71
105
  end
72
106
  end
73
107
  end
108
+
109
+ context 'when you do not override #set_defaults' do
110
+ it 'raises NotImplementedError' do
111
+ expect { MockComponent2.new }.to raise_error(NotImplementedError)
112
+ end
113
+ end
74
114
  end
@@ -13,7 +13,6 @@ describe Circuits::Terminal::Input do
13
13
 
14
14
  describe '#initialize' do
15
15
  context 'when given an output' do
16
-
17
16
  subject { Circuits::Terminal::Input.new(output: output) }
18
17
 
19
18
  it 'sets the output' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: circuits
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henry Muru Paenga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-24 00:00:00.000000000 Z
11
+ date: 2015-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler