Class: Circuits::Component::Base
- Inherits:
-
Object
- Object
- Circuits::Component::Base
- Defined in:
- lib/circuits/component/base.rb
Overview
Base class to extend
Instance Attribute Summary collapse
-
#inputs ⇒ Object
readonly
the inputs of this component.
-
#outputs ⇒ Object
readonly
the outputs of this component.
Instance Method Summary collapse
-
#[](port) ⇒ Input, Output
Gets the teminal assigned to the port.
-
#initialize(opts = {}) ⇒ Base
constructor
Creates the Component with inputs and outputs array of their names array of their names.
-
#tick ⇒ Object
Computes the outputs based on the inputs and previous state.
-
#tock ⇒ Object
Sets all the outputs expose what was set in #tick.
Constructor Details
#initialize(opts = {}) ⇒ Base
Creates the Component with inputs and outputs array of their names array of their names
18 19 20 21 22 23 24 25 |
# File 'lib/circuits/component/base.rb', line 18 def initialize(opts = {}) @inputs = create_inputs opts[:inputs] @outputs = create_outputs opts[:outputs] @port_mappings = create_port_mappings opts @sub_components = opts[:sub_components] || [] @ticks = opts[:ticks] || 0 declare_ports_as_methods end |
Instance Attribute Details
#inputs ⇒ Object (readonly)
the inputs of this component
28 29 30 |
# File 'lib/circuits/component/base.rb', line 28 def inputs @inputs end |
#outputs ⇒ Object (readonly)
the outputs of this component
31 32 33 |
# File 'lib/circuits/component/base.rb', line 31 def outputs @outputs end |
Instance Method Details
#[](port) ⇒ Input, Output
Gets the teminal assigned to the port
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/circuits/component/base.rb', line 49 def [](port) port_mapping = @port_mappings[port] return nil if port_mapping.nil? port_number = port_mapping[:number] case port_mapping[:type] when :input inputs[port_number] when :output outputs[port_number] end end |
#tick ⇒ Object
Computes the outputs based on the inputs and previous state
34 35 36 37 38 39 |
# File 'lib/circuits/component/base.rb', line 34 def tick @ticks.times.each do @sub_components.each(&:tick) @sub_components.each(&:tock) end end |
#tock ⇒ Object
Sets all the outputs expose what was set in #tick
42 43 44 |
# File 'lib/circuits/component/base.rb', line 42 def tock outputs.each(&:tock) end |