class AddedMethods::AddedMethod
Attributes
base[RW]
class=[RW]
def[RW]
file[RW]
klass[RW]
line[RW]
name[RW]
singleton[RW]
singleton?[RW]
time[RW]
Public Class Methods
new(args = {})
click to toggle source
# File lib/added_methods/added_method.rb 33 def initialize(args = {}) 34 self.time = Time.now 35 36 args.each { |key, value| 37 send("#{key}=", value) 38 } 39 end
Public Instance Methods
[](key)
click to toggle source
# File lib/added_methods/added_method.rb 44 def [](key) 45 send(key.to_sym == :class ? :klass : key) 46 end
extract_source(num_lines = nil)
click to toggle source
# File lib/added_methods/added_method.rb 56 def extract_source(num_lines = nil) 57 lines = extract_source_from_script_lines(num_lines) 58 59 # try to make sure we correctly extracted the method 60 # definition, otherwise try to get it from Ruby2Ruby 61 if lines && lines.first =~ /\b#{name}\b/ 62 lines 63 else 64 extract_source_from_r2r || lines 65 end 66 end
r2r_source()
click to toggle source
# File lib/added_methods/added_method.rb 52 def r2r_source 53 @r2r_source ||= extract_source_from_r2r 54 end
source()
click to toggle source
# File lib/added_methods/added_method.rb 48 def source 49 @source ||= extract_source 50 end
to_s()
click to toggle source
# File lib/added_methods/added_method.rb 68 def to_s 69 str = "# File #{file}, line #{line}" 70 71 case lines = source 72 when Array 73 num = line - 1 74 width = (num + lines.size).to_s.length 75 76 lines.map! { |l| "%0#{width}d: %s" % [num += 1, l] } 77 78 "#{' ' * width} #{str}\n#{lines}" 79 when String 80 "#{str}#{lines}" 81 else 82 str 83 end 84 end
Private Instance Methods
extract_source_from_r2r()
click to toggle source
Use Ruby2Ruby as a last resort. But note that it only ever finds the latest, i.e. currently active, method definition, not necessarily the one we’re looking for.
# File lib/added_methods/added_method.rb 134 def extract_source_from_r2r 135 if Object.const_defined?(:Ruby2Ruby) 136 " [R2R]\n#{Ruby2Ruby.translate(klass, name)}" 137 end 138 end
extract_source_from_script_lines(num_lines = nil)
click to toggle source
# File lib/added_methods/added_method.rb 88 def extract_source_from_script_lines(num_lines = nil) 89 return unless Object.const_defined?(:SCRIPT_LINES__) 90 return unless script_lines = SCRIPT_LINES__[file] 91 92 start, from, to = line - 1, line, script_lines.size - 1 93 94 # suppose we're already in a block 95 in_block = 1 96 97 num_lines ||= case definition = script_lines[start] 98 # def ... end, or do ... end style block 99 when /\b(?:def|do)\b/ 100 definition =~ /\bend\b/ ? 1 : begin 101 from.upto(to) { |i| 102 case line = script_lines[i] 103 when /[^;\s]\s+(?:if|unless)\b/ 104 # probably postfix conditional, ignore 105 when /\b(?:if|unless|while|until|def|do)\b/ 106 in_block += 1 107 when /\bend\b/ 108 in_block -= 1 109 end 110 111 break i - start + 1 if in_block.zero? 112 } 113 end 114 # { ... } style block 115 when /\bdefine_method\b/ 116 from.upto(to) { |i| 117 line = script_lines[i] 118 119 in_block += line.count('{') 120 in_block -= line.count('}') 121 122 break i - start + 1 if in_block.zero? 123 } 124 else 125 1 126 end 127 128 script_lines[start, num_lines] 129 end