rdbc 0.3.4 → 0.5.0
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.
- checksums.yaml +15 -0
- data/README.rdoc +25 -22
- data/lib/rdbc.rb +0 -13
- data/lib/rdbc/contract.rb +44 -74
- data/lib/rdbc/translator.rb +43 -0
- metadata +29 -45
- data/lib/rdbc/decorator.rb +0 -20
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YzIzMDU2MWJlODM2ZmJlZWJjZmI3YjM0NzI4ZDU2MzQ4MTQ2Mjc5ZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZWNkOWU1ZmYwNmExYWEzNmNiYjkzZTlkM2NkMjViYzkyYmRhYmE1NA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
Mzg1YjQ5Y2I5ZmM4ODBkNzU2YWM0NTU1YzE1ZTZmZjkxMTNlNDkwMjhiODY5
|
10
|
+
MWZkMTJmZjRiMDFiNzM2NzA0YjA1OGRkYWFlYTM2MzlkZDYzYjk4YjdjOTAz
|
11
|
+
Y2Q1MWU5NWNhOWJmYTZlYzJhY2Y5MGIyODVmYjhjZmExODg3MjQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MjIwNzA0NGMyYTdlOTdmNGI1YWNlMjc4ZjJkN2VjZDBhNjdmOWYyMjdjN2Qw
|
14
|
+
NjBiYjk4ZjcwMTFkNjc3Y2QzMzQ4YjQyZGYyYmE5MTk1NDkwOTA5NzA0NmNj
|
15
|
+
MGE1MzAzNzJjMDI3MWJlYTc3YWI2YjU2MWZjODNjNGNkYWNkOWM=
|
data/README.rdoc
CHANGED
@@ -4,42 +4,45 @@ This library supports Design by Contract for Ruby.
|
|
4
4
|
|
5
5
|
=Installation
|
6
6
|
|
7
|
-
Install the *rdbc* gem
|
7
|
+
Install the *rdbc* gem by the following command:
|
8
8
|
|
9
|
-
$ gem install
|
9
|
+
$ gem install rdbc
|
10
10
|
|
11
11
|
=Usage
|
12
12
|
|
13
|
-
Given there is a the class Foo
|
14
|
-
For instances of this class you can define a contract by subclassing
|
15
|
-
<b>Rdbc::Contract</b>, in the example the contract is named
|
16
|
-
FooContract.
|
17
|
-
Then connect the class Foo to its contract FooContract by using the line
|
18
|
-
contract FooContract
|
19
|
-
in the class definition of Foo.
|
13
|
+
Given there is a the class *Foo*.
|
20
14
|
|
21
|
-
|
15
|
+
For instances of this class you can define a contract by subclassing:
|
16
|
+
FooContract < Rdbc::Contract.
|
22
17
|
|
23
|
-
|
18
|
+
Then connect the class *Foo* to its contract *FooContract* by using
|
19
|
+
class Foo
|
20
|
+
contract FooContract
|
21
|
+
...
|
22
|
+
end
|
23
|
+
|
24
|
+
Then the following happens when a method named *bar* of a *Foo* instance is called:
|
25
|
+
|
26
|
+
1. the corresponding pre condition *pre_bar* is called with the parameters:
|
24
27
|
* the instance itself
|
25
28
|
* the parameters of the method call
|
26
29
|
|
27
|
-
2. the method is called
|
30
|
+
2. the original method *bar* itself is called
|
28
31
|
|
29
|
-
3. the corresponding post condition is called with the parameters:
|
30
|
-
* the instance before the method is called
|
31
|
-
* the instance after the method is called
|
32
|
-
* the return value of the method call
|
32
|
+
3. the corresponding post condition *post_bar* is called with the parameters:
|
33
|
+
* the instance before the original method is called
|
34
|
+
* the instance after the original method is called
|
35
|
+
* the return value of the method call respectively a raised exception
|
33
36
|
* the parameters of the method call
|
34
37
|
|
35
|
-
4. the invariant is called with the parameters:
|
36
|
-
* the instance before the method is called
|
37
|
-
* the instance after the method is called
|
38
|
+
4. the mathod *invariant* is called with the parameters:
|
39
|
+
* the instance before the original method is called
|
40
|
+
* the instance after the original method is called
|
38
41
|
|
39
|
-
The instance before the execution of
|
42
|
+
The instance before the execution of the original method in 3. and 4.
|
40
43
|
is in fact a copy of the instance before the execution. So you have to implement
|
41
44
|
the method *initialize_copy* in the class under contract, i.e. the class
|
42
|
-
Foo
|
45
|
+
*Foo*.
|
43
46
|
|
44
47
|
=Support
|
45
48
|
|
@@ -55,7 +58,7 @@ Feel free to send:
|
|
55
58
|
|
56
59
|
=Copyright
|
57
60
|
|
58
|
-
Copyright (c) 2007 -
|
61
|
+
Copyright (c) 2007 - 2013 by Armin Jöllenbeck
|
59
62
|
<armin@joellenbeck.net[mailto:armin@joellenbeck.net]>
|
60
63
|
|
61
64
|
All rights reserved.
|
data/lib/rdbc.rb
CHANGED
@@ -1,15 +1,2 @@
|
|
1
1
|
module Rdbc
|
2
|
-
autoload 'Contract', 'rdbc/contract'
|
3
|
-
autoload 'Decorator', 'rdbc/decorator'
|
4
|
-
end
|
5
|
-
|
6
|
-
|
7
|
-
class Class
|
8
|
-
def contract(contract_class)
|
9
|
-
old_new = self.method(:new)
|
10
|
-
(class << self; self; end).send(:define_method, :new) do |*args|
|
11
|
-
object = old_new.call(*args)
|
12
|
-
Rdbc::Decorator.new(contract_class.new(object))
|
13
|
-
end
|
14
|
-
end
|
15
2
|
end
|
data/lib/rdbc/contract.rb
CHANGED
@@ -1,87 +1,57 @@
|
|
1
|
-
|
2
|
-
class Contract
|
3
|
-
def self.method_pre(method)
|
4
|
-
method_with_prefix(method, 'pre')
|
5
|
-
end
|
1
|
+
require 'rdbc/translator'
|
6
2
|
|
7
|
-
|
8
|
-
|
3
|
+
class Rdbc::Contract
|
4
|
+
class << self
|
5
|
+
private
|
6
|
+
def pre(method, &block)
|
7
|
+
define_method(Rdbc::Translator.method_pre(method), &block)
|
9
8
|
end
|
10
9
|
|
11
|
-
def
|
12
|
-
(
|
10
|
+
def post(method, &block)
|
11
|
+
define_method(Rdbc::Translator.method_post(method), &block)
|
13
12
|
end
|
14
13
|
|
15
|
-
def
|
16
|
-
|
14
|
+
def invariant(&block)
|
15
|
+
define_method(:invariant, &block)
|
17
16
|
end
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def initialize(object)
|
21
|
+
@object = object
|
22
|
+
send_if_respond_to(:invariant, object)
|
23
|
+
end
|
24
|
+
|
25
|
+
def method_missing(method, *args)
|
26
|
+
send_if_respond_to(Rdbc::Translator.method_pre(method), @object, *args)
|
27
|
+
before = @object.dup
|
28
|
+
begin
|
29
|
+
result = @object.send(method, *args)
|
30
|
+
rescue RuntimeError => result
|
31
|
+
end
|
32
|
+
send_if_respond_to(:invariant, @object)
|
33
|
+
send_if_respond_to(Rdbc::Translator.method_post(method), before, @object, result, *args)
|
34
|
+
if result.kind_of?(RuntimeError)
|
35
|
+
raise result
|
36
|
+
else
|
37
|
+
result
|
21
38
|
end
|
39
|
+
end
|
22
40
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
:** => 'power',
|
27
|
-
:~ => 'not',
|
28
|
-
:+@ => 'unary_plus',
|
29
|
-
:-@ => 'unary_minus',
|
30
|
-
:* => 'product',
|
31
|
-
:/ => 'quotient',
|
32
|
-
:% => 'modulo',
|
33
|
-
:+ => 'plus',
|
34
|
-
:- => 'minus',
|
35
|
-
:>> => 'right_shift',
|
36
|
-
:<< => 'left_shift',
|
37
|
-
:& => 'and',
|
38
|
-
:^ => 'xor',
|
39
|
-
:| => 'or',
|
40
|
-
:<= => 'less_or_equal',
|
41
|
-
:< => 'less',
|
42
|
-
:> => 'greater',
|
43
|
-
:>= => 'greater_or_equal',
|
44
|
-
:<=> => 'comparison',
|
45
|
-
:== => 'eql',
|
46
|
-
:=== => 'case_comparison',
|
47
|
-
:=~ => 'match'
|
48
|
-
}
|
49
|
-
|
50
|
-
def self.pre(method, &block)
|
51
|
-
define_method(method_pre(method), &block)
|
41
|
+
def send_if_respond_to(method, *args)
|
42
|
+
if respond_to?(method)
|
43
|
+
send(method, *args)
|
52
44
|
end
|
45
|
+
end
|
46
|
+
end
|
53
47
|
|
54
|
-
def self.post(method, &block)
|
55
|
-
define_method(method_post(method), &block)
|
56
|
-
end
|
57
48
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
@translator = self.class
|
65
|
-
end
|
66
|
-
|
67
|
-
def method_missing(method, *args)
|
68
|
-
@object_pre = @object.clone
|
69
|
-
send_if_respond_to(@translator.method_pre(method), *args)
|
70
|
-
result = nil
|
71
|
-
begin
|
72
|
-
result = @object.send(method, *args)
|
73
|
-
rescue StandardError => result
|
74
|
-
end
|
75
|
-
send_if_respond_to(:invariant)
|
76
|
-
send_if_respond_to(@translator.method_post(method), result, *args)
|
77
|
-
raise result if result.kind_of?(StandardError)
|
78
|
-
result
|
79
|
-
end
|
80
|
-
|
81
|
-
def send_if_respond_to(method, *args)
|
82
|
-
if respond_to?(method)
|
83
|
-
send(method, *args)
|
84
|
-
end
|
49
|
+
class Class
|
50
|
+
def contract(contract_class)
|
51
|
+
old_new = self.method(:new)
|
52
|
+
(class << self; self; end).send(:define_method, :new) do |*args|
|
53
|
+
object = old_new.call(*args)
|
54
|
+
contract_class.new(object)
|
85
55
|
end
|
86
56
|
end
|
87
|
-
end
|
57
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'rdbc'
|
2
|
+
|
3
|
+
# TODO rename to Rdbc::Translating
|
4
|
+
module Rdbc::Translator
|
5
|
+
def self.method_pre(method)
|
6
|
+
method_with_prefix(method, :pre)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.method_post(method)
|
10
|
+
method_with_prefix(method, :post)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.method_with_prefix(method, type)
|
14
|
+
:"#{type}_#{OPERATOR[method] || method}"
|
15
|
+
end
|
16
|
+
|
17
|
+
OPERATOR = {
|
18
|
+
:[] => :op_element_read,
|
19
|
+
:[]= => :op_element_write,
|
20
|
+
:** => :op_power,
|
21
|
+
:~ => :op_not,
|
22
|
+
:+@ => :op_unary_plus,
|
23
|
+
:-@ => :op_unary_minus,
|
24
|
+
:* => :op_product,
|
25
|
+
:/ => :op_quotient,
|
26
|
+
:% => :op_modulo,
|
27
|
+
:+ => :op_plus,
|
28
|
+
:- => :op_minus,
|
29
|
+
:>> => :op_right_shift,
|
30
|
+
:<< => :op_left_shift,
|
31
|
+
:& => :op_and,
|
32
|
+
:^ => :op_xor,
|
33
|
+
:| => :op_or,
|
34
|
+
:<= => :op_less_or_equal,
|
35
|
+
:< => :op_less,
|
36
|
+
:> => :op_greater,
|
37
|
+
:>= => :op_greater_or_equal,
|
38
|
+
:<=> => :op_comparison,
|
39
|
+
:== => :op_eql,
|
40
|
+
:=== => :op_case_comparison,
|
41
|
+
:=~ => :op_match
|
42
|
+
}
|
43
|
+
end
|
metadata
CHANGED
@@ -1,46 +1,34 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: rdbc
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
|
-
authors:
|
7
|
-
- Armin
|
6
|
+
authors:
|
7
|
+
- Armin Jöllenbeck
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
+
date: 2013-10-03 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: ! ' This library supports Design by Contract for the Ruby programming
|
14
|
+
language.
|
11
15
|
|
12
|
-
|
13
|
-
default_executable:
|
14
|
-
dependencies:
|
15
|
-
- !ruby/object:Gem::Dependency
|
16
|
-
name: blankslate
|
17
|
-
type: :runtime
|
18
|
-
version_requirement:
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
requirements:
|
21
|
-
- - "="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 2.1.2.3
|
24
|
-
version:
|
25
|
-
description: " This library supports Design by Contract for Ruby.\n"
|
16
|
+
'
|
26
17
|
email: armin@joellenbeck.net
|
27
18
|
executables: []
|
28
|
-
|
29
19
|
extensions: []
|
30
|
-
|
31
|
-
extra_rdoc_files:
|
20
|
+
extra_rdoc_files:
|
32
21
|
- README.rdoc
|
33
|
-
files:
|
22
|
+
files:
|
34
23
|
- README.rdoc
|
35
24
|
- lib/rdbc.rb
|
36
25
|
- lib/rdbc/contract.rb
|
37
|
-
- lib/rdbc/
|
38
|
-
|
39
|
-
homepage: http://github.com/armin-joellenbeck/rdbc/tree/master
|
26
|
+
- lib/rdbc/translator.rb
|
27
|
+
homepage: http://github.com/armin-joellenbeck/rdbc
|
40
28
|
licenses: []
|
41
|
-
|
29
|
+
metadata: {}
|
42
30
|
post_install_message:
|
43
|
-
rdoc_options:
|
31
|
+
rdoc_options:
|
44
32
|
- --all
|
45
33
|
- --charset
|
46
34
|
- utf8
|
@@ -48,26 +36,22 @@ rdoc_options:
|
|
48
36
|
- README.rdoc
|
49
37
|
- --title
|
50
38
|
- Design by Contract for Ruby
|
51
|
-
require_paths:
|
39
|
+
require_paths:
|
52
40
|
- lib
|
53
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
-
requirements:
|
55
|
-
- -
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
version:
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
version: "0"
|
64
|
-
version:
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
65
51
|
requirements: []
|
66
|
-
|
67
52
|
rubyforge_project:
|
68
|
-
rubygems_version:
|
53
|
+
rubygems_version: 2.0.3
|
69
54
|
signing_key:
|
70
|
-
specification_version:
|
55
|
+
specification_version: 4
|
71
56
|
summary: Design by Contract for Ruby.
|
72
57
|
test_files: []
|
73
|
-
|
data/lib/rdbc/decorator.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'blankslate'
|
2
|
-
|
3
|
-
module Rdbc
|
4
|
-
class Decorator < BlankSlate
|
5
|
-
instance_methods.reject do |method|
|
6
|
-
/__.*__/ === method
|
7
|
-
end.each do |method|
|
8
|
-
undef_method(method)
|
9
|
-
end
|
10
|
-
|
11
|
-
def initialize(contract)
|
12
|
-
@contract = contract
|
13
|
-
@contract.send_if_respond_to(:invariant)
|
14
|
-
end
|
15
|
-
|
16
|
-
def method_missing(method, *args)
|
17
|
-
@contract.method_missing(method, *args)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|