Class: PDoc::Treemaker

Inherits:
Object
  • Object
show all
Includes:
Models
Defined in:
lib/pdoc/treemaker.rb

Instance Method Summary collapse

Constructor Details

#initialize(doc_fragments = []) ⇒ Treemaker

Returns a new instance of Treemaker.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pdoc/treemaker.rb', line 5

def initialize(doc_fragments = [])
  methodized = []
  doc_fragments.each do |attributes|
    if attributes['methodized']
      dups = attributes.dup
      dups['id'] = methodize_id(dups['id'])
      dups['type'] = 'instance method'
      if dups['signatures']
        dups['signatures'] = dups['signatures'].map do |s|
          {
            'signature' => methodize_signature(s['signature']),
            'return_value' => s['return_value']
          }
        end
      end
      if dups['alias_of']
        dups['alias_of'] = methodize_id(dups['alias_of'])
      end
      methodized << dups
      i = instantiate_from(dups)
      c = instantiate_from(attributes)
      i.functionalized_self = c
      c.methodized_self = i
    else
      instantiate_from(attributes)
    end
  end

  doc_fragments.concat(methodized).each do |attributes|
    if parent_id = attributes['parent_id']
      parent = root.find(parent_id)
      raise "Undocumented object: #{parent_id}." unless parent
    else
      parent = root
    end
    object = root.find(attributes['id'])
    object.parent = parent
    object.attach_to_parent(parent)
    
    if superclass_id = attributes['superclass_id']
      superclass = root.find(superclass_id)
      raise "Undocumented object: #{superclass_id}." unless superclass
      object.superclass = superclass
      superclass.subclasses << object
    end
    
    if included = attributes['included']
      included.each do |id|
        mixin = root.find(id)
        raise "Undocumented object: #{id}." unless mixin
        object.included_mixins << mixin
      end
    end
    
    if alias_of_id = attributes['alias_of']
      alias_of = root.find(alias_of_id)
      raise "Undocumented object: #{alias_of_id}." unless alias_of
      object.alias = alias_of
      alias_of.aliases << object
    end
  end
end

Instance Method Details

#instantiate_from(attributes) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/pdoc/treemaker.rb', line 68

def instantiate_from(attributes)
  arguments = attributes.delete('arguments')
  signatures = attributes.delete('signatures')
  object = Base.instantiate(attributes)
  arguments.each { |a| Argument.new(a).attach_to_parent(object) } if arguments
  signatures.each { |s| Signature.new(s).attach_to_parent(object) } if signatures
  object.register_on(root.registry)
end

#methodize_id(id) ⇒ Object



86
87
88
# File 'lib/pdoc/treemaker.rb', line 86

def methodize_id(id)
  id.sub(/\.([^\.]+)$/) { "##{$1}" }
end

#methodize_signature(sig) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/pdoc/treemaker.rb', line 77

def methodize_signature(sig)
  sig.sub(/\.([\w\d\$]+)\((.*?)(,\s*|\))/) do
    first_arg = $2.to_s.strip
    prefix = first_arg[-1, 1] == '[' ? '([' : '('
    rest = $3 == ')' ? $3 : ''
    "##{$1}#{prefix}#{rest}"
  end
end

#rootObject



90
91
92
# File 'lib/pdoc/treemaker.rb', line 90

def root
  @root ||= Root.new
end