Class: PDoc::Models::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pdoc/models.rb,
lib/pdoc/models/base.rb

Direct Known Subclasses

Argument, Entity, Section, Signature

Constant Summary collapse

@@subclasses_by_type =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



17
18
19
# File 'lib/pdoc/models/base.rb', line 17

def initialize(attributes = {})
  attributes.each { |k, v| instance_variable_set("@#{k}", v) }
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



11
12
13
# File 'lib/pdoc/models/base.rb', line 11

def description
  @description
end

#fileObject (readonly)

Returns the value of attribute file.



14
15
16
# File 'lib/pdoc/models/base.rb', line 14

def file
  @file
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/pdoc/models/base.rb', line 12

def id
  @id
end

#line_numberObject (readonly)

Returns the value of attribute line_number.



15
16
17
# File 'lib/pdoc/models/base.rb', line 15

def line_number
  @line_number
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/pdoc/models/base.rb', line 10

def parent
  @parent
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/pdoc/models/base.rb', line 13

def type
  @type
end

Class Method Details

.instantiate(attributes) ⇒ Object



6
7
8
# File 'lib/pdoc/models/base.rb', line 6

def self.instantiate(attributes)
  @@subclasses_by_type[attributes['type']].new(attributes)
end

Instance Method Details

#ancestor_of?(obj) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/pdoc/models/base.rb', line 42

def ancestor_of?(obj)
  while obj = obj.parent
    return true if obj == self
  end 
  false
end

#deprecated?Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/pdoc/models/base.rb', line 29

def deprecated?
  return !!@deprecated if @deprecated
  parent.respond_to?(:deprecated?) ? parent.deprecated? : false
end

#descendant_of?(obj) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/pdoc/models/base.rb', line 49

def descendant_of?(obj)
  obj.ancestor_of?(self)
end

#doc_hrefObject



53
54
55
56
# File 'lib/pdoc/models/base.rb', line 53

def doc_href
  proc = Models.doc_href
  @doc_href ||= proc ? proc.call(self) : nil
end

#full_nameObject



34
35
36
# File 'lib/pdoc/models/base.rb', line 34

def full_name
  @id
end

#inspectObject



102
103
104
# File 'lib/pdoc/models/base.rb', line 102

def inspect
  "#<#{self.class} #{id}>"
end

#nameObject



38
39
40
# File 'lib/pdoc/models/base.rb', line 38

def name
  @name ||= @id.match(/[\w\d\$]+$/)[0]
end

#normalized_nameObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pdoc/models/base.rb', line 87

def normalized_name
  @normalized_name ||= name.gsub(/(^\$+$)|(^\$+)|(\$+$)|(\$+)/) do |m|
    dollar = Array.new(m.length, 'dollar').join('-')
    if $1
      dollar
    elsif $2
      "#{dollar}-"
    elsif $3
      "-#{dollar}"
    elsif $4
      "-#{dollar}-"
    end
  end
end

#register_on(registry) ⇒ Object



21
22
23
# File 'lib/pdoc/models/base.rb', line 21

def register_on(registry)
  registry[id] = self
end

#short_descriptionObject



25
26
27
# File 'lib/pdoc/models/base.rb', line 25

def short_description
  @short_description ||= description.split(/\n\n/).first
end

#to_hashObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pdoc/models/base.rb', line 71

def to_hash
  {
    :deprecated => deprecated?,
    :id => id,
    :type => type,
    :description => description,
    :short_description => short_description,
    :parent => parent.is_a?(Models::Root) ? nil : parent.id,
    :doc_href => doc_href
  }
end

#to_json(*args) ⇒ Object



83
84
85
# File 'lib/pdoc/models/base.rb', line 83

def to_json(*args)
  to_hash.to_json(*args)
end

#url(separator = '/') ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pdoc/models/base.rb', line 58

def url(separator = '/')
  result = []
  obj = self
  begin
    result << obj.normalized_name
    if obj.is_a?(Models::InstanceMethod) || obj.is_a?(Models::InstanceProperty)
      result << 'prototype'
    end
    obj = obj.parent
  end until obj.is_a?(Models::Root)
  result.reverse.join(separator)
end