Class: Ruby::Reflection::MethodMirror
Instance Attribute Summary
#reflection
Instance Method Summary
collapse
#ast, #binding, #break, #breakpoints, #bytecode, #execution_time, #execution_time_average, #execution_time_share, #invocation_count, #is_alias?, #is_closure?, #native_code, #original_method, #private!, #protected!, #public!, #references_name?, #sends_message?
#initialize, #mirrors?, #name, #reflectee
#included, #mirror_class, #new, #reflect, #reflect!, #reflects?, #register_mirror
Instance Method Details
#arguments ⇒ Object
62
63
64
|
# File 'lib/ruby/reflection/method_mirror.rb', line 62
def arguments
try_send(:parameters).map { |t,a| a.to_s }
end
|
#block_argument ⇒ Object
46
47
48
|
# File 'lib/ruby/reflection/method_mirror.rb', line 46
def block_argument
args(:block).first
end
|
#defining_class ⇒ Object
38
39
40
|
# File 'lib/ruby/reflection/method_mirror.rb', line 38
def defining_class
reflection.reflect try_send(:owner)
end
|
42
43
44
|
# File 'lib/ruby/reflection/method_mirror.rb', line 42
def delete
try_send(:owner).send(:remove_method, @subject.name)
end
|
16
17
18
|
# File 'lib/ruby/reflection/method_mirror.rb', line 16
def file
source_location.first
end
|
#file=(name) ⇒ Object
20
21
22
23
|
# File 'lib/ruby/reflection/method_mirror.rb', line 20
def file=(name)
defining_class.reflectee.class_eval(source, name, line)
@subject = defining_class.method(selector)
end
|
25
26
27
|
# File 'lib/ruby/reflection/method_mirror.rb', line 25
def line
source_location.last - 1
end
|
#line=(num) ⇒ Object
29
30
31
32
|
# File 'lib/ruby/reflection/method_mirror.rb', line 29
def line=(num)
defining_class.reflectee.class_eval(source, file, num)
@subject = defining_class.method(selector)
end
|
#optional_arguments ⇒ Object
54
55
56
|
# File 'lib/ruby/reflection/method_mirror.rb', line 54
def optional_arguments
args(:opt)
end
|
#private? ⇒ Boolean
74
75
76
|
# File 'lib/ruby/reflection/method_mirror.rb', line 74
def private?
visibility? :private
end
|
#protected? ⇒ Boolean
66
67
68
|
# File 'lib/ruby/reflection/method_mirror.rb', line 66
def protected?
visibility? :protected
end
|
#public? ⇒ Boolean
70
71
72
|
# File 'lib/ruby/reflection/method_mirror.rb', line 70
def public?
visibility? :public
end
|
#required_arguments ⇒ Object
58
59
60
|
# File 'lib/ruby/reflection/method_mirror.rb', line 58
def required_arguments
args(:req)
end
|
34
35
36
|
# File 'lib/ruby/reflection/method_mirror.rb', line 34
def selector
@subject.name.to_s
end
|
#send_offsets ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/ruby/reflection/method_mirror.rb', line 105
def send_offsets
sexp = RubyParser.new.process(source).flatten
sends = sexp.each_with_index.map {|e,i| sexp[i-1] if e == :arglist }
sends = sends.compact.collect(&:to_s)
offsets = [0]
sends.each do |name|
next_offset = source[offsets.last..-1] =~ /#{Regexp.escape(name)}/
break unless next_offset
offsets << next_offset
end
offsets.shift
Hash[*sends.zip(offsets).flatten]
rescue Exception
return {}
end
|
78
79
80
81
82
|
# File 'lib/ruby/reflection/method_mirror.rb', line 78
def source
try_send(:source) or raise(CapabilitiesExceeded)
rescue => e
e.message
end
|
#source=(str) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/ruby/reflection/method_mirror.rb', line 84
def source=(str)
src = try_send(:source)
raise "cannot write to source location" unless file and line and File.writable?(file)
f = File.read(file).lines.to_a
srclen = src.lines.to_a.size
prefix = f[0...line].join
method_src = f[line...line + srclen].join
postfix = f[line + srclen..-1].join
raise "source differs from runtime" unless method_src.strip == src.strip
File.open(file, 'w') {|f| f << prefix << str << postfix }
defining_class.reflectee.class_eval(str, file, line)
rescue Exception => e
raise CapabilitiesExceeded, e.message
end
|
#splat_argument ⇒ Object
50
51
52
|
# File 'lib/ruby/reflection/method_mirror.rb', line 50
def splat_argument
args(:rest).first
end
|
#step_offsets ⇒ Object
101
102
103
|
# File 'lib/ruby/reflection/method_mirror.rb', line 101
def step_offsets
[1, *send_offsets.values]
end
|