Class: TLV::TLV::B

Inherits:
Field
  • Object
show all
Defined in:
lib/tlv/b.rb

Instance Attribute Summary

Attributes inherited from Field

#display_name, #length, #name

Instance Method Summary collapse

Methods inherited from Field

#initialize

Constructor Details

This class inherits a constructor from TLV::TLV::Field

Instance Method Details

#define_accessor(clazz) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tlv/b.rb', line 5

def define_accessor clazz
  super
  name = @name
  len  = @length 
  clazz.instance_eval{
    define_method("#{name}="){|val|
      raise("invalid value nil")        unless val
      raise("must be a String #{val}")  unless val.is_a?(String)
      raise("incorrect length: #{val}") unless (val.length*8) <= len
      self.instance_variable_set("@#{name}", val)
    }
    
    define_method("#{name}") {
      self.instance_variable_get("@#{name}") || "\x00" * (len/8) 
    }
  }
end

#parse(tlv, bytes, _len) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/tlv/b.rb', line 23

def parse tlv, bytes, _len
  val  = bytes[0, length/8]
  rest = bytes[length/8, bytes.length]
  # check val...
  tlv.send("#{name}=", val)
  rest 
end