contracts 0.2.4 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/decorators.rb +24 -3
- metadata +3 -4
data/lib/decorators.rb
CHANGED
@@ -38,11 +38,13 @@ module MethodDecorators
|
|
38
38
|
# We assume here that the decorator (klass) responds to .new
|
39
39
|
if is_class_method
|
40
40
|
decorator = klass.new(self, method(name), *args)
|
41
|
-
@decorated_methods[:class_methods][name]
|
41
|
+
@decorated_methods[:class_methods][name] ||= []
|
42
|
+
@decorated_methods[:class_methods][name] << decorator
|
42
43
|
is_private = self.private_methods.include?(name.to_s)
|
43
44
|
else
|
44
45
|
decorator = klass.new(self, instance_method(name), *args)
|
45
|
-
@decorated_methods[:instance_methods][name]
|
46
|
+
@decorated_methods[:instance_methods][name] ||= []
|
47
|
+
@decorated_methods[:instance_methods][name] << decorator
|
46
48
|
is_private = self.private_instance_methods.include?(name.to_s)
|
47
49
|
end
|
48
50
|
end
|
@@ -62,7 +64,26 @@ module MethodDecorators
|
|
62
64
|
if !current.respond_to?(:decorated_methods) || current.decorated_methods.nil?
|
63
65
|
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."
|
64
66
|
end
|
65
|
-
current.decorated_methods[#{is_class_method ? ":class_methods" : ":instance_methods"}][#{name.inspect}]
|
67
|
+
methods = current.decorated_methods[#{is_class_method ? ":class_methods" : ":instance_methods"}][#{name.inspect}]
|
68
|
+
|
69
|
+
# this adds support for overloading methods. Here we go through each method and call it with the arguments.
|
70
|
+
# If we get a ContractError, we move to the next function. Otherwise we return the result.
|
71
|
+
# If we run out of functions, we raise the last ContractError.
|
72
|
+
success = false
|
73
|
+
i = 0
|
74
|
+
result = nil
|
75
|
+
while !success
|
76
|
+
method = methods[i]
|
77
|
+
i += 1
|
78
|
+
begin
|
79
|
+
success = true
|
80
|
+
result = method.call_with(self, *args, &blk)
|
81
|
+
rescue ContractError => e
|
82
|
+
success = false
|
83
|
+
raise e unless methods[i]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
result
|
66
87
|
end
|
67
88
|
#{is_private ? "private #{name.inspect}" : ""}
|
68
89
|
ruby_eval
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: contracts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
|
10
|
-
version: 0.2.4
|
8
|
+
- 3
|
9
|
+
version: "0.3"
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Aditya Bhargava
|