Module: MethodSource
- Defined in:
- lib/method_source.rb,
lib/method_source/version.rb,
lib/method_source/source_location.rb
Defined Under Namespace
Modules: MethodExtensions, SourceLocation
Constant Summary collapse
- VERSION =
"0.4.0"
Class Method Summary collapse
-
.comment_helper(source_location) ⇒ String
Helper method responsible for opening source file and buffering up the comments for a specified method.
-
.source_helper(source_location) ⇒ File
Helper method responsible for extracting method body.
-
.valid_expression?(code) ⇒ Boolean
Determine if a string of code is a valid Ruby expression.
Class Method Details
.comment_helper(source_location) ⇒ String
Helper method responsible for opening source file and buffering up the comments for a specified method. Defined here to avoid polluting ‘Method` class.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/method_source.rb', line 70 def self.comment_helper(source_location) return nil if !source_location.is_a?(Array) file_name, line = source_location File.open(file_name) do |file| buffer = "" (line - 1).times do line = file.readline # Add any line that is a valid ruby comment, # but clear as soon as we hit a non comment line. if (line =~ /^\s*#/) || (line =~ /^\s*$/) buffer << line.lstrip else buffer.replace("") end end buffer end end |
.source_helper(source_location) ⇒ File
Helper method responsible for extracting method body. Defined here to avoid polluting ‘Method` class.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/method_source.rb', line 48 def self.source_helper(source_location) return nil if !source_location.is_a?(Array) file_name, line = source_location File.open(file_name) do |file| (line - 1).times { file.readline } code = "" loop do val = file.readline code << val return code if valid_expression?(code) end end end |
.valid_expression?(code) ⇒ Boolean
Determine if a string of code is a valid Ruby expression. Ruby 1.9 uses Ripper, Ruby 1.8 uses RubyParser.
21 22 23 |
# File 'lib/method_source.rb', line 21 def self.valid_expression?(code) !!Ripper::SexpBuilder.new(code).parse end |