module BBB::Components::Pinnable

Attributes

pins[R]
positions[R]

Public Class Methods

included(base) click to toggle source
# File lib/BBB/components/pinnable.rb, line 46
def self.included(base)
  base.extend(ClassMethods)
end
new(options={}) click to toggle source

Provide a default initializer

# File lib/BBB/components/pinnable.rb, line 140
def initialize(options={})
  set_options(options)
end

Public Instance Methods

after_connect_callbacks() click to toggle source
# 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) click to toggle source

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.

@param positions [Array<Symbol>] positions the pins should be

initialized with.

@param opts [Hash] Options to pass along to the pins during

initialization.

@return Array

# 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
Also aliased as: pin_initialization
pin() click to toggle source

Convenience method to grab the first pin in the pins array

@return BBB::Pins::AnalogPin

# File lib/BBB/components/pinnable.rb, line 133
def pin
  pins.first
end
pin_initialization(*positions)
Alias for: connect
pinnable?() click to toggle source

Method that is used in the test suite to test if the pinnable module is included in an object

@return true

# File lib/BBB/components/pinnable.rb, line 124
def pinnable?
  true
end
set_options(options) click to toggle source

Provide a function to handle the options

@param options [Hash]

# File lib/BBB/components/pinnable.rb, line 149
def set_options(options)
  @positions = [options[:pin],
                options[:pins],
                options[:position],
                options[:path]].flatten.compact
end
verify_pin_position_count(positions) click to toggle source

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.

@param positions [Array<Symbol>] The array of positions

@raise [PinsDoNotMatchException] If the pin counts between the positions

provided as an argument and the pins in the pins array do not match,
this exception gets raised, to prevent hard to debug situations later
on in the lifecycle of an application.

@return Void

# 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