Class: Ruby::Reflection::MethodMirror

Inherits:
Mirror show all
Includes:
AbstractReflection::MethodMirror
Defined in:
lib/ruby/reflection/method_mirror.rb

Instance Attribute Summary

Attributes included from AbstractReflection::Mirror

#reflection

Instance Method Summary collapse

Methods included from AbstractReflection::MethodMirror

#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?

Methods included from AbstractReflection::Mirror

#initialize, #mirrors?, #name, #reflectee

Methods included from AbstractReflection::Mirror::ClassMethods

#included, #mirror_class, #new, #reflect, #reflect!, #reflects?, #register_mirror

Instance Method Details

#argumentsObject



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_argumentObject



46
47
48
# File 'lib/ruby/reflection/method_mirror.rb', line 46

def block_argument
  args(:block).first
end

#defining_classObject



38
39
40
# File 'lib/ruby/reflection/method_mirror.rb', line 38

def defining_class
  reflection.reflect try_send(:owner)
end

#deleteObject



42
43
44
# File 'lib/ruby/reflection/method_mirror.rb', line 42

def delete
  try_send(:owner).send(:remove_method, @subject.name)
end

#fileObject



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

#lineObject



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_argumentsObject



54
55
56
# File 'lib/ruby/reflection/method_mirror.rb', line 54

def optional_arguments
  args(:opt)
end

#private?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/ruby/reflection/method_mirror.rb', line 74

def private?
  visibility? :private
end

#protected?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/ruby/reflection/method_mirror.rb', line 66

def protected?
  visibility? :protected
end

#public?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/ruby/reflection/method_mirror.rb', line 70

def public?
  visibility? :public
end

#required_argumentsObject



58
59
60
# File 'lib/ruby/reflection/method_mirror.rb', line 58

def required_arguments
  args(:req)
end

#selectorObject



34
35
36
# File 'lib/ruby/reflection/method_mirror.rb', line 34

def selector
  @subject.name.to_s
end

#send_offsetsObject



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

#sourceObject



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_argumentObject



50
51
52
# File 'lib/ruby/reflection/method_mirror.rb', line 50

def splat_argument
  args(:rest).first
end

#step_offsetsObject



101
102
103
# File 'lib/ruby/reflection/method_mirror.rb', line 101

def step_offsets
  [1, *send_offsets.values]
end