Class: TLV::TLV::Raw

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

Instance Attribute Summary

Attributes inherited from Field

#display_name, #length, #name

Instance Method Summary collapse

Constructor Details

#initialize(clazz, desc = nil, name = nil, len = 0) ⇒ Raw

Returns a new instance of Raw.



4
5
6
7
# File 'lib/tlv/raw.rb', line 4

def initialize clazz, desc=nil, name=nil, len=0
  desc ||= "Value" 
  super
end

Instance Method Details

#define_accessor(clazz) ⇒ Object



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

def define_accessor clazz
  super
  name = @name
  clazz.instance_eval{
    define_method("#{name}="){|val|
      val ||= ""
      raise("must be a String #{val}")  unless val.is_a?(String)
      self.instance_variable_set("@#{name}", val)
    }
    
    define_method("#{name}") {
      self.instance_variable_get("@#{name}") || ""
    }
  }
end

#parse(tlv, bytes, length) ⇒ Object



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

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