23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/decorators.rb', line 23
def common_method_added name, is_class_method
return unless @decorators
decorators = @decorators.dup
@decorators = nil
@decorated_methods ||= {:class_methods => {}, :instance_methods => {}}
class << self; attr_accessor :decorated_methods; end
is_private = nil
decorators.each do |klass, args|
if is_class_method
decorator = klass.new(self, method(name), *args)
@decorated_methods[:class_methods][name] ||= []
@decorated_methods[:class_methods][name] << decorator
is_private = self.private_methods.include?(name.to_s)
else
decorator = klass.new(self, instance_method(name), *args)
@decorated_methods[:instance_methods][name] ||= []
@decorated_methods[:instance_methods][name] << decorator
is_private = self.private_instance_methods.include?(name.to_s)
end
end
class_eval <<-ruby_eval, __FILE__, __LINE__ + 1
def #{is_class_method ? "self." : ""}#{name}(*args, &blk)
current = self#{is_class_method ? "" : ".class"}
ancestors = current.ancestors
ancestors.shift # first one is just the class itself
while current && !current.respond_to?(:decorated_methods) || current.decorated_methods.nil?
current = ancestors.shift
end
if !current.respond_to?(:decorated_methods) || current.decorated_methods.nil?
raise "Couldn't find decorator for method " + self.class.name + ":#{name}.\nDoes this method look correct to you? If you are using contracts from rspec, rspec wraps classes in it's own class.\nLook at the specs for contracts.ruby as an example of how to write contracts in this case."
end
methods = current.decorated_methods[#{is_class_method ? ":class_methods" : ":instance_methods"}][#{name.inspect}]
# this adds support for overloading methods. Here we go through each method and call it with the arguments.
# If we get a ContractError, we move to the next function. Otherwise we return the result.
# If we run out of functions, we raise the last ContractError.
success = false
i = 0
result = nil
while !success
method = methods[i]
i += 1
begin
success = true
result = method.call_with(self, *args, &blk)
rescue ContractError => e
success = false
raise e unless methods[i]
end
end
result
end
#{is_private ? "private #{name.inspect}" : ""}
ruby_eval
end
|