Module: BBB::Components::Pinnable
- Included in:
- AnalogComponent, Button, ESC, Led, Nunchuck, Servo, WiiMotionPlus
- Defined in:
- lib/BBB/components/pinnable.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#pins ⇒ Object
readonly
Returns the value of attribute pins.
-
#positions ⇒ Object
readonly
Returns the value of attribute positions.
Class Method Summary collapse
Instance Method Summary collapse
- #after_connect_callbacks ⇒ Object
-
#connect(*positions) ⇒ Object
(also: #pin_initialization)
Initialize the pin classes with their positions and options.
-
#initialize(options = {}) ⇒ Object
Provide a default initializer.
-
#pin ⇒ Object
Convenience method to grab the first pin in the pins array.
-
#pinnable? ⇒ Boolean
Method that is used in the test suite to test if the pinnable module is included in an object.
-
#set_options(options) ⇒ Object
Provide a function to handle the options.
-
#verify_pin_position_count(positions) ⇒ Object
Verifies if the number of pins registered in the @pins array match with the number of pins provided as an argument to the method.
Instance Attribute Details
#pins ⇒ Object (readonly)
Returns the value of attribute pins.
4 5 6 |
# File 'lib/BBB/components/pinnable.rb', line 4 def pins @pins end |
#positions ⇒ Object (readonly)
Returns the value of attribute positions.
5 6 7 |
# File 'lib/BBB/components/pinnable.rb', line 5 def positions @positions end |
Class Method Details
.included(base) ⇒ Object
46 47 48 |
# File 'lib/BBB/components/pinnable.rb', line 46 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#after_connect_callbacks ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/BBB/components/pinnable.rb', line 108 def after_connect_callbacks self.class.after_connect_callbacks.each do |callback| if callback.responds_to?(:call) callback.call else self.send(callback) end end end |
#connect(*positions) ⇒ Object Also known as: pin_initialization
Initialize the pin classes with their positions and options. Turning them from their Classes into working pin instances.
The argument list of the methods is a bit odd. Since it’s not possible to have both a splat and an option array as arguments, the method signature only shows a splat of positions. The options are then taken out of the positions, if the last position is actually a Hash as opposed to a Symbol. If the last element of the positions list is not a Hash, the options are set to an empty hash.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/BBB/components/pinnable.rb', line 68 def connect(*positions) positions = self.positions if positions.empty? positions.flatten! opts = positions.last.kind_of?(Hash) ? positions.pop : {} verify_pin_position_count(positions) @pins = [] self.class.pin_classes.each_with_index do |pin, index| @pins << pin.new(positions[index], opts) end after_connect_callbacks return self # so that we can chain it with the initializer end |
#initialize(options = {}) ⇒ Object
Provide a default initializer
140 141 142 |
# File 'lib/BBB/components/pinnable.rb', line 140 def initialize(={}) () end |
#pin ⇒ Object
Convenience method to grab the first pin in the pins array
133 134 135 |
# File 'lib/BBB/components/pinnable.rb', line 133 def pin pins.first end |
#pinnable? ⇒ Boolean
Method that is used in the test suite to test if the pinnable module is included in an object
124 125 126 |
# File 'lib/BBB/components/pinnable.rb', line 124 def pinnable? true end |
#set_options(options) ⇒ Object
Provide a function to handle the options
149 150 151 152 153 154 |
# File 'lib/BBB/components/pinnable.rb', line 149 def () @positions = [[:pin], [:pins], [:position], [:path]].flatten.compact end |
#verify_pin_position_count(positions) ⇒ Object
Verifies if the number of pins registered in the @pins array match with the number of pins provided as an argument to the method. This function normally gets called as part of the initialize pins method to verify if the positions given to the connect method matches the number of registered pins.
101 102 103 104 105 106 |
# File 'lib/BBB/components/pinnable.rb', line 101 def verify_pin_position_count(positions) if self.class.pin_classes.count != positions.count fail PinsDoNotMatchException, "#{self.class.to_s} requires #{self.class.pin_classes.count} but received #{positions.count} pin position." end end |