Class: Rstruct::ContainerType
- Includes:
- Packable
- Defined in:
- lib/rstruct/base_types/container_type.rb
Instance Attribute Summary
Attributes inherited from Type
Instance Method Summary collapse
- #claim_value(vals, obj = nil) ⇒ Object
- #field_names ⇒ Object
- #field_types ⇒ Object
- #format ⇒ Object
- #groupable? ⇒ Boolean
-
#initialize(*args, &block) ⇒ ContainerType
constructor
A new instance of ContainerType.
- #read(raw, obj = nil) ⇒ Object
- #sizeof ⇒ Object
Methods included from Packable
Methods inherited from Type
Constructor Details
#initialize(*args, &block) ⇒ ContainerType
Returns a new instance of ContainerType.
54 55 56 57 |
# File 'lib/rstruct/base_types/container_type.rb', line 54 def initialize(*args, &block) @countainer = true super(*args, &block) end |
Instance Method Details
#claim_value(vals, obj = nil) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rstruct/base_types/container_type.rb', line 100 def claim_value(vals, obj=nil) if @claim_cb @claim_cb.call(vals, obj) else # create our struct container s = instance() # iterate through the fields assigning values in the # container and pass it along with values to each # field's claim_value method. self.fields.do {|f| s[f.name] = f.typ.claim_value(vals,s) } return s end end |
#field_names ⇒ Object
83 84 85 |
# File 'lib/rstruct/base_types/container_type.rb', line 83 def field_names self.fields.map{|f| f.name } end |
#field_types ⇒ Object
87 88 89 |
# File 'lib/rstruct/base_types/container_type.rb', line 87 def field_types self.fields.map{|f| f.typ } end |
#format ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/rstruct/base_types/container_type.rb', line 63 def format self.fields.map do |f| if f.groupable? f.format else return nil end end.join end |
#groupable? ⇒ Boolean
59 60 61 |
# File 'lib/rstruct/base_types/container_type.rb', line 59 def groupable? self.fields.find {|f| not f.groupable? }.nil? end |
#read(raw, obj = nil) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/rstruct/base_types/container_type.rb', line 91 def read(raw, obj=nil) raw = StringIO.new(raw) if raw.is_a?(String) obj = self.instance() fields.each do |f| obj[f.name] = f.typ.read(raw, obj) end return obj end |
#sizeof ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/rstruct/base_types/container_type.rb', line 73 def sizeof self.fields.inject(0) do |s,v| if vs=v.typ.sizeof s+=vs else return nil end end end |