Class: CallMap::Definition

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

Overview

A single class / module / method definition.

This is a plain value object and must NOT depend on the parser (Prism). Building a Definition from an AST is the job of the parser boundary class, so that parser-specific code stays in one place.

Constant Summary collapse

KINDS =
%i[class module instance_method class_method].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind:, name:, path:, line:, owner: nil, lexical_nesting: nil, superclass: nil, visibility: :public) ⇒ Definition

Returns a new instance of Definition.

Parameters:

  • kind (Symbol)

    one of KINDS

  • name (String)

    method name, or qualified constant name for class/module

  • path (String)

    file path where the definition is written

  • line (Integer)

    starting line number of the definition

  • owner (String, nil) (defaults to: nil)

    qualified constant name of the enclosing class/module (for methods)

  • lexical_nesting (Array<String>, nil) (defaults to: nil)

    lexical scope stack at the definition site, outermost first

  • superclass (String, nil) (defaults to: nil)

    superclass constant name as written (for :class definitions)

  • visibility (Symbol) (defaults to: :public)

    :public / :private / :protected (instance methods)

Raises:

  • (ArgumentError)


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

def initialize(kind:, name:, path:, line:, owner: nil, lexical_nesting: nil, superclass: nil, visibility: :public)
  raise ArgumentError, "unknown kind: #{kind}" unless KINDS.include?(kind)

  @kind = kind
  @name = name
  @owner = owner
  @path = path
  @line = line
  @lexical_nesting = lexical_nesting
  @superclass = superclass
  @visibility = visibility
  # Free-form metadata; :comments holds the method's leading comment lines.
  @metadata = {}
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



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

def kind
  @kind
end

#lexical_nestingObject (readonly)

Returns the value of attribute lexical_nesting.



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

def lexical_nesting
  @lexical_nesting
end

#lineObject (readonly)

Returns the value of attribute line.



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

def line
  @line
end

#metadataObject

Returns the value of attribute metadata.



42
43
44
# File 'lib/call_map/definition.rb', line 42

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#ownerObject (readonly)

Returns the value of attribute owner.



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

def owner
  @owner
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#superclassObject (readonly)

Returns the value of attribute superclass.



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

def superclass
  @superclass
end

#visibilityObject

Writable so private :foo-style post-declarations can adjust it.



37
38
39
# File 'lib/call_map/definition.rb', line 37

def visibility
  @visibility
end

Instance Method Details

#class_or_module?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/call_map/definition.rb', line 49

def class_or_module?
  %i[class module].include?(kind)
end

#commentsObject

Leading comment lines attached at collection time ("#" stripped).



45
46
47
# File 'lib/call_map/definition.rb', line 45

def comments
  [:comments] || []
end

#method?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/call_map/definition.rb', line 53

def method?
  %i[instance_method class_method].include?(kind)
end

#public_method?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/call_map/definition.rb', line 39

def public_method?
  visibility == :public
end

#qualified_nameObject

Human-readable qualified name used for lookups and tree output.

  • class / module: "Admin::ReportsController"
  • instance method: "OrdersController#destroy"
  • class method: "OrderDeleteService.execute"


62
63
64
65
66
67
68
# File 'lib/call_map/definition.rb', line 62

def qualified_name
  case kind
  when :instance_method then "#{owner}##{name}"
  when :class_method then "#{owner}.#{name}"
  else name
  end
end