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.



198
199
200
201
202
# File 'lib/method_extensions/method/source_with_doc.rb', line 198

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



193
194
195
196
# File 'lib/method_extensions/method/source_with_doc.rb', line 193

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

Instance Method Details

#definition_linesObject



214
215
216
# File 'lib/method_extensions/method/source_with_doc.rb', line 214

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

#method_sourceObject



204
205
206
207
208
209
210
211
212
# File 'lib/method_extensions/method/source_with_doc.rb', line 204

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



224
225
226
227
228
229
230
231
# File 'lib/method_extensions/method/source_with_doc.rb', line 224

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



233
234
235
# File 'lib/method_extensions/method/source_with_doc.rb', line 233

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