circuits 0.6.0 → 0.7.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
- NzU2MmI0ZGE1MTA5YjllOWE3NTdiOGFhOTJlOTI3OTI4ZGQ5M2RmOA==
4
+ MzQwY2JkYzJjMDVkZWNmMjFlZTI0ZWQzZDNmOGQwMWIxODE5YmVlMw==
5
5
  data.tar.gz: !binary |-
6
- MGM4Y2QwMTI3OTM1NGUyNjdhNzE3NzYyMDQyNzIyNzk5ZDk3NGMxZA==
6
+ YTY3NGI2Nzc3MzBlZjZjZDg1MWEyMzg0NDc3MWY5ODBhZDYxNWJjYg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- Y2UwZGRjZGVlOTc0ZTVkNDcxMjkyMjRhNjU0OWNiYWQxZDg5YTA2NzgyZWM4
10
- ODYzMjNkM2QyNGYxMGVjYzRjOGY1ODEwMjc4ZTAxNWJjYzNiZTJjZThkY2Zl
11
- MGQ0MTAyZTcwOWFhZGMwY2M2YjZkOWM1NjQyYzEwZmE3MmJkNzE=
9
+ ZjcwM2U4YzY1N2JiYWVmZWIwNWE3ZGFmYmFmNWRkMTg4MWVlYTU2MzRiMzM0
10
+ ZmFkMDI2MmM0NDBjY2I1Y2ViODJjMTMwN2EzNTY3ZmRhYjA1Zjk1MDhiNTA1
11
+ OGZlM2ZkYjBkMzU5ZjNkNmZhOGFmNTNkNWQxNmJjZTQzNjE2M2U=
12
12
  data.tar.gz: !binary |-
13
- MjljM2I5M2FmNTFmOTIyNGI2MDkwMjQ0YTZmNzM4ZDU5OTAzZWNiOTA2Mzlh
14
- OTkzMGUyNzgzMDEyZTBlNzczMTNmNGYzNmYyZmU5YjBjMDQyOWIyZmJiMDE0
15
- MmMwZmFjMWYyNTZlNTFlNzNhOWRlM2M0NjE1NWQ5ZjFjZGY3YTY=
13
+ MWRjYmY5MjhhZTJiMWZiNjgwZjgyZjMzMTkxYWUwODBkZTVjMDRkNTVjZTUy
14
+ MWMxOTVmYWQ4M2RkZDdiYzUwYWMyMjAwNTkyMmQ1NzgyNGE4ZTgzZTlkYWMx
15
+ ZmU4M2M0ZjU5Mzg4NzhhYTkwYzZhNTIwOGEyMjUzZTk4NjE4MjA=
@@ -12,7 +12,7 @@ module Circuits
12
12
 
13
13
  # Sets the output to be the result of a logical AND of the inputs
14
14
  def tick
15
- outputs[0].set(inputs.map(&:get).inject(:&))
15
+ self[:out].set(inputs.map(&:get).inject(:&))
16
16
  end
17
17
  end
18
18
  end
@@ -1,4 +1,5 @@
1
- require 'circuits/terminal'
1
+ require 'circuits/terminal/input'
2
+ require 'circuits/terminal/output'
2
3
 
3
4
  module Circuits
4
5
  # A component has a set of inputs an outputs. Every `tick` a componnent
@@ -68,7 +69,7 @@ module Circuits
68
69
  elsif opts[:inputs].class == Fixnum
69
70
  @input_count = opts[:inputs]
70
71
  end
71
- @inputs ||= @input_count.times.collect { Circuits::Terminal.new }
72
+ @inputs ||= @input_count.times.collect { Circuits::Terminal::Input.new }
72
73
  end
73
74
 
74
75
  def create_outputs(opts)
@@ -79,7 +80,7 @@ module Circuits
79
80
  @output_count = opts[:outputs]
80
81
  end
81
82
  @outputs ||= @output_count.times.collect do
82
- Circuits::Terminal.new
83
+ Circuits::Terminal::Output.new
83
84
  end
84
85
  end
85
86
 
@@ -12,7 +12,7 @@ module Circuits
12
12
 
13
13
  # Sets the output to be the result of a logical NAND of the inputs
14
14
  def tick
15
- outputs[0].set(!inputs.map(&:get).inject(:&))
15
+ self[:out].set(!inputs.map(&:get).inject(:&))
16
16
  end
17
17
  end
18
18
  end
@@ -12,7 +12,7 @@ module Circuits
12
12
 
13
13
  # Sets the output to be the result of a logical OR of the inputs
14
14
  def tick
15
- outputs[0].set(!inputs.map(&:get).inject(:|))
15
+ self[:out].set(!inputs.map(&:get).inject(:|))
16
16
  end
17
17
  end
18
18
  end
@@ -12,7 +12,7 @@ module Circuits
12
12
 
13
13
  # Sets the output to be the result of a logical NOT of the inputs
14
14
  def tick
15
- outputs[0].set(!inputs[0].get)
15
+ self[:out].set(!self[:in].get)
16
16
  end
17
17
  end
18
18
  end
@@ -12,7 +12,7 @@ module Circuits
12
12
 
13
13
  # Sets the output to be the result of a logical OR of the inputs
14
14
  def tick
15
- outputs[0].set(inputs.map(&:get).inject(:|))
15
+ self[:out].set(inputs.map(&:get).inject(:|))
16
16
  end
17
17
  end
18
18
  end
@@ -1,4 +1,5 @@
1
1
  require 'circuits/component/base'
2
+ require 'circuits/component/nand'
2
3
 
3
4
  module Circuits
4
5
  module Component
@@ -8,9 +9,9 @@ module Circuits
8
9
  set_defaults
9
10
  super opts
10
11
  create_sub_components
12
+ link_inputs
13
+ link_outputs
11
14
  link_sub_components
12
- self[:q].set nand_1[:out]
13
- self[:not_q].set nand_2[:out]
14
15
  end
15
16
 
16
17
  # Computes the outputs based on the inputs and previous state
@@ -31,11 +32,19 @@ module Circuits
31
32
  @sub_components = [@nand_1, @nand_2]
32
33
  end
33
34
 
35
+ def link_inputs
36
+ nand_1[:a].set self[:not_s]
37
+ nand_2[:a].set self[:not_r]
38
+ end
39
+
40
+ def link_outputs
41
+ self[:q].set nand_1[:out]
42
+ self[:not_q].set nand_2[:out]
43
+ end
44
+
34
45
  def link_sub_components
35
- nand_1[:a] = self[:not_s]
36
- nand_2[:a] = self[:not_r]
37
- nand_1[:b] = nand_2[:out]
38
- nand_2[:b] = nand_1[:out]
46
+ nand_1[:b].set nand_2[:out]
47
+ nand_2[:b].set nand_1[:out]
39
48
  end
40
49
 
41
50
  def set_defaults
@@ -1,4 +1,5 @@
1
1
  require 'circuits/component/base'
2
+ require 'circuits/component/nor'
2
3
 
3
4
  module Circuits
4
5
  module Component
@@ -8,9 +9,9 @@ module Circuits
8
9
  set_defaults
9
10
  super opts
10
11
  create_sub_components
12
+ link_inputs
13
+ link_outputs
11
14
  link_sub_components
12
- self[:q].set nor_1[:out]
13
- self[:not_q].set nor_2[:out]
14
15
  end
15
16
 
16
17
  # Computes the outputs based on the inputs and previous state
@@ -31,11 +32,19 @@ module Circuits
31
32
  @sub_components = [@nor_1, @nor_2]
32
33
  end
33
34
 
35
+ def link_inputs
36
+ nor_1[:a].set self[:r]
37
+ nor_2[:a].set self[:s]
38
+ end
39
+
40
+ def link_outputs
41
+ self[:q].set nor_1[:out]
42
+ self[:not_q].set nor_2[:out]
43
+ end
44
+
34
45
  def link_sub_components
35
- nor_1[:a] = self[:r]
36
- nor_2[:a] = self[:s]
37
- nor_1[:b] = nor_2[:out]
38
- nor_2[:b] = nor_1[:out]
46
+ nor_1[:b].set nor_2[:out]
47
+ nor_2[:b].set nor_1[:out]
39
48
  end
40
49
 
41
50
  def set_defaults
@@ -12,7 +12,7 @@ module Circuits
12
12
 
13
13
  # Sets the output to be the result of a logical XNOR of the inputs
14
14
  def tick
15
- outputs[0].set(!inputs.map(&:get).inject(:^))
15
+ self[:out].set(!inputs.map(&:get).inject(:^))
16
16
  end
17
17
  end
18
18
  end
@@ -12,7 +12,7 @@ module Circuits
12
12
 
13
13
  # Sets the output to be the result of a logical XOR of the inputs
14
14
  def tick
15
- outputs[0].set(inputs.map(&:get).inject(:^))
15
+ self[:out].set(inputs.map(&:get).inject(:^))
16
16
  end
17
17
  end
18
18
  end
@@ -0,0 +1,32 @@
1
+ require 'circuits/terminal/output'
2
+
3
+ module Circuits
4
+ # An input or an output to read from and set
5
+ module Terminal
6
+ # Reads from a single output
7
+ class Input
8
+ # Creates the input
9
+ # @param opts [Hash] Options to create the Input with
10
+ # @option opts [Component::Output] :output The output to read from
11
+ def initialize(opts = {})
12
+ @terminal = opts[:terminal] || Output.new(state: opts[:state])
13
+ end
14
+
15
+ # Forward get to the output
16
+ # @return [Boolean] The state of the output
17
+ def get
18
+ @terminal.get
19
+ end
20
+
21
+ # Output to use or state to make a dummy output with
22
+ # @param [Output, Boolean] output The output to read from, or state
23
+ def set(output)
24
+ if [Input, Output].include? output.class
25
+ @terminal = output
26
+ else
27
+ @terminal = Output.new(state: output)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,36 @@
1
+ module Circuits
2
+ # An input or an output to read from and set
3
+ module Terminal
4
+ # Gets set tcked then read
5
+ class Output
6
+ # Creates the output
7
+ # @param opts [Hash] Options to create the Output with
8
+ # @option opts [Boolean] :state The initial state of the Output
9
+ def initialize(opts = {})
10
+ @next_state = opts[:terminal] || opts[:state] || false
11
+ tock
12
+ end
13
+
14
+ # Gets the state of the terminal
15
+ # @return [Boolean] The state of the output
16
+ def get
17
+ @state
18
+ end
19
+
20
+ # The next state
21
+ # @param [Boolean, Terminal] terminal The terminal or state to output
22
+ def set(state)
23
+ @next_state = state
24
+ end
25
+
26
+ # Sets the state what was last set
27
+ def tock
28
+ if [Input, Output].include? @next_state.class
29
+ @state = @next_state.get
30
+ else
31
+ @state = @next_state
32
+ end
33
+ end
34
+ end
35
+ end
36
+ 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.6.0'
4
+ VERSION = '0.7.0'
5
5
  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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henry Muru Paenga
@@ -134,7 +134,8 @@ files:
134
134
  - lib/circuits/component/sr_nor.rb
135
135
  - lib/circuits/component/xnor.rb
136
136
  - lib/circuits/component/xor.rb
137
- - lib/circuits/terminal.rb
137
+ - lib/circuits/terminal/input.rb
138
+ - lib/circuits/terminal/output.rb
138
139
  - lib/circuits/version.rb
139
140
  - spec/spec_helper.rb
140
141
  - spec/unit/circuits/component/and_spec.rb
@@ -147,7 +148,6 @@ files:
147
148
  - spec/unit/circuits/component/sr_nor_spec.rb
148
149
  - spec/unit/circuits/component/xnor_spec.rb
149
150
  - spec/unit/circuits/component/xor_spec.rb
150
- - spec/unit/circuits/terminal_spec.rb
151
151
  homepage: https://github.com/meringu/circuits
152
152
  licenses:
153
153
  - MIT
@@ -184,4 +184,3 @@ test_files:
184
184
  - spec/unit/circuits/component/sr_nor_spec.rb
185
185
  - spec/unit/circuits/component/xnor_spec.rb
186
186
  - spec/unit/circuits/component/xor_spec.rb
187
- - spec/unit/circuits/terminal_spec.rb
@@ -1,35 +0,0 @@
1
- module Circuits
2
- # A terminal holds a logical state, it can be attatched to components
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
34
- end
35
- end
@@ -1,84 +0,0 @@
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