protocol 1.0.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +3 -0
- data/.travis.yml +8 -5
- data/.utilsrc +25 -0
- data/Gemfile +0 -2
- data/README.rdoc +0 -19
- data/Rakefile +2 -19
- data/VERSION +1 -1
- data/examples/assignments.rb +0 -0
- data/examples/comparing.rb +5 -1
- data/examples/enumerating.rb +4 -11
- data/examples/game.rb +2 -0
- data/examples/hello_world_patternitis.rb +1 -3
- data/examples/indexing.rb +13 -20
- data/examples/queue.rb +7 -4
- data/examples/stack.rb +21 -21
- data/examples/synchronizing.rb +91 -0
- data/lib/protocol.rb +1 -10
- data/lib/protocol/core.rb +24 -0
- data/lib/protocol/message.rb +0 -7
- data/lib/protocol/protocol_module.rb +29 -47
- data/lib/protocol/version.rb +1 -1
- data/lib/protocol/xt.rb +10 -5
- data/protocol.gemspec +29 -25
- data/tests/protocol_core_test.rb +79 -0
- data/tests/protocol_method_parser_test.rb +51 -50
- data/tests/protocol_test.rb +19 -51
- metadata +35 -19
- data/examples/locking.rb +0 -111
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bfef0f48bf225f759ac37b18a433be6c33cfd020415c909180a287a106499c93
|
4
|
+
data.tar.gz: 5150d9283c633ed037e074e24579f9824ab2bc3a4dfecd794fc4a7301d59c3b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1c9cd985049db0689046eca4f07b90914cb1901f830e0ecfb9fdc28d95ccfecfe38eb5441a87e0196961f7cc272d0216fda26ab741d48e17061a05af21421fd9
|
7
|
+
data.tar.gz: b0ea3c0c9f7cb114ec7da8b7e27258bc849ad4f9b35b155ba6d3d717d0516f6c35ebcc94d20aec75b72b2a745c2c14232f4cfc034a20731050f16ce24c696ba5
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/.utilsrc
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# vim: set ft=ruby:
|
2
|
+
|
3
|
+
search do
|
4
|
+
prune_dirs /\A(\.svn|\.git|CVS|tmp|tags|coverage|pkg)\z/
|
5
|
+
skip_files /(\A\.|\.sw[pon]\z|\.(log|fnm|jpg|jpeg|png|pdf|svg)\z|tags|~\z)/i
|
6
|
+
end
|
7
|
+
|
8
|
+
discover do
|
9
|
+
prune_dirs /\A(\.svn|\.git|CVS|tmp|tags|coverage|pkg)\z/
|
10
|
+
skip_files /(\A\.|\.sw[pon]\z|\.log\z|~\z)/
|
11
|
+
binary false
|
12
|
+
end
|
13
|
+
|
14
|
+
strip_spaces do
|
15
|
+
prune_dirs /\A(\..*|CVS|pkg)\z/
|
16
|
+
skip_files /(\A\.|\.sw[pon]\z|\.log\z|~\z)/
|
17
|
+
end
|
18
|
+
|
19
|
+
probe do
|
20
|
+
test_framework :'test-unit'
|
21
|
+
end
|
22
|
+
|
23
|
+
ssh_tunnel do
|
24
|
+
terminal_multiplexer :tmux
|
25
|
+
end
|
data/Gemfile
CHANGED
data/README.rdoc
CHANGED
@@ -217,22 +217,3 @@ shows how:
|
|
217
217
|
Defining protocols and checking against conformance doesn't get in the way of
|
218
218
|
Ruby's duck typing, but you can still use protocols to define, document, and
|
219
219
|
check implementations that you expect from client code.
|
220
|
-
|
221
|
-
==== Error modes in Protocols
|
222
|
-
|
223
|
-
You can set different error modes for your protocols. By default the mode is
|
224
|
-
set to :error, and a failed protocol conformance check raises a CheckError (a
|
225
|
-
marker module) exception. Alternatively you can set the error mode to
|
226
|
-
:warning with:
|
227
|
-
|
228
|
-
Foo = Protocol do
|
229
|
-
check_failure :warning
|
230
|
-
end
|
231
|
-
|
232
|
-
during Protocol definition or later
|
233
|
-
|
234
|
-
Foo.check_failure :warning
|
235
|
-
|
236
|
-
In :warning mode no execptions are raised, only a warning is printed to
|
237
|
-
STDERR. If you set the error mode via Protocol::ProtocolModule#check_failure
|
238
|
-
to :none, nothing will happen on conformance check failures.
|
data/Rakefile
CHANGED
@@ -17,26 +17,9 @@ for methods specified in a protocol.
|
|
17
17
|
EOT
|
18
18
|
licenses << 'GPL-2'
|
19
19
|
test_dir 'tests'
|
20
|
-
ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', 'coverage', '.rvmrc', '.AppleDouble'
|
20
|
+
ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', 'coverage', '.rvmrc', '.AppleDouble', '.byebug_history', 'tags', 'errors.lst'
|
21
21
|
readme 'README.rdoc'
|
22
22
|
dependency 'ruby_parser', '~> 3.0'
|
23
23
|
development_dependency 'simplecov'
|
24
|
-
|
25
|
-
install_library do
|
26
|
-
file = 'lib/protocol.rb'
|
27
|
-
dest = CONFIG["sitelibdir"]
|
28
|
-
install(file, dest)
|
29
|
-
|
30
|
-
dest = File.join(CONFIG["sitelibdir"], 'protocol')
|
31
|
-
mkdir_p dest
|
32
|
-
for file in Dir['lib/protocol/*.rb']
|
33
|
-
install(file, dest)
|
34
|
-
end
|
35
|
-
|
36
|
-
dest = File.join(CONFIG["sitelibdir"], 'protocol', 'method_parser')
|
37
|
-
mkdir_p dest
|
38
|
-
for file in Dir['lib/protocol/method_parser/*.rb']
|
39
|
-
install(file, dest)
|
40
|
-
end
|
41
|
-
end
|
24
|
+
development_dependency 'test-unit'
|
42
25
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
2.0.0
|
data/examples/assignments.rb
CHANGED
File without changes
|
data/examples/comparing.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
|
2
|
+
#!/usr/bin/env ruby
|
3
|
+
|
1
4
|
require 'protocol/core'
|
2
5
|
|
3
6
|
class Person
|
@@ -32,7 +35,8 @@ begin
|
|
32
35
|
|
33
36
|
conform_to Comparing
|
34
37
|
end
|
35
|
-
puts "Should have thrown Protocol::CheckFailed!"
|
36
38
|
rescue Protocol::CheckFailed => e
|
37
39
|
p e
|
40
|
+
else
|
41
|
+
puts "Should have thrown Protocol::CheckFailed!"
|
38
42
|
end
|
data/examples/enumerating.rb
CHANGED
@@ -1,12 +1,6 @@
|
|
1
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
|
4
|
-
# Iterate over each element of this Enumerating class and pass it to the
|
5
|
-
# _block_.
|
6
|
-
def each(&block) end
|
7
|
-
|
8
|
-
include Enumerable
|
9
|
-
end
|
3
|
+
require 'protocol/core'
|
10
4
|
|
11
5
|
begin
|
12
6
|
class FailAry
|
@@ -19,9 +13,10 @@ begin
|
|
19
13
|
|
20
14
|
conform_to Enumerating
|
21
15
|
end
|
22
|
-
puts "Should have thrown Protocol::CheckFailed!"
|
23
16
|
rescue Protocol::CheckFailed => e
|
24
17
|
p e # => "Enumerating#each(0&): expected a block argument for FailAry"
|
18
|
+
else
|
19
|
+
puts "Should have thrown Protocol::CheckFailed!"
|
25
20
|
end
|
26
21
|
|
27
22
|
class Ary
|
@@ -40,8 +35,6 @@ puts Ary.new.map { |x| x * x }.inspect + " ([1, 4, 9])"
|
|
40
35
|
puts Ary.conform_to?(Enumerating).to_s + " (true)"
|
41
36
|
puts Ary.new.conform_to?(Enumerating).to_s + " (true)"
|
42
37
|
|
43
|
-
Enumerating.check_failure :none
|
44
|
-
|
45
38
|
class FailAry2
|
46
39
|
def initialize
|
47
40
|
@ary = [1, 2, 3]
|
data/examples/game.rb
CHANGED
data/examples/indexing.rb
CHANGED
@@ -1,28 +1,21 @@
|
|
1
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
|
4
|
-
check_failure :error
|
3
|
+
require 'protocol/core'
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
}
|
5
|
+
class Array
|
6
|
+
conform_to Indexing
|
7
|
+
end
|
10
8
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
9
|
+
class Hash
|
10
|
+
conform_to Indexing
|
11
|
+
end
|
15
12
|
|
16
|
-
|
13
|
+
begin
|
14
|
+
class Proc
|
17
15
|
conform_to Indexing
|
18
16
|
end
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
conform_to Indexing
|
23
|
-
end
|
17
|
+
rescue Protocol::CheckFailed => e
|
18
|
+
p e
|
19
|
+
else
|
24
20
|
puts "Should have thrown Protocol::CheckFailed!"
|
25
|
-
rescue Protocol::CheckFailed => e
|
26
|
-
p e
|
27
|
-
end
|
28
21
|
end
|
data/examples/queue.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
1
3
|
require 'protocol'
|
2
4
|
|
3
5
|
# Queue + Observer example.
|
@@ -27,8 +29,7 @@ end
|
|
27
29
|
|
28
30
|
class SneakyO
|
29
31
|
def after_enq(q)
|
30
|
-
puts "Enqueued."
|
31
|
-
q.deq
|
32
|
+
puts "Enqueued, actually Sneakily dequeued #{q.deq}."
|
32
33
|
end
|
33
34
|
|
34
35
|
def after_deq(q)
|
@@ -130,9 +131,10 @@ q.observer = O.new
|
|
130
131
|
should_be q.empty?, true
|
131
132
|
begin
|
132
133
|
q.deq
|
133
|
-
puts "Should have thrown Protocol::CheckFailed!"
|
134
134
|
rescue Protocol::CheckError => e
|
135
135
|
p e
|
136
|
+
else
|
137
|
+
puts "Should have thrown Protocol::CheckFailed!"
|
136
138
|
end
|
137
139
|
should_be q.empty?, true
|
138
140
|
should_be q.size, 0
|
@@ -151,7 +153,8 @@ should_be q.deq, 2
|
|
151
153
|
should_be q.empty?, true
|
152
154
|
begin
|
153
155
|
q.enq 7
|
154
|
-
puts "Should have thrown Protocol::CheckFailed!"
|
155
156
|
rescue Protocol::CheckError => e
|
156
157
|
p e
|
158
|
+
else
|
159
|
+
puts "Should have thrown Protocol::CheckFailed!"
|
157
160
|
end
|
data/examples/stack.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
1
3
|
require 'protocol'
|
2
4
|
|
3
5
|
# Classical Stack example.
|
@@ -51,25 +53,23 @@ class S
|
|
51
53
|
conform_to StackProtocol
|
52
54
|
end
|
53
55
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
p e # => #<Protocol::PreconditionCheckError: StackProtocol#empty?(0): precondition failed for S>
|
63
|
-
end
|
64
|
-
puts s.empty?.to_s + " (true)"
|
65
|
-
s.push 2
|
66
|
-
puts s.empty?.to_s + " (false)"
|
67
|
-
puts s.size.to_s + " (1)"
|
68
|
-
puts s.top.to_s + " (2)"
|
69
|
-
s.push 4
|
70
|
-
puts s.top.to_s + " (4)"
|
71
|
-
puts s.size.to_s + " (2)"
|
72
|
-
puts s.pop.to_s + " (4)"
|
73
|
-
puts s.top.to_s + " (2)"
|
74
|
-
puts s.size.to_s + " (1)"
|
56
|
+
s = S.new
|
57
|
+
puts s.top.inspect + " (nil)"
|
58
|
+
puts s.empty?.to_s + " (true)"
|
59
|
+
puts s.size.to_s + " (0)"
|
60
|
+
begin
|
61
|
+
s.pop
|
62
|
+
rescue Protocol::CheckError => e
|
63
|
+
p e # => #<Protocol::PreconditionCheckError: StackProtocol#empty?(0): precondition failed for S>
|
75
64
|
end
|
65
|
+
puts s.empty?.to_s + " (true)"
|
66
|
+
s.push 2
|
67
|
+
puts s.empty?.to_s + " (false)"
|
68
|
+
puts s.size.to_s + " (1)"
|
69
|
+
puts s.top.to_s + " (2)"
|
70
|
+
s.push 4
|
71
|
+
puts s.top.to_s + " (4)"
|
72
|
+
puts s.size.to_s + " (2)"
|
73
|
+
puts s.pop.to_s + " (4)"
|
74
|
+
puts s.top.to_s + " (2)"
|
75
|
+
puts s.size.to_s + " (1)"
|
@@ -0,0 +1,91 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'protocol/core'
|
4
|
+
require 'thread'
|
5
|
+
require 'tempfile'
|
6
|
+
|
7
|
+
class FileMutex
|
8
|
+
def initialize
|
9
|
+
@tempfile = Tempfile.new 'file-mutex'
|
10
|
+
end
|
11
|
+
|
12
|
+
def path
|
13
|
+
@tempfile.path
|
14
|
+
end
|
15
|
+
|
16
|
+
def lock
|
17
|
+
puts "Synchronizing '#{path}'."
|
18
|
+
@tempfile.flock File::LOCK_EX
|
19
|
+
end
|
20
|
+
|
21
|
+
def unlock
|
22
|
+
puts "Unlocking '#{path}'."
|
23
|
+
@tempfile.flock File::LOCK_UN
|
24
|
+
end
|
25
|
+
|
26
|
+
conform_to Synchronizing
|
27
|
+
end
|
28
|
+
|
29
|
+
FileMutex.conform_to? Synchronizing # => true
|
30
|
+
FileMutex.new.conform_to? Synchronizing # => true
|
31
|
+
|
32
|
+
# Outputs something like:
|
33
|
+
# Synchronizing '...'.
|
34
|
+
# Synchronized with '...'..
|
35
|
+
# Unlocking '...'.
|
36
|
+
p mutex = FileMutex.new
|
37
|
+
mutex.synchronize do
|
38
|
+
puts "Synchronized with '#{mutex.path}'."
|
39
|
+
end
|
40
|
+
|
41
|
+
class MemoryMutex
|
42
|
+
def initialize
|
43
|
+
@mutex = Mutex.new
|
44
|
+
end
|
45
|
+
|
46
|
+
def lock
|
47
|
+
@mutex.lock
|
48
|
+
end
|
49
|
+
|
50
|
+
def unlock
|
51
|
+
@mutex.unlock
|
52
|
+
end
|
53
|
+
|
54
|
+
conform_to Synchronizing # actually Mutex itself would conform as well ;)
|
55
|
+
end
|
56
|
+
|
57
|
+
p mutex = MemoryMutex.new
|
58
|
+
mutex.synchronize do
|
59
|
+
puts "Synchronized in memory."
|
60
|
+
end
|
61
|
+
|
62
|
+
puts MemoryMutex.conform_to?(Synchronizing).to_s + ' (true)'
|
63
|
+
puts MemoryMutex.new.conform_to?(Synchronizing).to_s + ' (true)'
|
64
|
+
|
65
|
+
class MyClass
|
66
|
+
def initialize
|
67
|
+
@mutex = FileMutex.new
|
68
|
+
end
|
69
|
+
|
70
|
+
attr_reader :mutex
|
71
|
+
|
72
|
+
def mutex=(mutex)
|
73
|
+
Synchronizing =~ mutex
|
74
|
+
@mutex = mutex
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
obj = MyClass.new
|
79
|
+
p obj.mutex # => #<FileMutex:0xb788f9ac @tempfile=#<File:/tmp/file-mutex.26553.2>>
|
80
|
+
begin
|
81
|
+
obj.mutex = Object.new
|
82
|
+
rescue Protocol::CheckFailed => e
|
83
|
+
p e
|
84
|
+
else
|
85
|
+
puts "Should have thrown Protocol::CheckFailed!"
|
86
|
+
end
|
87
|
+
p obj.mutex = MemoryMutex.new # => #<MemoryMutex:0xb788f038 @mutex=#<Mutex:0xb788eea8>>
|
88
|
+
# This works as well:
|
89
|
+
obj.mutex = Mutex.new
|
90
|
+
puts Synchronizing.check(Mutex).to_s + ' (true)'
|
91
|
+
puts Mutex.conform_to?(Synchronizing).to_s + ' (true)'
|
data/lib/protocol.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require 'protocol/version'
|
2
1
|
|
3
2
|
module Protocol
|
3
|
+
require 'protocol/version'
|
4
4
|
require 'protocol/method_parser/ruby_parser'
|
5
5
|
require 'protocol/utilities'
|
6
6
|
require 'protocol/protocol_module'
|
@@ -9,13 +9,4 @@ module Protocol
|
|
9
9
|
require 'protocol/message'
|
10
10
|
require 'protocol/errors'
|
11
11
|
require 'protocol/xt'
|
12
|
-
|
13
|
-
# The legal check modes, that influence the behaviour of the conform_to
|
14
|
-
# method in a class definition:
|
15
|
-
#
|
16
|
-
# - :error -> raises a CheckFailed exception, containing other
|
17
|
-
# CheckError exceptions.
|
18
|
-
# - :warning -> prints a warning to STDERR.
|
19
|
-
# - :none -> does nothing.
|
20
|
-
CHECK_MODES = [ :error, :warning, :none ]
|
21
12
|
end
|