circuits 0.5.0 → 0.6.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
- YzIxYzBlMDM5ZmJjYjliOWZkODEwM2QxNGUwYmE5ZGIxMWZlYzFkZg==
4
+ NzU2MmI0ZGE1MTA5YjllOWE3NTdiOGFhOTJlOTI3OTI4ZGQ5M2RmOA==
5
5
  data.tar.gz: !binary |-
6
- Y2E3NDYzMjVmMWY3OWI4MjliZmE5NTRlMTIyMDAwZTRmMGM5ZjNhZg==
6
+ MGM4Y2QwMTI3OTM1NGUyNjdhNzE3NzYyMDQyNzIyNzk5ZDk3NGMxZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MTJhMDdmNzE2ZDYyMjAzYjkyMGJlMmRkNmNhYjc4NTZhMzliYmM1MzZiNjdh
10
- OGUwMzY2ZWM0OThjYTQyNDIwYTZmOWMxYTdlOTA3ODAwZDhiZjI2NmE0NjEy
11
- ZTA1MDg2MTE4NmMyMWI2YTgzYzhjMzRiNmQzOTY4MGZhNTM5YjM=
9
+ Y2UwZGRjZGVlOTc0ZTVkNDcxMjkyMjRhNjU0OWNiYWQxZDg5YTA2NzgyZWM4
10
+ ODYzMjNkM2QyNGYxMGVjYzRjOGY1ODEwMjc4ZTAxNWJjYzNiZTJjZThkY2Zl
11
+ MGQ0MTAyZTcwOWFhZGMwY2M2YjZkOWM1NjQyYzEwZmE3MmJkNzE=
12
12
  data.tar.gz: !binary |-
13
- MjM5YzM1OTE2MTZlZDc4ZjdlNDI1ODgzNTkxNmUxZWVjYjgyNGFkNDU4Yjlh
14
- MTZiNjY0MDM5OGJlMzRiNjc0ZjU3YjM5MzU3NzM5ZWQyNDVmZTE2ZjRlYTdl
15
- MDVhZjViYjc2MTlhNTc5ZWU1NGYzM2E5YzYzMGIxMDI5MmYyZDM=
13
+ MjljM2I5M2FmNTFmOTIyNGI2MDkwMjQ0YTZmNzM4ZDU5OTAzZWNiOTA2Mzlh
14
+ OTkzMGUyNzgzMDEyZTBlNzczMTNmNGYzNmYyZmU5YjBjMDQyOWIyZmJiMDE0
15
+ MmMwZmFjMWYyNTZlNTFlNzNhOWRlM2M0NjE1NWQ5ZjFjZGY3YTY=
data/README.md CHANGED
@@ -51,7 +51,8 @@ and_gate[:out].get # false
51
51
 
52
52
  ```ruby
53
53
  and_gate = Circuits::Component::And.new
54
- not_gate = Circuits::Component::Not.new(inputs: [and_gate[:out]])
54
+ not_gate = Circuits::Component::Not.new
55
+ not_gate[:in].set = and_gate[:out]
55
56
  ```
56
57
 
57
58
  ## Contributing
@@ -1,5 +1,4 @@
1
- require 'circuits/terminal/input'
2
- require 'circuits/terminal/output'
1
+ require 'circuits/terminal'
3
2
 
4
3
  module Circuits
5
4
  # A component has a set of inputs an outputs. Every `tick` a componnent
@@ -69,7 +68,7 @@ module Circuits
69
68
  elsif opts[:inputs].class == Fixnum
70
69
  @input_count = opts[:inputs]
71
70
  end
72
- @inputs ||= @input_count.times.collect { Circuits::Terminal::Input.new }
71
+ @inputs ||= @input_count.times.collect { Circuits::Terminal.new }
73
72
  end
74
73
 
75
74
  def create_outputs(opts)
@@ -80,7 +79,7 @@ module Circuits
80
79
  @output_count = opts[:outputs]
81
80
  end
82
81
  @outputs ||= @output_count.times.collect do
83
- Circuits::Terminal::Output.new
82
+ Circuits::Terminal.new
84
83
  end
85
84
  end
86
85
 
@@ -9,13 +9,16 @@ module Circuits
9
9
  super opts
10
10
  create_sub_components
11
11
  link_sub_components
12
+ self[:q].set nand_1[:out]
13
+ self[:not_q].set nand_2[:out]
12
14
  end
13
15
 
14
16
  # Computes the outputs based on the inputs and previous state
15
17
  def tick
16
- update_sub_components
17
- self[:q].set nand_1[:out].get
18
- self[:not_q].set nand_2[:out].get
18
+ 2.times.each do
19
+ sub_components.each(&:tick)
20
+ sub_components.each(&:tock)
21
+ end
19
22
  end
20
23
 
21
24
  private
@@ -45,13 +48,6 @@ module Circuits
45
48
  not_q: { type: :output, number: 1 }
46
49
  }
47
50
  end
48
-
49
- def update_sub_components
50
- 2.times.each do
51
- sub_components.each(&:tick)
52
- sub_components.each(&:tock)
53
- end
54
- end
55
51
  end
56
52
  end
57
53
  end
@@ -7,28 +7,31 @@ module Circuits
7
7
  def initialize(opts = {})
8
8
  set_defaults
9
9
  super opts
10
- create_internal_components
11
- link_internal_components
10
+ create_sub_components
11
+ link_sub_components
12
+ self[:q].set nor_1[:out]
13
+ self[:not_q].set nor_2[:out]
12
14
  end
13
15
 
14
16
  # Computes the outputs based on the inputs and previous state
15
17
  def tick
16
- update_internal_components
17
- self[:q].set nor_1[:out].get
18
- self[:not_q].set nor_2[:out].get
18
+ 2.times.each do
19
+ sub_components.each(&:tick)
20
+ sub_components.each(&:tock)
21
+ end
19
22
  end
20
23
 
21
24
  private
22
25
 
23
26
  attr_reader :nor_1, :nor_2, :sub_components
24
27
 
25
- def create_internal_components
28
+ def create_sub_components
26
29
  @nor_1 = Nor.new
27
30
  @nor_2 = Nor.new
28
31
  @sub_components = [@nor_1, @nor_2]
29
32
  end
30
33
 
31
- def link_internal_components
34
+ def link_sub_components
32
35
  nor_1[:a] = self[:r]
33
36
  nor_2[:a] = self[:s]
34
37
  nor_1[:b] = nor_2[:out]
@@ -45,13 +48,6 @@ module Circuits
45
48
  not_q: { type: :output, number: 1 }
46
49
  }
47
50
  end
48
-
49
- def update_internal_components
50
- 2.times.each do
51
- sub_components.each(&:tick)
52
- sub_components.each(&:tock)
53
- end
54
- end
55
51
  end
56
52
  end
57
53
  end
@@ -1,5 +1,35 @@
1
1
  module Circuits
2
2
  # A terminal holds a logical state, it can be attatched to components
3
- module Terminal
3
+ class Terminal
4
+ # Creates the terminal
5
+ # @param opts [Hash] Options to create the Output with
6
+ # @option opts [Boolean] :state The initial state of the Output
7
+ def initialize(opts = {})
8
+ @terminal = opts[:terminal]
9
+ @next_state = opts[:state] || false
10
+ @state = opts[:state] || false
11
+ end
12
+
13
+ # Gets the state of the terminal
14
+ # @return [Boolean] The state of the output
15
+ def get
16
+ @state
17
+ end
18
+
19
+ # The other terminal to read from next {#tock} or a state to be in now
20
+ # @param [Boolean, Terminal] terminal The terminal or state to output
21
+ def set(terminal)
22
+ if terminal.class == Terminal
23
+ @terminal = terminal
24
+ else
25
+ @terminal = nil
26
+ @state = @next_state = terminal
27
+ end
28
+ end
29
+
30
+ # Sets the state to be that of the terminal or state last passed by {#set}
31
+ def tock
32
+ @state = @terminal.nil? ? @next_state : @terminal.get
33
+ end
4
34
  end
5
35
  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.5.0'
4
+ VERSION = '0.6.0'
5
5
  end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+ require 'circuits/terminal'
3
+
4
+ describe Circuits::Terminal do
5
+ describe '#get' do
6
+ let(:state) { double('state') }
7
+
8
+ context 'when given no state' do
9
+ subject { Circuits::Terminal.new }
10
+
11
+ it 'is false' do
12
+ expect(subject.get).to eq(false)
13
+ end
14
+ end
15
+
16
+ context 'when given a state' do
17
+ subject { Circuits::Terminal.new(state: state) }
18
+
19
+ it 'has that state' do
20
+ expect(subject.get).to eq(state)
21
+ end
22
+ end
23
+
24
+ context 'when given terminal' do
25
+ let(:terminal) { Circuits::Terminal.new(state: state) }
26
+
27
+ subject { Circuits::Terminal.new(terminal: terminal) }
28
+
29
+ it 'is false' do
30
+ expect(subject.get).to eq(false)
31
+ end
32
+ end
33
+ end
34
+
35
+ describe '#set' do
36
+ let(:state_1) { double('state_1') }
37
+ let(:state_2) { double('state_2') }
38
+
39
+ context 'when given a state' do
40
+ subject { Circuits::Terminal.new(state: state_1) }
41
+
42
+ it 'gets set immediately' do
43
+ subject.set state_2
44
+ expect(subject.get).to eq(state_2)
45
+ end
46
+ end
47
+
48
+ context 'when given a terminal' do
49
+ let(:terminal) { Circuits::Terminal.new(state: state_2) }
50
+
51
+ subject { Circuits::Terminal.new(state: state_1) }
52
+
53
+ it 'gets does not get set immediately' do
54
+ subject.set terminal
55
+ expect(subject.get).to eq(state_1)
56
+ end
57
+ end
58
+ end
59
+
60
+ describe '#tock' do
61
+ let(:state) { double('state') }
62
+
63
+ context 'when passed a state' do
64
+ subject { Circuits::Terminal.new }
65
+
66
+ it 'moves next set to get' do
67
+ subject.set state
68
+ subject.tock
69
+ expect(subject.get).to eq(state)
70
+ end
71
+ end
72
+
73
+ context 'when given terminal' do
74
+ let(:terminal) { Circuits::Terminal.new(state: state) }
75
+
76
+ subject { Circuits::Terminal.new(terminal: terminal) }
77
+
78
+ it 'is has that state' do
79
+ subject.tock
80
+ expect(subject.get).to eq(state)
81
+ end
82
+ end
83
+ end
84
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: circuits
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henry Muru Paenga
@@ -135,8 +135,6 @@ files:
135
135
  - lib/circuits/component/xnor.rb
136
136
  - lib/circuits/component/xor.rb
137
137
  - lib/circuits/terminal.rb
138
- - lib/circuits/terminal/input.rb
139
- - lib/circuits/terminal/output.rb
140
138
  - lib/circuits/version.rb
141
139
  - spec/spec_helper.rb
142
140
  - spec/unit/circuits/component/and_spec.rb
@@ -149,8 +147,7 @@ files:
149
147
  - spec/unit/circuits/component/sr_nor_spec.rb
150
148
  - spec/unit/circuits/component/xnor_spec.rb
151
149
  - spec/unit/circuits/component/xor_spec.rb
152
- - spec/unit/circuits/terminal/input_spec.rb
153
- - spec/unit/circuits/terminal/output_spec.rb
150
+ - spec/unit/circuits/terminal_spec.rb
154
151
  homepage: https://github.com/meringu/circuits
155
152
  licenses:
156
153
  - MIT
@@ -187,5 +184,4 @@ test_files:
187
184
  - spec/unit/circuits/component/sr_nor_spec.rb
188
185
  - spec/unit/circuits/component/xnor_spec.rb
189
186
  - spec/unit/circuits/component/xor_spec.rb
190
- - spec/unit/circuits/terminal/input_spec.rb
191
- - spec/unit/circuits/terminal/output_spec.rb
187
+ - spec/unit/circuits/terminal_spec.rb
@@ -1,30 +0,0 @@
1
- require 'circuits/terminal'
2
-
3
- module Circuits
4
- module Terminal
5
- # Reads from a single output, only assigned to one component
6
- class Input
7
- # Creates the input
8
- # @param opts [Hash] Options to create the Input with
9
- # @option opts [Component::Output] :output The output to read from
10
- def initialize(opts = {})
11
- @output = opts[:output] || Circuits::Terminal::Output.new
12
- end
13
-
14
- # The output the input reads from
15
- attr_accessor :output
16
-
17
- # Forward get to the output
18
- # @return [Boolean] The state of the output
19
- def get
20
- @output.get
21
- end
22
-
23
- # Create a new output to use
24
- # @param [Boolean] s The state of the output to create
25
- def set(s)
26
- @output = Circuits::Terminal::Output.new(state: s)
27
- end
28
- end
29
- end
30
- end
@@ -1,37 +0,0 @@
1
- require 'circuits/terminal'
2
-
3
- module Circuits
4
- module Terminal
5
- # Belongs to a single component, gets set for reading by inputs
6
- class Output
7
- # Creates the output
8
- # @param opts [Hash] Options to create the Output with
9
- # @option opts [Boolean] :state The initial state of the Output
10
- def initialize(opts = {})
11
- @next_state = opts[:state] || false
12
- @state = opts[:state] || false
13
- end
14
-
15
- # Gets the state of the output
16
- # @return [Boolean] The state of the output
17
- def get
18
- state
19
- end
20
-
21
- # Saves the state
22
- # @param [Boolean] s The next desired state of the output
23
- def set(s)
24
- @next_state = s
25
- end
26
-
27
- # Sets the state to be the last state passed by #set
28
- def tock
29
- @state = next_state
30
- end
31
-
32
- private
33
-
34
- attr_reader :state, :next_state
35
- end
36
- end
37
- end
@@ -1,60 +0,0 @@
1
- require 'spec_helper'
2
- require 'circuits/terminal/input'
3
-
4
- module Circuits
5
- module Terminal
6
- class Output
7
- end
8
- end
9
- end
10
-
11
- describe Circuits::Terminal::Input do
12
- let(:output) { double('output') }
13
-
14
- describe '#initialize' do
15
- context 'when given an output' do
16
- subject { Circuits::Terminal::Input.new(output: output) }
17
-
18
- it 'sets the output' do
19
- expect(subject.output).to eq(output)
20
- end
21
- end
22
-
23
- context 'when not given an output' do
24
- subject { Circuits::Terminal::Input.new }
25
-
26
- before do
27
- expect(Circuits::Terminal::Output).to receive(:new).and_return(output)
28
- end
29
-
30
- it 'sets the output' do
31
- expect(subject.output).to eq(output)
32
- end
33
- end
34
- end
35
-
36
- describe '#get' do
37
- let(:res) { double('res') }
38
-
39
- subject { Circuits::Terminal::Input.new(output: output) }
40
-
41
- it 'forwards the request to output' do
42
- expect(output).to receive(:get).and_return(res)
43
- expect(subject.get).to eq(res)
44
- end
45
- end
46
-
47
- describe '#set' do
48
- let(:state) { double('state') }
49
- let(:new_output) { double('new_output', get: state) }
50
-
51
- subject { Circuits::Terminal::Input.new(output: output) }
52
-
53
- it 'sets the output to the passed output' do
54
- expect(Circuits::Terminal::Output).to receive(:new)
55
- .with(state: state).and_return(new_output)
56
- subject.set state
57
- expect(subject.get).to eq(state)
58
- end
59
- end
60
- end
@@ -1,54 +0,0 @@
1
- require 'spec_helper'
2
- require 'circuits/terminal/output'
3
-
4
- describe Circuits::Terminal::Output do
5
- describe '#get' do
6
- context 'when given no state' do
7
- subject { Circuits::Terminal::Output.new }
8
-
9
- it 'is false' do
10
- expect(subject.get).to eq(false)
11
- end
12
- end
13
-
14
- context 'when given false state' do
15
- subject { Circuits::Terminal::Output.new(state: false) }
16
-
17
- it 'is false' do
18
- expect(subject.get).to eq(false)
19
- end
20
- end
21
-
22
- context 'when given true state' do
23
- subject { Circuits::Terminal::Output.new(state: true) }
24
-
25
- it 'is true' do
26
- expect(subject.get).to eq(true)
27
- end
28
- end
29
- end
30
-
31
- describe '#set' do
32
- let(:state_1) { double('state_1') }
33
- let(:state_2) { double('state_2') }
34
-
35
- subject { Circuits::Terminal::Output.new(state: state_1) }
36
-
37
- it 'does not set get immediately' do
38
- subject.set state_2
39
- expect(subject.get).to eq(state_1)
40
- end
41
- end
42
-
43
- describe '#tock' do
44
- let(:state) { double('state') }
45
-
46
- subject { Circuits::Terminal::Output.new }
47
-
48
- it 'moves next set to get' do
49
- subject.set state
50
- subject.tock
51
- expect(subject.get).to eq(state)
52
- end
53
- end
54
- end