rdbc 0.2.5 → 0.3.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.
data/lib/rdbc.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  module Rdbc
2
2
  autoload 'Contract', 'rdbc/contract'
3
3
  autoload 'Decorator', 'rdbc/decorator'
4
- autoload 'Translating', 'rdbc/translating'
5
4
  end
6
5
 
7
6
 
@@ -10,7 +9,7 @@ class Class
10
9
  old_new = self.method(:new)
11
10
  (class << self; self; end).send(:define_method, :new) do |*args|
12
11
  object = old_new.call(*args)
13
- Rdbc::Decorator.new(object, contract_class.new)
12
+ Rdbc::Decorator.new(contract_class.new(object))
14
13
  end
15
14
  end
16
15
  end
data/lib/rdbc/contract.rb CHANGED
@@ -1,55 +1,81 @@
1
1
  module Rdbc
2
2
  class Contract
3
- class << self
4
- include Translating
5
-
6
- def pre(method, &block)
7
- define_method(method_pre(method), &block)
8
- end
3
+ def self.method_pre(method)
4
+ method_with_prefix(method, 'pre')
5
+ end
9
6
 
10
- def post(method, &block)
11
- define_method(method_post(method), &block)
12
- end
7
+ def self.method_post(method)
8
+ method_with_prefix(method, 'post')
9
+ end
13
10
 
14
- def invariant(&block)
15
- define_method(:invariant, &block)
16
- end
11
+ def self.method_with_prefix(method, type)
12
+ (type + '_' + (operator?(method) ? operator(method) : method.to_s)).to_sym
17
13
  end
18
-
19
- include Translating
20
-
21
- def delegate_and_verify(object_pre, object)
22
- send_method_pre(method, object, *args)
23
- begin
24
- result = object.send(method, *args)
25
- rescue => exception
26
- send_invariant(object)
27
- send_method_post(method, object_pre, object, exception, result, *args)
28
- raise exception
29
- else
30
- send_invariant(@object)
31
- send_method_post(method, object_pre, object, exception, result, *args)
32
- result
33
- end
14
+
15
+ def self.operator?(method)
16
+ /^[A-Za-z_]/ !~ method.to_s
34
17
  end
35
-
36
- def send_method_pre(method, object, *args)
37
- method_pre = method_pre(method)
38
- if respond_to?(method_pre)
39
- send(method_pre, object, *args)
40
- end
18
+
19
+ def self.operator(method)
20
+ 'op_' + OPERATOR[method]
41
21
  end
42
-
43
- def send_invariant(object)
44
- if respond_to?(:invariant)
45
- invariant(object)
46
- end
22
+
23
+ OPERATOR = {
24
+ :[] => 'element_read',
25
+ :[]= => 'element_write',
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)
52
+ end
53
+
54
+ def self.post(method, &block)
55
+ define_method(method_post(method), &block)
56
+ end
57
+
58
+ def self.invariant(&block)
59
+ define_method(:invariant, &block)
47
60
  end
48
61
 
49
- def send_method_post(method, object_pre, object, exception, result, *args)
50
- method_post = method_post(method)
51
- if respond_to?(method_post)
52
- send(method_post, object_pre, object, exception, result, *args)
62
+ def initialize(object)
63
+ @object = object
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 = @object.send(method, *args) rescue result
71
+ send_if_respond_to(:invariant)
72
+ send_if_respond_to(@translator.method_post(method), result, *args)
73
+ result
74
+ end
75
+
76
+ def send_if_respond_to(method, *args)
77
+ if respond_to?(method)
78
+ send(method, *args)
53
79
  end
54
80
  end
55
81
  end
@@ -1,19 +1,20 @@
1
+ require 'blankslate'
2
+
1
3
  module Rdbc
2
- class Decorator
4
+ class Decorator < BlankSlate
3
5
  instance_methods.reject do |method|
4
6
  /__.*__/ === method
5
7
  end.each do |method|
6
8
  undef_method(method)
7
9
  end
8
10
 
9
- def initialize(object, contract)
10
- @object = object
11
+ def initialize(contract)
11
12
  @contract = contract
12
- @contract.send_invariant(@object)
13
+ @contract.send_if_respond_to(:invariant)
13
14
  end
14
15
 
15
16
  def method_missing(method, *args)
16
- @contract.delegate_and_verify(@object.clone, @object)
17
+ @contract.method_missing(method, *args)
17
18
  end
18
19
  end
19
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdbc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Armin Joellenbeck
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-08 00:00:00 +01:00
12
+ date: 2009-12-25 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,7 +26,6 @@ files:
26
26
  - lib/rdbc.rb
27
27
  - lib/rdbc/contract.rb
28
28
  - lib/rdbc/decorator.rb
29
- - lib/rdbc/translating.rb
30
29
  has_rdoc: true
31
30
  homepage: http://github.com/armin-joellenbeck/rdbc/tree/master
32
31
  licenses: []
@@ -1,50 +0,0 @@
1
- module Rdbc
2
- module Translating
3
- def method_pre(method)
4
- method_with_prefix(method, 'pre')
5
- end
6
-
7
- def method_post(method)
8
- method_with_prefix(method, 'post')
9
- end
10
-
11
- def method_with_prefix(method, type)
12
- (type + '_' + (operator?(method) ? operator(method) : method.to_s)).to_sym
13
- end
14
-
15
- def operator?(method)
16
- /^[A-Za-z_]/ !~ method.to_s
17
- end
18
-
19
- def operator(method)
20
- 'op_' + OPERATOR[method]
21
- end
22
-
23
- OPERATOR = {
24
- :[] => 'element_read',
25
- :[]= => 'element_write',
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
- end
50
- end