Class: Dependencytree::ClassModel

Inherits:
Object
  • Object
show all
Defined in:
lib/dependencytree/classmodel.rb

Overview

Model for classes and modules

Constant Summary collapse

@@generator =
SecureRandom

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, path, name) ⇒ ClassModel

type: :class or :module path: the filesystem path the parsed class was found in module_name: eventual module name or :anonymous class_name: the class name



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dependencytree/classmodel.rb', line 25

def initialize(type, path, name)
  # unique uuid for reference
  @uuid = @@generator.uuid
  # :module or :class
  @type = type
  # filesystem path of (first) definition
  @path = path
  # local name (without enclosing modules)
  @name = name
  # list of names of methods
  @method_names = []
  # list of names of constants
  @constant_names = []
  # list of (unresolved) references as arrays
  @references = []

  # no parent by default (will be set later)
  @parent = nil

  @resolved_references = []
  @unresolved_references = []
end

Instance Attribute Details

#constant_namesObject (readonly)

Returns the value of attribute constant_names.



13
14
15
# File 'lib/dependencytree/classmodel.rb', line 13

def constant_names
  @constant_names
end

#method_namesObject (readonly)

Returns the value of attribute method_names.



14
15
16
# File 'lib/dependencytree/classmodel.rb', line 14

def method_names
  @method_names
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/dependencytree/classmodel.rb', line 10

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/dependencytree/classmodel.rb', line 12

def path
  @path
end

#referencesObject (readonly)

Returns the value of attribute references.



11
12
13
# File 'lib/dependencytree/classmodel.rb', line 11

def references
  @references
end

#resolved_referencesObject (readonly)

Returns the value of attribute resolved_references.



16
17
18
# File 'lib/dependencytree/classmodel.rb', line 16

def resolved_references
  @resolved_references
end

#unresolved_referencesObject (readonly)

Returns the value of attribute unresolved_references.



17
18
19
# File 'lib/dependencytree/classmodel.rb', line 17

def unresolved_references
  @unresolved_references
end

#uuidObject (readonly)

Returns the value of attribute uuid.



9
10
11
# File 'lib/dependencytree/classmodel.rb', line 9

def uuid
  @uuid
end

Class Method Details

.generator=(generator) ⇒ Object



48
49
50
# File 'lib/dependencytree/classmodel.rb', line 48

def self.generator=(generator)
  @@generator = generator
end

Instance Method Details

#add_constant(constant_name) ⇒ Object

Adds a constant by its name to the list of constants.



104
105
106
# File 'lib/dependencytree/classmodel.rb', line 104

def add_constant(constant_name)
  @constant_names << constant_name.to_sym
end

#add_method(method_name) ⇒ Object

Adds a method by its name to the list of methods.



99
100
101
# File 'lib/dependencytree/classmodel.rb', line 99

def add_method(method_name)
  @method_names << method_name.to_sym
end

#add_reference(ref) ⇒ Object

Adds a reference by its array-style full name.



109
110
111
# File 'lib/dependencytree/classmodel.rb', line 109

def add_reference(ref)
  @references << ref
end

#as_json(*a) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dependencytree/classmodel.rb', line 74

def as_json(*a)
  result = {
    "uuid" => @uuid,
    "type" => @type,
    "path" => @path,
    "name" => @name,
    "full_name" => full_name,
    "methods" => @method_names,
    "constants" => @constant_names,
    "refs" => @references.uniq.each_with_object([])  { |clazz, arr| arr<<clazz.join("::") },
    "resolved_refs" => @resolved_references.uniq,
    "unresolved_refs" => @unresolved_references.uniq.each_with_object([])  { |clazz, arr| arr<<clazz.join("::") }
  }

  if @parent
    result["parent_uuid"] = @parent.uuid
  end
  result
end

#full_nameObject

Gets the full name of the class/module.

Returns:

  • the full name, for example “ModuleA::ModuleB::ClassA”



65
66
67
# File 'lib/dependencytree/classmodel.rb', line 65

def full_name
  full_name_array.join("::")
end

#full_name_arrayObject

Gets the full name of the class/module as an array.

Returns:

  • the full name, for example [“ModuleA”,“ModuleB”,“ClassA”]



54
55
56
57
58
59
60
61
# File 'lib/dependencytree/classmodel.rb', line 54

def full_name_array
  if @parent
    result = @parent.full_name_array << @name.to_s
  else
    result = [ @name.to_s ]
  end
  result
end

#set_parent(parent) ⇒ Object

Raises:

  • (ArgumentError)


69
70
71
72
# File 'lib/dependencytree/classmodel.rb', line 69

def set_parent(parent)
  raise ArgumentError, "Self parent reference for name #{@name}" if parent == self
  @parent = parent
end

#to_json(*a) ⇒ Object



94
95
96
# File 'lib/dependencytree/classmodel.rb', line 94

def to_json(*a)
    as_json.to_json(*a)
end