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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c5a6e574c6a7e481f83ca95daada71f299edb932
4
- data.tar.gz: dedb9e24930b8a181197fa1a1cefe294b966cd5d
2
+ SHA256:
3
+ metadata.gz: bfef0f48bf225f759ac37b18a433be6c33cfd020415c909180a287a106499c93
4
+ data.tar.gz: 5150d9283c633ed037e074e24579f9824ab2bc3a4dfecd794fc4a7301d59c3b9
5
5
  SHA512:
6
- metadata.gz: 44746928c286fde8d894ca7bd7ce7cc4fd4dd8d52cebc77c5c0b9141df1b480383105423340d166481e5dec7fbbafbdaa70254079a8fa9ab5489bf6a59008746
7
- data.tar.gz: c8d5db4af17119799fb6d2535db1c0bfcf5c1d1e78a84079b85b37c92a75fc6a47da44682056cbb03102ebe1207aad978440d9bd74498ac90a0e73da33ff64d6
6
+ metadata.gz: 1c9cd985049db0689046eca4f07b90914cb1901f830e0ecfb9fdc28d95ccfecfe38eb5441a87e0196961f7cc272d0216fda26ab741d48e17061a05af21421fd9
7
+ data.tar.gz: b0ea3c0c9f7cb114ec7da8b7e27258bc849ad4f9b35b155ba6d3d717d0516f6c35ebcc94d20aec75b72b2a745c2c14232f4cfc034a20731050f16ce24c696ba5
data/.gitignore CHANGED
@@ -1,6 +1,9 @@
1
1
  .*.sw[pon]
2
2
  .AppleDouble
3
+ .byebug_history
3
4
  .rvmrc
4
5
  Gemfile.lock
5
6
  coverage
7
+ errors.lst
6
8
  pkg
9
+ tags
data/.travis.yml CHANGED
@@ -1,11 +1,14 @@
1
1
  rvm:
2
- - 1.9.3
3
- - 2.0.0
2
+ - 2.1
3
+ - 2.2
4
+ - 2.3
5
+ - 2.4
6
+ - 2.5
7
+ - 2.6
4
8
  - ruby-head
5
- - rbx-19mode
6
- - jruby-19mode
9
+ - jruby
7
10
  matrix:
8
11
  allow_failures:
9
12
  - rvm: ruby-head
10
- - rvm: jruby-19mode
13
+ - rvm: jruby
11
14
 
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
@@ -3,5 +3,3 @@
3
3
  source 'https://rubygems.org'
4
4
 
5
5
  gemspec
6
-
7
- gem 'utils'
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.0.1
1
+ 2.0.0
File without changes
@@ -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
@@ -1,12 +1,6 @@
1
- require 'protocol'
1
+ #!/usr/bin/env ruby
2
2
 
3
- Enumerating = Protocol do
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
@@ -1,3 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
1
3
  require 'protocol'
2
4
 
3
5
  # Small example of the template method pattern with Protocol. (I think, it was
@@ -72,6 +72,4 @@ class HelloWorld
72
72
  end
73
73
  end
74
74
 
75
- if $0 == __FILE__
76
- HelloWorld.main(*ARGV)
77
- end
75
+ HelloWorld.main(*ARGV)
data/examples/indexing.rb CHANGED
@@ -1,28 +1,21 @@
1
- require 'protocol'
1
+ #!/usr/bin/env ruby
2
2
 
3
- Indexing = Protocol {
4
- check_failure :error
3
+ require 'protocol/core'
5
4
 
6
- understand :[]
7
-
8
- understand :[]=
9
- }
5
+ class Array
6
+ conform_to Indexing
7
+ end
10
8
 
11
- if $0 == __FILE__
12
- class Array
13
- conform_to Indexing
14
- end
9
+ class Hash
10
+ conform_to Indexing
11
+ end
15
12
 
16
- class Hash
13
+ begin
14
+ class Proc
17
15
  conform_to Indexing
18
16
  end
19
-
20
- begin
21
- class Proc
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
- if $0 == __FILE__
55
- s = S.new
56
- puts s.top.inspect + " (nil)"
57
- puts s.empty?.to_s + " (true)"
58
- puts s.size.to_s + " (0)"
59
- begin
60
- s.pop
61
- rescue Protocol::CheckError => e
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