Class: Blender3d::Structure

Inherits:
Object
  • Object
show all
Includes:
Serializer
Defined in:
lib/blender-3d/structure.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader = nil) ⇒ Structure

Returns a new instance of Structure.



9
10
11
# File 'lib/blender-3d/structure.rb', line 9

def initialize(reader = nil)
  deserialize(reader) if reader
end

Class Attribute Details

.definitionObject (readonly)

Returns the value of attribute definition.



4
5
6
# File 'lib/blender-3d/structure.rb', line 4

def definition
  @definition
end

Instance Method Details

#deserialize(reader) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/blender-3d/structure.rb', line 13

def deserialize(reader)
  self.class.definition.fields.each do |field|
    value = field.type.read(reader)
    instance_variable_set "@#{field.name}", value
  end
  self
end

#to_xmlObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/blender-3d/structure.rb', line 21

def to_xml
  vars = instance_variables.map do |n|
    value = instance_variable_get(n)
    REXML::Element.new(n[1..-1]).tap { |e| e << value_to_xml(value) }
  end

  REXML::Element.new(self.class.basename).tap do |e|
    vars.each { |v| e.add_element v }
  end
end