Class: Rigor::ModuleGraph::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/rigor/module_graph/node.rb

Overview

A piece of node metadata extracted from a source file.

Three flavours, distinguished by kind:

  • class / module — a constant declaration. Carries name, path, line, column. owner/visibility/ access are nil.

  • instance_method / class_method — a method definition. Carries name, owner (the enclosing class/module), visibility, path, line, column.

  • attribute — an attr_reader / attr_writer / attr_accessor symbol. Carries name, owner, visibility, access, path, line, column.

Constant Summary collapse

KINDS =
NODE_KINDS
VISIBILITIES =
NODE_VISIBILITIES
ACCESSES =
NODE_ACCESSES

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(kind:, name:, owner: nil, path: nil, line: nil, column: nil, visibility: nil, access: nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rigor/module_graph/node.rb', line 44

def self.build(kind:, name:, owner: nil, path: nil, line: nil, column: nil,
               visibility: nil, access: nil)
  new(
    kind: validate_kind!(kind),
    name: name.to_s.freeze,
    owner: owner && owner.to_s.freeze,
    path: path,
    line: line,
    column: column,
    visibility: visibility && validate_visibility!(visibility),
    access: access && validate_access!(access)
  )
end

.validate_access!(access) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


72
73
74
75
76
77
# File 'lib/rigor/module_graph/node.rb', line 72

def self.validate_access!(access) # :nodoc:
  access = access.to_s
  return access if ACCESSES.include?(access)

  raise ArgumentError, "unknown access #{access.inspect}; expected one of #{ACCESSES.inspect}"
end

.validate_kind!(kind) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


58
59
60
61
62
63
# File 'lib/rigor/module_graph/node.rb', line 58

def self.validate_kind!(kind) # :nodoc:
  kind = kind.to_s
  return kind if KINDS.include?(kind)

  raise ArgumentError, "unknown node kind #{kind.inspect}; expected one of #{KINDS.inspect}"
end

.validate_visibility!(visibility) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


65
66
67
68
69
70
# File 'lib/rigor/module_graph/node.rb', line 65

def self.validate_visibility!(visibility) # :nodoc:
  visibility = visibility.to_s
  return visibility if VISIBILITIES.include?(visibility)

  raise ArgumentError, "unknown visibility #{visibility.inspect}; expected one of #{VISIBILITIES.inspect}"
end

Instance Method Details

#dedup_keyObject

Key used to dedupe node rows. Two declarations of the same method on the same owner collapse to one row; class re-opens collapse to one class node.



104
105
106
# File 'lib/rigor/module_graph/node.rb', line 104

def dedup_key
  [kind, owner, name]
end

#to_hObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/rigor/module_graph/node.rb', line 79

def to_h
  h = { "kind" => kind, "name" => name }
  h["owner"] = owner if owner
  h["path"] = path if path
  h["line"] = line if line
  h["column"] = column if column
  h["visibility"] = visibility if visibility
  h["access"] = access if access
  h
end

#to_message_payloadObject

The payload embedded in the plugin’s :info diagnostic message. Position is intentionally absent — the diagnostic row carries path/line/column on its own.



93
94
95
96
97
98
99
# File 'lib/rigor/module_graph/node.rb', line 93

def to_message_payload
  h = { "kind" => kind, "name" => name }
  h["owner"] = owner if owner
  h["visibility"] = visibility if visibility
  h["access"] = access if access
  h
end