contracts 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/builtin_contracts.rb +1 -0
- data/lib/contracts.rb +9 -2
- metadata +13 -4
- data/lib/decorators_aliased.rb +0 -102
- data/lib/decorators_old.rb +0 -97
data/lib/builtin_contracts.rb
CHANGED
data/lib/contracts.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
require 'decorators'
|
2
2
|
require 'builtin_contracts'
|
3
3
|
|
4
|
+
class ContractError < ArgumentError
|
5
|
+
end
|
6
|
+
|
4
7
|
module Contracts
|
5
8
|
def self.included(base)
|
6
9
|
common base
|
@@ -77,7 +80,11 @@ class Contract < Decorator
|
|
77
80
|
end
|
78
81
|
|
79
82
|
if RUBY_VERSION =~ /^1\.8/
|
80
|
-
|
83
|
+
if data[:method].respond_to?(:__file__)
|
84
|
+
position = data[:method].__file__ + ":" + data[:method].__line__.to_s
|
85
|
+
else
|
86
|
+
position = data[:method].inspect
|
87
|
+
end
|
81
88
|
else
|
82
89
|
file, line = data[:method].source_location
|
83
90
|
position = file + ":" + line.to_s
|
@@ -105,7 +112,7 @@ class Contract < Decorator
|
|
105
112
|
# exit
|
106
113
|
# end
|
107
114
|
def self.failure_callback(data)
|
108
|
-
raise failure_msg(data)
|
115
|
+
raise ContractError, failure_msg(data)
|
109
116
|
end
|
110
117
|
|
111
118
|
# Used to verify if an argument satisfies a contract.
|
metadata
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: contracts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 21
|
4
5
|
prerelease:
|
5
|
-
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
6
11
|
platform: ruby
|
7
12
|
authors:
|
8
13
|
- Aditya Bhargava
|
@@ -25,8 +30,6 @@ files:
|
|
25
30
|
- lib/builtin_contracts.rb
|
26
31
|
- lib/contracts.rb
|
27
32
|
- lib/decorators.rb
|
28
|
-
- lib/decorators_aliased.rb
|
29
|
-
- lib/decorators_old.rb
|
30
33
|
- lib/testable.rb
|
31
34
|
homepage: http://github.com/egonSchiele/contracts.ruby
|
32
35
|
licenses: []
|
@@ -41,17 +44,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
41
44
|
requirements:
|
42
45
|
- - ">="
|
43
46
|
- !ruby/object:Gem::Version
|
47
|
+
hash: 3
|
48
|
+
segments:
|
49
|
+
- 0
|
44
50
|
version: "0"
|
45
51
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
52
|
none: false
|
47
53
|
requirements:
|
48
54
|
- - ">="
|
49
55
|
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
50
59
|
version: "0"
|
51
60
|
requirements: []
|
52
61
|
|
53
62
|
rubyforge_project:
|
54
|
-
rubygems_version: 1.8.
|
63
|
+
rubygems_version: 1.8.25
|
55
64
|
signing_key:
|
56
65
|
specification_version: 3
|
57
66
|
summary: Contracts for Ruby.
|
data/lib/decorators_aliased.rb
DELETED
@@ -1,102 +0,0 @@
|
|
1
|
-
module MethodDecorators
|
2
|
-
def self.extended(klass)
|
3
|
-
klass.class_eval do
|
4
|
-
@@__decorated_methods ||= {}
|
5
|
-
def self.__decorated_methods
|
6
|
-
@@__decorated_methods
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.__decorated_methods_set(k, v)
|
10
|
-
@@__decorated_methods[k] = v
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
# first, when you write a contract, the decorate method gets called which
|
16
|
-
# sets the @decorators variable. Then when the next method after the contract
|
17
|
-
# is defined, method_added is called and we look at the @decorators variable
|
18
|
-
# to find the decorator for that method. This is how we associate decorators
|
19
|
-
# with methods.
|
20
|
-
def method_added(name)
|
21
|
-
common_method_added name, false
|
22
|
-
super
|
23
|
-
end
|
24
|
-
|
25
|
-
# For Ruby 1.9
|
26
|
-
def singleton_method_added name
|
27
|
-
common_method_added name, true
|
28
|
-
super
|
29
|
-
end
|
30
|
-
|
31
|
-
def common_method_added name, is_class_method
|
32
|
-
return unless @decorators
|
33
|
-
|
34
|
-
decorators = @decorators.dup
|
35
|
-
@decorators = nil
|
36
|
-
|
37
|
-
decorators.each do |klass, args|
|
38
|
-
# a reference to the method gets passed into the contract here. This is good because
|
39
|
-
# we are going to redefine this method with a new name below...so this reference is
|
40
|
-
# now the *only* reference to the old method that exists.
|
41
|
-
if klass.respond_to? :new
|
42
|
-
if is_class_method
|
43
|
-
decorator = klass.new(*args)
|
44
|
-
else
|
45
|
-
decorator = klass.new(*args)
|
46
|
-
end
|
47
|
-
else
|
48
|
-
decorator = klass
|
49
|
-
end
|
50
|
-
__decorated_methods_set(name, decorator)
|
51
|
-
end
|
52
|
-
|
53
|
-
# in place of this method, we are going to define our own method. This method
|
54
|
-
# just calls the decorator passing in all args that were to be passed into the method.
|
55
|
-
# The decorator in turn has a reference to the actual method, so it can call it
|
56
|
-
# on its own, after doing it's decorating of course.
|
57
|
-
alias_method :"original_#{name}", name
|
58
|
-
class_eval %{
|
59
|
-
def #{is_class_method ? "self." : ""}#{name}(*args, &blk)
|
60
|
-
this = self#{is_class_method ? "" : ".class"}
|
61
|
-
#this.__decorated_methods[#{name.inspect}].check_args(*args, &blk)
|
62
|
-
raise unless args[0].is_a?(Numeric)
|
63
|
-
raise unless args[1].is_a?(Numeric)
|
64
|
-
|
65
|
-
res = self.send :"original_#{name}", *args, &blk
|
66
|
-
raise unless res.is_a?(Numeric)
|
67
|
-
#this.__decorated_methods[#{name.inspect}].check_res(res)
|
68
|
-
res
|
69
|
-
end
|
70
|
-
}, __FILE__, __LINE__ + 1
|
71
|
-
end
|
72
|
-
|
73
|
-
def decorate(klass, *args)
|
74
|
-
@decorators ||= []
|
75
|
-
@decorators << [klass, args]
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
class Decorator
|
80
|
-
# an attr_accessor for a class variable:
|
81
|
-
class << self; attr_accessor :decorators; end
|
82
|
-
|
83
|
-
def self.inherited(klass)
|
84
|
-
name = klass.name.gsub(/^./) {|m| m.downcase}
|
85
|
-
|
86
|
-
return if name =~ /^[^A-Za-z_]/ || name =~ /[^0-9A-Za-z_]/
|
87
|
-
|
88
|
-
# the file and line parameters set the text for error messages
|
89
|
-
# make a new method that is the name of your decorator.
|
90
|
-
# that method accepts random args and a block.
|
91
|
-
# inside, `decorate` is called with those params.
|
92
|
-
MethodDecorators.module_eval <<-ruby_eval, __FILE__, __LINE__ + 1
|
93
|
-
def #{klass}(*args, &blk)
|
94
|
-
decorate(#{klass}, *args, &blk)
|
95
|
-
end
|
96
|
-
ruby_eval
|
97
|
-
end
|
98
|
-
|
99
|
-
def initialize(klass, method)
|
100
|
-
@method = method
|
101
|
-
end
|
102
|
-
end
|
data/lib/decorators_old.rb
DELETED
@@ -1,97 +0,0 @@
|
|
1
|
-
module MethodDecorators
|
2
|
-
def self.extended(klass)
|
3
|
-
class << klass
|
4
|
-
attr_accessor :decorated_methods
|
5
|
-
end
|
6
|
-
end
|
7
|
-
|
8
|
-
# first, when you write a contract, the decorate method gets called which
|
9
|
-
# sets the @decorators variable. Then when the next method after the contract
|
10
|
-
# is defined, method_added is called and we look at the @decorators variable
|
11
|
-
# to find the decorator for that method. This is how we associate decorators
|
12
|
-
# with methods.
|
13
|
-
def method_added(name)
|
14
|
-
common_method_added name, false
|
15
|
-
super
|
16
|
-
end
|
17
|
-
|
18
|
-
def singleton_method_added name
|
19
|
-
common_method_added name, true
|
20
|
-
super
|
21
|
-
end
|
22
|
-
|
23
|
-
def common_method_added name, is_class_method
|
24
|
-
return unless @decorators
|
25
|
-
|
26
|
-
decorators = @decorators.dup
|
27
|
-
@decorators = nil
|
28
|
-
@decorated_methods ||= Hash.new {|h,k| h[k] = []}
|
29
|
-
|
30
|
-
# attr_accessor on the class variable decorated_methods
|
31
|
-
class << self; attr_accessor :decorated_methods; end
|
32
|
-
|
33
|
-
decorators.each do |klass, args|
|
34
|
-
# a reference to the method gets passed into the contract here. This is good because
|
35
|
-
# we are going to redefine this method with a new name below...so this reference is
|
36
|
-
# now the *only* reference to the old method that exists.
|
37
|
-
if klass.respond_to? :new
|
38
|
-
if is_class_method
|
39
|
-
decorator = klass.new(self, method(name), *args)
|
40
|
-
else
|
41
|
-
decorator = klass.new(self, instance_method(name), *args)
|
42
|
-
end
|
43
|
-
else
|
44
|
-
decorator = klass
|
45
|
-
end
|
46
|
-
@decorated_methods[name] << decorator
|
47
|
-
end
|
48
|
-
|
49
|
-
# in place of this method, we are going to define our own method. This method
|
50
|
-
# just calls the decorator passing in all args that were to be passed into the method.
|
51
|
-
# The decorator in turn has a reference to the actual method, so it can call it
|
52
|
-
# on its own, after doing it's decorating of course.
|
53
|
-
class_eval <<-ruby_eval, __FILE__, __LINE__ + 1
|
54
|
-
def #{is_class_method ? "self." : ""}#{name}(*args, &blk)
|
55
|
-
ret = nil
|
56
|
-
this = self#{is_class_method ? "" : ".class"}
|
57
|
-
unless this.respond_to?(:decorated_methods) && !this.decorated_methods.nil?
|
58
|
-
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."
|
59
|
-
end
|
60
|
-
this.decorated_methods[#{name.inspect}].each do |decorator|
|
61
|
-
ret = decorator.call_with(self, *args, &blk)
|
62
|
-
end
|
63
|
-
ret
|
64
|
-
end
|
65
|
-
ruby_eval
|
66
|
-
end
|
67
|
-
|
68
|
-
def decorate(klass, *args)
|
69
|
-
@decorators ||= []
|
70
|
-
@decorators << [klass, args]
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
class Decorator
|
75
|
-
# an attr_accessor for a class variable:
|
76
|
-
class << self; attr_accessor :decorators; end
|
77
|
-
|
78
|
-
def self.inherited(klass)
|
79
|
-
name = klass.name.gsub(/^./) {|m| m.downcase}
|
80
|
-
|
81
|
-
return if name =~ /^[^A-Za-z_]/ || name =~ /[^0-9A-Za-z_]/
|
82
|
-
|
83
|
-
# the file and line parameters set the text for error messages
|
84
|
-
# make a new method that is the name of your decorator.
|
85
|
-
# that method accepts random args and a block.
|
86
|
-
# inside, `decorate` is called with those params.
|
87
|
-
MethodDecorators.module_eval <<-ruby_eval, __FILE__, __LINE__ + 1
|
88
|
-
def #{klass}(*args, &blk)
|
89
|
-
decorate(#{klass}, *args, &blk)
|
90
|
-
end
|
91
|
-
ruby_eval
|
92
|
-
end
|
93
|
-
|
94
|
-
def initialize(klass, method)
|
95
|
-
@method = method
|
96
|
-
end
|
97
|
-
end
|