Class: MethodExtensions::MethodSourceWithDoc::MethodSourceRipper

Inherits:
Ripper
  • Object
show all
Defined in:
lib/method_extensions/method/source_with_doc.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, method_definition_lineno) ⇒ MethodSourceRipper

Returns a new instance of MethodSourceRipper.



154
155
156
157
158
# File 'lib/method_extensions/method/source_with_doc.rb', line 154

def initialize(filename, method_definition_lineno)
  super(IO.read(filename), filename)
  @src_lines = IO.read(filename).split("\n")
  @method_definition_lineno = method_definition_lineno
end

Class Method Details

.source_from_source_location(source_location) ⇒ Object



149
150
151
152
# File 'lib/method_extensions/method/source_with_doc.rb', line 149

def self.source_from_source_location(source_location)
  return unless source_location
  new(*source_location).method_source
end

Instance Method Details

#definition_linesObject



170
171
172
# File 'lib/method_extensions/method/source_with_doc.rb', line 170

def definition_lines
  @src_lines[@method_definition_lineno - 1 .. @method_definition_lineno + 1]
end

#method_sourceObject



160
161
162
163
164
165
166
167
168
# File 'lib/method_extensions/method/source_with_doc.rb', line 160

def method_source
  parse
  if @method_source
    @method_source
  else
    raise ArgumentError.new("failed to find method definition around the lines:\n" <<
            definition_lines.join("\n"))
  end
end

#on_def(name, params, body) ⇒ Object



180
181
182
183
184
185
186
187
# File 'lib/method_extensions/method/source_with_doc.rb', line 180

def on_def(name, params, body)
  from_lineno, from_column = name
  return unless @method_definition_lineno == from_lineno

  to_lineno, to_column = lineno, column

  @method_source = @src_lines[from_lineno - 1 .. to_lineno - 1].join("\n").strip
end

#on_defs(target, period, name, params, body) ⇒ Object



189
190
191
# File 'lib/method_extensions/method/source_with_doc.rb', line 189

def on_defs(target, period, name, params, body)
  on_def(target, params, body)
end