class BitSwitch
Public Class Methods
new(input = 0, labels = {})
click to toggle source
# File lib/bitswitch.rb, line 5 def initialize(input = 0, labels = {}) # Placeholder @labels = {} @val = 0 # Validate the value input unless input.is_a?(Fixnum) || input.is_a?(Hash) raise KellyLSB::BitSwitch::Error, "BitSwitch: BitSwitch can only accept an instance of `Fixnum` or `Hash` as the first argument" end # Validate the labels input unless labels.is_a?(Hash) raise KellyLSB::BitSwitch::Error, "BitSwitch: BitSwitch expected the second argument to be a `Hash`" end # Validate hash value input if input.is_a?(Hash) input.each do |label, value| # Require a String, Symbol or Fixnum value for input hash keys unless label.is_a?(String) || label.is_a?(Symbol) || label.is_a?(Fixnum) raise KellyLSB::BitSwitch::Error, "BitSwitch: Input Hash keys must be a String, Symbol or Fixnum representation of the bit." end # Require input hash values to be true or false unless value === true || value === false raise KellyLSB::BitSwitch::Error, "BitSwitch: Input Hash values must be either true or false." end end end # Validate label hash format labels.each do |bit, label| # Require label bits to be Fixnum unless bit.is_a?(Fixnum) raise KellyLSB::BitSwitch::Error, "BitSwitch: Label Hash keys must be instances of Fixnum" end # Require labels to be Strings or Symbols unless label.is_a?(String) || label.is_a?(Symbol) raise KellyLSB::BitSwitch::Error, "BitSwitch: Label Hash values must be either Symbols or Strings" end end # Apply label hash into the instance variable and assume 0 @labels = labels.inject({}){|h, (k, v)|h.merge(k => v.to_sym)} @val = input.is_a?(Hash) ? 0 : input # Handle hash input if input.is_a?(Hash) # If no labels are set # Loop through the input and set the values input.each_with_index { |(label, value), index| @labels[index] = label.to_sym self[index] = value } if @labels.empty? # Otherwise just set input.each { |label, value| self[label] = value } unless @labels.empty? end end
Public Instance Methods
[](bit)
click to toggle source
Check a bit status
# File lib/bitswitch.rb, line 114 def [](bit) # Validate input label / bit unless bit.is_a?(Symbol) || bit.is_a?(String) || bit.is_a?(Fixnum) raise KellyLSB::BitSwitch::Error, "BitSwitch (#{__method__}): Expected the key to be a Symbol, String or Fixnum" end # Get the numerical representation of the label bit = @labels.invert[bit.to_sym] unless bit.is_a?(Fixnum) # If nil return false if bit.nil? raise KellyLSB::BitSwitch::Error, "BitSwitch (#{__method__}): There was no bit to match the requested label." end # Check if the bit was set (2 ** bit) & @val > 0 end
[]=(bit, val)
click to toggle source
Set a bit (or label)
# File lib/bitswitch.rb, line 79 def []=(bit, val) # Validate input label / bit unless bit.is_a?(Symbol) || bit.is_a?(String) || bit.is_a?(Fixnum) raise KellyLSB::BitSwitch::Error, "BitSwitch (#{__method__}): Expected the key to be a Symbol, String or Fixnum" end # Validate input value unless val === true || val === false || val.is_a?(Fixnum) raise KellyLSB::BitSwitch::Error, "BitSwitch (#{__method__}): Expected the value to be true, false or Fixnum" end # Convert numerical to boolean val = val > 0 if val.is_a?(Fixnum) # Get the numerical representation of the label bit = @labels.invert[bit.to_sym] unless bit.is_a?(Fixnum) # If nil return false if bit.nil? raise KellyLSB::BitSwitch::Error, "BitSwitch (#{__method__}): There was no bit to match the requested label." end # Set/Unset the bits @val |= 2 ** bit if val @val &= ~(2 ** bit) if !val && self[bit] # Return self self end
labels(hash = {}, reset = false)
click to toggle source
# File lib/bitswitch.rb, line 151 def labels(hash = {}, reset = false) # Either merge or overwrite labels @labels.merge!(hash) unless reset @labels = hash if reset # Return self self end
method_missing(method, *args)
click to toggle source
Method access
# File lib/bitswitch.rb, line 188 def method_missing(method, *args) # Handle setting values if method[-1] == '=' method = method[0..-2] return self[method] = args.first end # Return a value self[method] end
set=(input)
click to toggle source
Set an integer
# File lib/bitswitch.rb, line 136 def set=(input) # Validate input unless input.is_a?(Fixnum) raise KellyLSB::BitSwitch::Error, "BitSwitch (#{__method__}): Expected value to be a Fixnum" end # Set the value @val = input # Return self self end
to_hash()
click to toggle source
Get hash
# File lib/bitswitch.rb, line 167 def to_hash # Make sure labels are set if @labels.empty? raise KellyLSB::BitSwitch::Error, "BitSwitch (#{__method__}): No labels were set!" end # Prepare new hash serialized = Hash.new # Loop through the labels @labels.each do |bit, label| serialized[label] = self[bit] end # Return serialization serialized end
to_i()
click to toggle source
Get value
# File lib/bitswitch.rb, line 162 def to_i @val end