Class: BareTypes::Enum
Instance Method Summary collapse
- #==(otherType) ⇒ Object
- #decode(msg) ⇒ Object
- #encode(msg, buffer) ⇒ Object
- #finalize_references(schema) ⇒ Object
-
#initialize(source) ⇒ Enum
constructor
A new instance of Enum.
- #intToVal ⇒ Object
- #to_schema(buffer) ⇒ Object
Methods inherited from BaseType
Constructor Details
#initialize(source) ⇒ Enum
Returns a new instance of Enum.
796 797 798 799 800 801 802 803 804 805 806 807 |
# File 'lib/types.rb', line 796 def initialize(source) @intToVal = {} @valToInt = {} raise BareException.new("Enum must initialized with a hash from integers to anything") if !source.is_a?(Hash) raise BareException.new("Enum must have unique positive integer assignments") if Set.new(source.keys).size != source.keys.size raise EnumValueError.new("Enum must have unique values") if source.values.to_set.size != source.values.size source.each do |k, v| raise("Enum keys must be positive integers") if k < 0 @intToVal[k.to_i] = v @valToInt[v] = k.to_i end end |
Instance Method Details
#==(otherType) ⇒ Object
774 775 776 777 778 779 780 |
# File 'lib/types.rb', line 774 def ==(otherType) return false unless otherType.class == BareTypes::Enum @intToVal.each do |int, val| return false unless otherType.intToVal[int] == val end return true end |
#decode(msg) ⇒ Object
815 816 817 818 |
# File 'lib/types.rb', line 815 def decode(msg) value, rest = BareTypes::Uint.new.decode(msg) return @intToVal[value], rest end |
#encode(msg, buffer) ⇒ Object
809 810 811 812 813 |
# File 'lib/types.rb', line 809 def encode(msg, buffer) raise SchemaMismatch.new("#{msg.inspect} is not part of this enum: #{@intToVal}") if !@valToInt.keys.include?(msg) integerRep = @valToInt[msg] BareTypes::Uint.new.encode(integerRep, buffer) end |
#finalize_references(schema) ⇒ Object
790 |
# File 'lib/types.rb', line 790 def finalize_references(schema) end |
#intToVal ⇒ Object
792 793 794 |
# File 'lib/types.rb', line 792 def intToVal @intToVal end |
#to_schema(buffer) ⇒ Object
782 783 784 785 786 787 788 |
# File 'lib/types.rb', line 782 def to_schema(buffer) buffer << "{\n" @valToInt.each do |name, repr| buffer << " #{name} = #{repr}\n" end buffer << "}" end |