Class: Depix::Binary::RdocGenerator

Inherits:
Object
  • Object
show all
Includes:
Fields
Defined in:
lib/depix/binary/rdoc_generator.rb

Overview

Generates a description of the structure in RDoc format

Constant Summary collapse

TPL =
<<eof
= DPX header structure description

DPX metadata gets returned as a Depix::DPX object with nested properties.
  
  meta.file.magic # => "SDPX"

== Metadata structure

%s
eof

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRdocGenerator

Returns a new instance of RdocGenerator.



21
22
23
24
25
26
# File 'lib/depix/binary/rdoc_generator.rb', line 21

def initialize
  @padding =  '  '
  @attr_template = "%s* <tt>%s</tt> %s"
  @struct_template = "%s* <tt>%s</tt> %s:"
  @array_template  = "%s* <tt>%s</tt> %s:"
end

Instance Attribute Details

#attr_templateObject

Returns the value of attribute attr_template.



7
8
9
# File 'lib/depix/binary/rdoc_generator.rb', line 7

def attr_template
  @attr_template
end

#ioObject

Returns the value of attribute io.



7
8
9
# File 'lib/depix/binary/rdoc_generator.rb', line 7

def io
  @io
end

#struct_templateObject

Returns the value of attribute struct_template.



7
8
9
# File 'lib/depix/binary/rdoc_generator.rb', line 7

def struct_template
  @struct_template
end

Instance Method Details

#explain_attr(padding, e) ⇒ Object



56
57
58
59
# File 'lib/depix/binary/rdoc_generator.rb', line 56

def explain_attr(padding, e)
  type_name = e.rtype ? "(#{e.rtype})" : nil
  @io.puts( @attr_template % [padding, e.name, e.explain])
end

#explain_struct(struct, padding = '') ⇒ Object

:nodoc:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/depix/binary/rdoc_generator.rb', line 34

def explain_struct(struct, padding = '') #:nodoc:
  struct.fields.each do | e |
    if e.is_a?(InnerField)
      
      @io.puts( @struct_template % [padding, e.name, e.explain])
      explain_struct(e.rtype, padding + @padding)
    
    elsif e.is_a?(ArrayField)
      
      @io.puts( @array_template % [padding, e.name, e.explain])
      
      inner_struct = e.members[0]
      
      if inner_struct.is_a?(InnerField)
        explain_struct(inner_struct.rtype, padding + @padding)
      end
    else
      explain_attr(padding, e)
    end
  end
end

#get_rdoc_for(struct) ⇒ Object



28
29
30
31
32
# File 'lib/depix/binary/rdoc_generator.rb', line 28

def get_rdoc_for(struct)
  @io = StringIO.new
  explain_struct(struct)
  TPL % @io.string
end