Class: Analyst::Entities::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/analyst/entities/entity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ast, parent) ⇒ Entity

Returns a new instance of Entity.



18
19
20
21
# File 'lib/analyst/entities/entity.rb', line 18

def initialize(ast, parent)
  @parent = parent
  @ast = ast
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



8
9
10
# File 'lib/analyst/entities/entity.rb', line 8

def ast
  @ast
end

#parentObject (readonly)

Returns the value of attribute parent.



8
9
10
# File 'lib/analyst/entities/entity.rb', line 8

def parent
  @parent
end

Class Method Details

.handles_node(type) ⇒ Object



10
11
12
# File 'lib/analyst/entities/entity.rb', line 10

def self.handles_node(type)
  Analyst::Processor.register_processor(type, self)
end

.process(ast, parent) ⇒ Object



14
15
16
# File 'lib/analyst/entities/entity.rb', line 14

def self.process(ast, parent)
  new(ast, parent)
end

Instance Method Details

#classesObject



23
24
25
26
27
28
29
# File 'lib/analyst/entities/entity.rb', line 23

def classes
  @classes ||= begin
    nested_classes = top_level_classes.map(&:classes).flatten
    namespaced_classes = top_level_modules.map(&:classes).flatten
    top_level_classes + nested_classes + namespaced_classes
  end
end

#conditionalsObject



81
82
83
# File 'lib/analyst/entities/entity.rb', line 81

def conditionals
  @conditionals ||= contents_of_type(Entities::Conditional)
end

#constant_assignmentsObject



46
47
48
# File 'lib/analyst/entities/entity.rb', line 46

def constant_assignments
  @constant_assignments ||= top_level_constant_assignments + contents.map(&:constant_assignments).flatten
end

#constantsObject



42
43
44
# File 'lib/analyst/entities/entity.rb', line 42

def constants
  @constants ||= top_level_constants + contents.map(&:constants).flatten
end

#file_pathObject



97
98
99
# File 'lib/analyst/entities/entity.rb', line 97

def file_path
  ast.location.expression.source_buffer.name
end

#full_nameObject



109
110
111
# File 'lib/analyst/entities/entity.rb', line 109

def full_name
  throw "Subclass #{self.class.name} must implement #full_name"
end

#hashesObject



89
90
91
# File 'lib/analyst/entities/entity.rb', line 89

def hashes
  @hashes ||= contents_of_type(Entities::Hash)
end

#inspectObject



113
114
115
116
117
# File 'lib/analyst/entities/entity.rb', line 113

def inspect
  "\#<#{self.class} location=#{location} full_name=#{full_name}>"
rescue
  "\#<#{self.class} location=#{location}>"
end

#line_numberObject



101
102
103
# File 'lib/analyst/entities/entity.rb', line 101

def line_number
  ast.location.line
end

#locationObject



93
94
95
# File 'lib/analyst/entities/entity.rb', line 93

def location
  "#{file_path}:#{line_number}"
end

#method_callsObject



66
67
68
# File 'lib/analyst/entities/entity.rb', line 66

def method_calls
  @method_calls ||= contents_of_type(Entities::MethodCall)
end

#methodsObject

TODO: rethink the different kinds of Methods. there’s really only one kind of Method, right?? ref: www.devalot.com/articles/2008/09/ruby-singleton



73
74
75
76
77
78
79
# File 'lib/analyst/entities/entity.rb', line 73

def methods
  return @methods if defined?(@methods)
  @methods = contents_of_type(Entities::InstanceMethod)
  @methods << contents_of_type(Entities::ClassMethod)
  @methods << contents_of_type(Entities::SingletonMethod)
  @methods.flatten!
end

#modulesObject



35
36
37
38
39
40
# File 'lib/analyst/entities/entity.rb', line 35

def modules
  @modules ||= begin
    nested_modules = top_level_modules.map(&:modules).flatten
    top_level_modules + nested_modules
  end
end

#sourceObject



105
106
107
# File 'lib/analyst/entities/entity.rb', line 105

def source
  ast.location.expression.source
end

#stringsObject



31
32
33
# File 'lib/analyst/entities/entity.rb', line 31

def strings
  @strings ||= contents_of_type(Entities::String)
end

#top_level_classesObject



62
63
64
# File 'lib/analyst/entities/entity.rb', line 62

def top_level_classes
  @top_level_classes ||= contents_of_type(Entities::Class)
end

#top_level_constant_assignmentsObject



50
51
52
# File 'lib/analyst/entities/entity.rb', line 50

def top_level_constant_assignments
  @top_level_constant_assignments ||= contents_of_type(Entities::ConstantAssignment)
end

#top_level_constantsObject



54
55
56
# File 'lib/analyst/entities/entity.rb', line 54

def top_level_constants
  @top_level_constants ||= contents_of_type(Entities::Constant)
end

#top_level_modulesObject



58
59
60
# File 'lib/analyst/entities/entity.rb', line 58

def top_level_modules
  @top_level_modules ||= contents_of_type(Entities::Module)
end

#variablesObject



85
86
87
# File 'lib/analyst/entities/entity.rb', line 85

def variables
  @variables ||= contents_of_type(Entities::VariableAssignment)
end