Class: Steep::Index::SourceIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/index/source_index.rb

Defined Under Namespace

Classes: ConstantEntry, MethodEntry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, parent: nil) ⇒ SourceIndex

Returns a new instance of SourceIndex.



96
97
98
99
100
101
102
103
104
105
# File 'lib/steep/index/source_index.rb', line 96

def initialize(source:, parent: nil)
  @source = source
  @parent = parent
  @parent_count = parent&.count

  @count = @parent_count || 0

  @constant_index = {}
  @method_index = {}
end

Instance Attribute Details

#constant_indexObject (readonly)

Returns the value of attribute constant_index.



89
90
91
# File 'lib/steep/index/source_index.rb', line 89

def constant_index
  @constant_index
end

#countObject (readonly)

Returns the value of attribute count.



93
94
95
# File 'lib/steep/index/source_index.rb', line 93

def count
  @count
end

#method_indexObject (readonly)

Returns the value of attribute method_index.



90
91
92
# File 'lib/steep/index/source_index.rb', line 90

def method_index
  @method_index
end

#parentObject (readonly)

Returns the value of attribute parent.



92
93
94
# File 'lib/steep/index/source_index.rb', line 92

def parent
  @parent
end

#parent_countObject (readonly)

Returns the value of attribute parent_count.



94
95
96
# File 'lib/steep/index/source_index.rb', line 94

def parent_count
  @parent_count
end

#sourceObject (readonly)

Returns the value of attribute source.



88
89
90
# File 'lib/steep/index/source_index.rb', line 88

def source
  @source
end

Instance Method Details

#add_definition(constant: nil, method: nil, definition:) ⇒ Object



126
127
128
129
130
# File 'lib/steep/index/source_index.rb', line 126

def add_definition(constant: nil, method: nil, definition:)
  @count += 1
  entry(constant: constant, method: method).add_definition(definition)
  self
end

#add_reference(constant: nil, method: nil, ref:) ⇒ Object



132
133
134
135
136
# File 'lib/steep/index/source_index.rb', line 132

def add_reference(constant: nil, method: nil, ref:)
  @count += 1
  entry(constant: constant, method: method).add_reference(ref)
  self
end

#entry(constant: nil, method: nil) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/steep/index/source_index.rb', line 138

def entry(constant: nil, method: nil)
  case
  when constant
    constant_index[constant] ||= ConstantEntry.new(name: constant)
  when method
    method_index[method] ||= MethodEntry.new(name: method)
  else
    raise
  end
end

#merge!(child) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/steep/index/source_index.rb', line 111

def merge!(child)
  raise unless child.parent == self
  raise unless child.parent_count == count

  constant_index.merge!(child.constant_index) do |_, entry, child_entry|
    entry.merge!(child_entry)
  end

  method_index.merge!(child.method_index) do |_, entry, child_entry|
    entry.merge!(child_entry)
  end

  @count = child.count + 1
end

#new_childObject



107
108
109
# File 'lib/steep/index/source_index.rb', line 107

def new_child
  SourceIndex.new(source: source, parent: self)
end

#reference(constant_node: nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/steep/index/source_index.rb', line 149

def reference(constant_node: nil)
  case
  when constant_node
    constant_index.each do |name, entry|
      if entry.references.include?(constant_node)
        return name
      end

      if entry.definitions.include?(constant_node)
        return name
      end
    end

    nil
  end
end