Module: SetupConfiguration::BinaryCodedValues

Included in:
Roles, SoftwareOptions
Defined in:
lib/setup_configuration/binary_coded_values.rb

Overview

The BinaryCodedValues mixin provides common methods for transforming meaningful values in binary coded values - and vice versa. The class must provide a method values, which delivers a hash with the values and its numbers.

Instance Method Summary collapse

Instance Method Details

#number(value) ⇒ Object

Gets the number (or: numerical value) of the given value.



12
13
14
15
16
17
18
# File 'lib/setup_configuration/binary_coded_values.rb', line 12

def number(value)
  if values.has_key?(value)
    values[value]
  else
    raise ArgumentError.new("'#{value}' is not a valid option. Valid values: #{self.pretty}")
  end
end

#prettyObject



36
37
38
39
40
# File 'lib/setup_configuration/binary_coded_values.rb', line 36

def pretty
  s = ''
  PP.pp(values.keys, s)
  s
end

#value(number) ⇒ Object

Gets the value to the given number.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/setup_configuration/binary_coded_values.rb', line 21

def value(number)
  # 60.to_s(2).chars.to_a.reverse.each_with_index { |s,i| puts s; puts i}
  result=[]
  unless number.eql?(0) then
    number.to_s(2).chars.to_a.reverse.each_with_index do |value, index|
      if value.eql?('1')
        # invert hash with values to get meaningful value (key) for given binary coded value
        r = values.invert[2**index]
        result << r if r
      end
    end
  end
  result
end