Class: Decode::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/decode/definition.rb

Overview

A symbol with attached documentation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, parent: nil, language: parent&.language, comments: nil, visibility: :public, source: parent&.source) ⇒ Definition

Initialize the symbol.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/decode/definition.rb', line 18

def initialize(path, parent: nil, language: parent&.language, comments: nil, visibility: :public, source: parent&.source)
  @path = Array(path).map(&:to_sym)
  
  @parent = parent
  @language = language
  @source = source
  
  @comments = comments
  @visibility = visibility
  @documentation = nil
  
  @full_path = nil
  @qualified_name = nil
  @nested_name = nil
end

Instance Attribute Details

#commentsObject (readonly)

Returns the value of attribute comments.



76
77
78
# File 'lib/decode/definition.rb', line 76

def comments
  @comments
end

#languageObject (readonly)

Returns the value of attribute language.



70
71
72
# File 'lib/decode/definition.rb', line 70

def language
  @language
end

#parentObject (readonly)

Returns the value of attribute parent.



67
68
69
# File 'lib/decode/definition.rb', line 67

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



48
49
50
# File 'lib/decode/definition.rb', line 48

def path
  @path
end

#sourceObject (readonly)

Returns the value of attribute source.



73
74
75
# File 'lib/decode/definition.rb', line 73

def source
  @source
end

#The comment lines which directly preceeded the definition.(commentlineswhichdirectlypreceededthedefinition.) ⇒ Object (readonly)



76
# File 'lib/decode/definition.rb', line 76

attr :comments

#The language the symbol is defined within.(languagethesymbolisdefinedwithin.) ⇒ Object (readonly)



70
# File 'lib/decode/definition.rb', line 70

attr :language

#The path to the definition, relative to the parent.(pathtothedefinition, relativetotheparent.) ⇒ Object (readonly)



48
# File 'lib/decode/definition.rb', line 48

attr :path

#The source file containing this definition.(sourcefilecontainingthisdefinition.) ⇒ Object (readonly)



73
# File 'lib/decode/definition.rb', line 73

attr :source

#visibilityObject

The visibility of the definition.



194
195
196
# File 'lib/decode/definition.rb', line 194

def visibility
  @visibility
end

Instance Method Details

#:public, :private, :protected=(: public, : private, : protected=(value)) ⇒ Object

The visibility of the definition.



194
# File 'lib/decode/definition.rb', line 194

attr_accessor :visibility

#container?Boolean

Whether this definition can contain nested definitions.

Returns:

  • (Boolean)


165
166
167
# File 'lib/decode/definition.rb', line 165

def container?
  false
end

#convert(kind) ⇒ Object

Convert this definition into another kind of definition.

Raises:

  • (ArgumentError)


122
123
124
# File 'lib/decode/definition.rb', line 122

def convert(kind)
  raise ArgumentError, "Unable to convert #{self} into #{kind}!"
end

#coverage_relevant?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/decode/definition.rb', line 86

def coverage_relevant?
  self.public?
end

#documentationObject

Structured access to the definitions comments.



179
180
181
182
183
# File 'lib/decode/definition.rb', line 179

def documentation
  if comments = @comments and comments.any?
    @documentation ||= Documentation.new(comments, @language)
  end
end

#documented?Boolean

Whether the definition has documentation.

Returns:

  • (Boolean)


92
93
94
# File 'lib/decode/definition.rb', line 92

def documented?
  @comments&.any? || false
end

#full_pathObject Also known as: lexical_path

The full path to the definition.



52
53
54
55
56
57
58
59
60
# File 'lib/decode/definition.rb', line 52

def full_path
  @full_path ||= begin
    if parent = @parent
      parent.full_path + @path
    else
      @path
    end
  end
end

#inspectObject Also known as: to_s

Generate a debug representation of the definition.



35
36
37
# File 'lib/decode/definition.rb', line 35

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

#locationObject

The location of the definition.



188
189
190
# File 'lib/decode/definition.rb', line 188

def location
  nil
end

#long_formObject

A long form of the definition. e.g. ‘def initialize(kind, name, comments, **options)`.



137
138
139
# File 'lib/decode/definition.rb', line 137

def long_form
  self.short_form
end

#multiline?Boolean

Whether the definition spans multiple lines.

Returns:

  • (Boolean)


152
153
154
# File 'lib/decode/definition.rb', line 152

def multiline?
  false
end

#nameObject



43
44
45
# File 'lib/decode/definition.rb', line 43

def name
  @path.last
end

#nested?Boolean

Whether this represents a single entity to be documented (along with it’s contents).

Returns:

  • (Boolean)


172
173
174
# File 'lib/decode/definition.rb', line 172

def nested?
  container?
end

#nested_nameObject



109
110
111
# File 'lib/decode/definition.rb', line 109

def nested_name
  @nested_name ||= "#{@path.join("::")}"
end

#public?Boolean

Whether the definition is considered part of the public interface. This is used to determine whether the definition should be documented for coverage purposes.

Returns:

  • (Boolean)


81
82
83
# File 'lib/decode/definition.rb', line 81

def public?
  true
end

#qualified_formObject

A long form which uses the qualified name if possible. Defaults to #long_form.



145
146
147
# File 'lib/decode/definition.rb', line 145

def qualified_form
  self.long_form
end

#qualified_nameObject

The qualified name is an absolute name which includes any and all namespacing.



98
99
100
101
102
103
104
105
106
# File 'lib/decode/definition.rb', line 98

def qualified_name
  @qualified_name ||= begin
    if parent = @parent
      [parent.qualified_name, self.nested_name].join("::")
    else
      self.nested_name
    end
  end
end

#short_formObject

A short form of the definition. e.g. ‘def short_form`.



130
131
# File 'lib/decode/definition.rb', line 130

def short_form
end

#start_with?(prefix) ⇒ Boolean

Does the definition name match the specified prefix?

Returns:

  • (Boolean)


116
117
118
# File 'lib/decode/definition.rb', line 116

def start_with?(prefix)
  self.nested_name.start_with?(prefix)
end

#textObject

The full text of the definition.



159
160
# File 'lib/decode/definition.rb', line 159

def text
end

#The parent definition, defining lexical scope.=(parentdefinition, defininglexicalscope. = (value)) ⇒ Object



67
# File 'lib/decode/definition.rb', line 67

attr :parent

#The symbol name e.g. `:Decode`.=(symbolnamee.g.`: Decode`.=(value)) ⇒ Object



43
44
45
# File 'lib/decode/definition.rb', line 43

def name
  @path.last
end