Module: UnboundMethod::Source
Overview
Source lookup.
Instance Method Summary collapse
-
#get_above_comment(file, line) ⇒ Object
Get comment from file searching up from given line number.
-
#get_above_comment_lines(file, line) ⇒ Object
Get comment from file searching up from given line number.
-
#get_following_comment(file, line) ⇒ Object
Get comment from file searching down from given line number.
-
#get_following_comment_lines(file, line) ⇒ Object
Get comment from file searching down from given line number.
-
#read(file) ⇒ Array
Read and cache file.
Instance Method Details
#get_above_comment(file, line) ⇒ Object
Get comment from file searching up from given line number.
40 41 42 |
# File 'lib/executable/core_ext.rb', line 40 def get_above_comment(file, line) get_above_comment_lines(file, line).join("\n").strip end |
#get_above_comment_lines(file, line) ⇒ Object
Get comment from file searching up from given line number.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/executable/core_ext.rb', line 49 def get_above_comment_lines(file, line) text = read(file) index = line - 1 while index >= 0 && text[index] !~ /^\s*\#/ return [] if text[index] =~ /^\s*end/ index -= 1 end rindex = index while text[index] =~ /^\s*\#/ index -= 1 end result = text[index..rindex] result = result.map{ |s| s.strip } result = result.reject{ |s| s[0,1] != '#' } result = result.map{ |s| s.sub(/^#/,'').strip } #result = result.reject{ |s| s == "" } result end |
#get_following_comment(file, line) ⇒ Object
Get comment from file searching down from given line number.
73 74 75 |
# File 'lib/executable/core_ext.rb', line 73 def get_following_comment(file, line) get_following_comment_lines(file, line).join("\n").strip end |
#get_following_comment_lines(file, line) ⇒ Object
Get comment from file searching down from given line number.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/executable/core_ext.rb', line 82 def get_following_comment_lines(file, line) text = read(file) index = line || 0 while text[index] !~ /^\s*\#/ return nil if text[index] =~ /^\s*(class|module)/ index += 1 end rindex = index while text[rindex] =~ /^\s*\#/ rindex += 1 end result = text[index..(rindex-2)] result = result.map{ |s| s.strip } result = result.reject{ |s| s[0,1] != '#' } result = result.map{ |s| s.sub(/^#/,'').strip } result.join("\n").strip end |
#read(file) ⇒ Array
Read and cache file.
30 31 32 33 |
# File 'lib/executable/core_ext.rb', line 30 def read(file) @read ||= {} @read[file] ||= File.readlines(file) end |