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 " def \#{is_class_method ? \"self.\" : \"\"}\#{name}(*args, &blk)\n current = self\#{is_class_method ? \"\" : \".class\"}\n ancestors = current.ancestors\n ancestors.shift # first one is just the class itself\n while current && !current.respond_to?(:decorated_methods) || current.decorated_methods.nil?\n current = ancestors.shift\n end\n if !current.respond_to?(:decorated_methods) || current.decorated_methods.nil?\n 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.\"\n end\n methods = current.decorated_methods[\#{is_class_method ? \":class_methods\" : \":instance_methods\"}][\#{name.inspect}]\n\n # this adds support for overloading methods. Here we go through each method and call it with the arguments.\n # If we get a ContractError, we move to the next function. Otherwise we return the result.\n # If we run out of functions, we raise the last ContractError.\n success = false\n i = 0\n result = nil\n while !success\n method = methods[i]\n i += 1\n begin\n success = true\n result = method.call_with(self, *args, &blk)\n rescue ContractError => e\n success = false\n raise e unless methods[i]\n end\n end\n result\n end\n \#{is_private ? \"private \#{name.inspect}\" : \"\"}\n ruby_eval\nend\n", __FILE__, __LINE__ + 1
|