Module: R2Doc::ContextExtensions

Defined in:
lib/r2doc/context_extensions.rb

Overview

Extensions for the RDoc::Generator::Context / Generators::ContextUser class

Constant Summary collapse

VISIBILITY_VALUE =

Numeric value associated with visibility values for sorting

{ :public=>0, :protected=>1, :private=>2 }

Instance Method Summary collapse

Instance Method Details

#aliasesObject

Return a list of Hashes for all aliases containing the following keys:

:old_name

The old name

:new_name

The new name

:description

Alias description markup



61
62
63
# File 'lib/r2doc/context_extensions.rb', line 61

def aliases
  @context.aliases.sort{|a,b| a.old_name<=>b.old_name}.collect{|al| {:old_name=>al.old_name, :new_name=>al.new_name, :description=>markup(al.comment, true)}}
end

#all_methodsObject

Returns a collection of HtmlMethod objects for all methods in this context



87
88
89
90
91
# File 'lib/r2doc/context_extensions.rb', line 87

def all_methods
  collect_methods unless @methods
  @all_methods = sort_members(@methods) unless @all_methods
  @all_methods
end

#attributesObject

Return a list of Hashes for all documentable attributes containing the following keys:

:name

The attribute name

:visibility

:public, :protected, or :private

:rw

'r' or 'rw'

:description

Attribute description markup



75
76
77
78
# File 'lib/r2doc/context_extensions.rb', line 75

def attributes
  attrs = sort_members(@context.attributes).find_all{|a| @options.show_all || a.visibility == :public || a.visibility == :protected}
  attrs.collect{|a| {:name=>a.name, :visibility=>a.visibility, :rw=>a.rw, :description=>markup(a.comment, true)}}
end

#class_methodsObject

Return a list of Hashes for all documentable class methods containing the following keys:

:name

The method name

:visibility

:public:, :protected, or :private

:params

Method call signature markup (does not include method name)

:callseq

Markup documenting alternative ways to call the method (should take precedence over name and params when present)

:url

The relative URL to the method from the documentation root (e.g. 'classes/Foo.html#bar')

:anchor

The name of the anchor for this method (e.g. 'bar')

:description

Method description markup



101
102
103
# File 'lib/r2doc/context_extensions.rb', line 101

def class_methods
  all_methods().find_all{|m| m.singleton && (@options.show_all || m.visibility == :public || m.visibility == :protected)}.collect{|m| method_hash(m)}
end

#classesObject

The collection of classes contained within this context



129
130
131
132
# File 'lib/r2doc/context_extensions.rb', line 129

def classes
  return @classes if @classes
  @classes = @context.classes.sort.find_all{|c| c.document_self}.collect{|c| R2Doc.all_references[c.full_name]}
end

#constantsObject

Return a list of Hashes for all constants containing the following keys:

:name

The constant name

:value

The value of the constant

:description

Constant description markup



48
49
50
# File 'lib/r2doc/context_extensions.rb', line 48

def constants
  @context.constants.sort{|a,b| a.name<=>b.name}.collect{|c| {:name=>c.name, :value=>c.value, :description=>markup(c.comment, true)}}
end

#description(temporaryPath = nil) ⇒ Object

Return the description markup for this context



36
37
38
39
40
41
42
# File 'lib/r2doc/context_extensions.rb', line 36

def description(temporaryPath = nil)
  oldPath = self.path if (self.respond_to?(:path) && (!temporaryPath.nil?))
  self.format_path = temporaryPath unless temporaryPath.nil?
  d = markup(@context.comment)
  self.format_path = oldPath if (self.respond_to?(:path) && (!temporaryPath.nil?))
  d
end

#has_aliases?Boolean

Return true if this context contains aliases

Returns:

  • (Boolean)


66
67
68
# File 'lib/r2doc/context_extensions.rb', line 66

def has_aliases?
  @context.aliases.length > 0
end

#has_attributes?Boolean

Return true if this context contains attributes

Returns:

  • (Boolean)


81
82
83
84
# File 'lib/r2doc/context_extensions.rb', line 81

def has_attributes?
  a = @context.attributes.find{|a| @options.show_all || a.visibility == :public || a.visibility == :protected}
  a ? true : false
end

#has_class_methods?Boolean

Returns true if the context contains class (singleton) methods

Returns:

  • (Boolean)


106
107
108
109
# File 'lib/r2doc/context_extensions.rb', line 106

def has_class_methods?
  m = all_methods().find{|m| m.singleton && (@options.show_all || m.visibility == :public || m.visibility == :protected)}
  m ? true : false
end

#has_classes?Boolean

Returns true if this context contains classes

Returns:

  • (Boolean)


135
136
137
138
# File 'lib/r2doc/context_extensions.rb', line 135

def has_classes?
  c = @context.classes.find{|c| c.document_self}
  c ? true : false
end

#has_classes_or_modules?Boolean

Returns true if this context contains classes or modules

Returns:

  • (Boolean)


147
148
149
# File 'lib/r2doc/context_extensions.rb', line 147

def has_classes_or_modules?
  has_classes? || has_modules?
end

#has_constants?Boolean

Returns true if the context contains contants

Returns:

  • (Boolean)


53
54
55
# File 'lib/r2doc/context_extensions.rb', line 53

def has_constants?
  @context.constants.length > 0
end

#has_includes?Boolean

Returns true if this context has included modules

Returns:

  • (Boolean)


152
153
154
# File 'lib/r2doc/context_extensions.rb', line 152

def has_includes?
  @context.includes.length > 0
end

#has_instance_methods?Boolean

Returns true if the context contains instance (non-singleton) methods

Returns:

  • (Boolean)


117
118
119
120
# File 'lib/r2doc/context_extensions.rb', line 117

def has_instance_methods?
  m = all_methods().find{|m| (!m.singleton) && (@options.show_all || m.visibility == :public || m.visibility == :protected)}
  m ? true : false
end

#has_modules?Boolean

Returns true if this context contains modules

Returns:

  • (Boolean)


141
142
143
144
# File 'lib/r2doc/context_extensions.rb', line 141

def has_modules?
  m = @context.modules.find{|m| m.document_self}
  m ? true : false
end

#includesObject

The collection of included modules. This returns a hash with the folliwing keys for each module:

:name

The module name

:url

The relative URL to the module from the documentation root



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/r2doc/context_extensions.rb', line 159

def includes
  return @includes if @includes
  @includes = []
  @context.includes.each do |i|
    ref = R2Doc.all_references[i.name]
    ref = @context.find_symbol(i.name)
    ref = ref.viewer if ref
    if ref and ref.document_self
      @includes << {:name=>i.name, :url=>ref.path}
    else
      @includes << {:name=>i.name}
    end
  end
  @includes
end

#instance_methodsObject

Return a list of Hashes for all documentable instance methods. See class_methods for Hash key information.



112
113
114
# File 'lib/r2doc/context_extensions.rb', line 112

def instance_methods
  all_methods().find_all{|m| (!m.singleton) && (@options.show_all || m.visibility == :public || m.visibility == :protected)}.collect{|m| method_hash(m)}
end

#is_method_context?Boolean

Returns true if this object is a method context

Returns:

  • (Boolean)


176
177
178
# File 'lib/r2doc/context_extensions.rb', line 176

def is_method_context?
  false
end

#modulesObject

The collection of modules contained within this context



123
124
125
126
# File 'lib/r2doc/context_extensions.rb', line 123

def modules
  return @modules if @modules
  @modules = @context.modules.sort.find_all{|m| m.document_self}.collect{|m| R2Doc.all_references[m.full_name]}
end

#name_path_to_parentObject

Return an array representing the navigational path to this class in the namespace hierarchy. The first item in the array is the outermost class or module. The class itself is not included in the array, nor is the root of the namespace. So, for a class A::B::C::D, the returned array will contain the A module, the B module, and the C module, in that order. All items in the array are instances of ContextUser



25
26
27
28
29
30
31
32
33
# File 'lib/r2doc/context_extensions.rb', line 25

def name_path_to_parent
  path = []
  p = parent
  while p do
    path.unshift p
    p = p.parent
  end
  path
end

#parentObject

Return the parent ContextUser of this object



12
13
14
15
16
# File 'lib/r2doc/context_extensions.rb', line 12

def parent
  p = (@context.parent.nil? || (@context == @context.parent)) ? nil : R2Doc.all_references[parent_name]
  # guard against pollution of all_references with methods having conflicting names
  (p.respond_to?(:is_method_context?) && p.is_method_context?) ? nil : p
end