contracts 0.6 → 0.7

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
2
  SHA1:
3
- metadata.gz: 2aee00ef1803ee1ac8e84e96156494f7b7c4162a
4
- data.tar.gz: a3798c342c9b29170f523f23083baeb2553cbd83
3
+ metadata.gz: 065efb039a13c2917e10fc96dd77dd2fe858bc08
4
+ data.tar.gz: 98f3253cc22a5da08f4d3414361db7218c438107
5
5
  SHA512:
6
- metadata.gz: d1404d6dd2fe5212b9c7a8bfa682b982bf612161200e33bc14ab73609eaac0681b85540a182a3d4d2437d3446380241785d229b6a8145c5a8c3fda8e37f682f5
7
- data.tar.gz: 0912d5946702185218f94d1058577e92fd9f7b668e248ab4d796e67b620455f4a43e2a0e02d4871f8def9c8666feb3f4343c4f3f6d04d6c1a25b97541c7afd27
6
+ metadata.gz: b8486b6e85a38517371403aa4cb7b3e6795ee01a2170e663eab8ff772507971a8e7bfa050b3b98dabb8fc60772153f544a14e9e419025a7c899ebda682b82338
7
+ data.tar.gz: 44cfdb1101dbe48269e88c5758196e2bc11ce2238f70a791c0f62aa36d0754994dd00f49f4c8e0d98cca43a4b254a085cc83af3673964b6e31198ba66c14527e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- contracts (0.5)
4
+ contracts (0.6)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/TUTORIAL.md CHANGED
@@ -29,14 +29,17 @@ Copy this code into a file and run it:
29
29
 
30
30
  ```ruby
31
31
  require 'contracts'
32
- include Contracts
33
32
 
34
- Contract Num, Num => Num
35
- def add(a, b)
36
- a + b
33
+ class Math
34
+ include Contracts
35
+
36
+ Contract Num, Num => Num
37
+ def self.add(a, b)
38
+ a + b
39
+ end
37
40
  end
38
41
 
39
- puts add(1, "foo")
42
+ puts Math.add(1, "foo")
40
43
  ```
41
44
 
42
45
  You'll see a detailed error message like so:
@@ -60,7 +63,7 @@ This can be useful if you're in a repl and want to figure out how a function sho
60
63
 
61
64
  ## Builtin Contracts
62
65
 
63
- `Num` is one of the builtin contracts that contracts.ruby comes with. The builtin contracts are in the `Contracts` namespace. The easiest way to use them is to put `include Contracts` at the top of your file, but beware that they will pollute your namespace with new class names.
66
+ `Num` is one of the builtin contracts that contracts.ruby comes with. The builtin contracts are in the `Contracts` namespace. The easiest way to use them is to include the `Contracts` module in your class/module.
64
67
 
65
68
  contracts.ruby comes with a lot of builtin contracts, including:
66
69
 
@@ -401,7 +404,7 @@ If your failure callback returns `false`, the method that the contract is guardi
401
404
 
402
405
  ## Disabling contracts
403
406
 
404
- If you want to disable contracts, set the `NO_CONTRACTS` environment variable. This will disable contracts completely and you won't have a performance hit.
407
+ If you want to disable contracts, set the `NO_CONTRACTS` environment variable. This will disable contracts and you won't have a performance hit. Pattern matching will still work if you disable contracts in this way! With NO_CONTRACTS only pattern-matching contracts are defined.
405
408
 
406
409
  ## Method overloading
407
410
 
@@ -439,12 +442,12 @@ For an argument, each function will be tried in order. The first function that d
439
442
  This allows you write methods more declaratively, rather than using conditional branching. This feature is not only useful for recursion; you can use it to keep parallel use cases separate:
440
443
 
441
444
  ```ruby
442
- Contract And[Num, lambda{|n| n < 12 }] => Ticket
445
+ Contract lambda{|n| n < 12 } => Ticket
443
446
  def get_ticket(age)
444
447
  ChildTicket.new(age: age)
445
448
  end
446
449
 
447
- Contract And[Num, lambda{|n| n >= 12 }] => Ticket
450
+ Contract lambda{|n| n >= 12 } => Ticket
448
451
  def get_ticket(age)
449
452
  AdultTicket.new(age: age)
450
453
  end
@@ -486,7 +489,7 @@ A simple example:
486
489
  ```ruby
487
490
  class MyBirthday < Struct.new(:day, :month)
488
491
  include Contracts
489
- include Contracts:Invariants
492
+ include Contracts::Invariants
490
493
 
491
494
  Invariant(:day) { 1 <= day && day <= 31 }
492
495
  Invariant(:month) { 1 <= month && month <= 12 }
@@ -20,6 +20,7 @@ module Contracts
20
20
 
21
21
  unless eigenclass.respond_to?(:pop_decorators)
22
22
  eigenclass.extend(MethodDecorators)
23
+ eigenclass.send(:include, Contracts)
23
24
  end
24
25
 
25
26
  eigenclass.owner_class = base
@@ -24,7 +24,8 @@ module Contracts
24
24
 
25
25
  # Makes a method private
26
26
  def make_private(this)
27
- alias_target(this).class_eval { private name }
27
+ original_name = name
28
+ alias_target(this).class_eval { private original_name }
28
29
  end
29
30
 
30
31
  # Aliases original method to a special unique name, which is known
@@ -1,3 +1,3 @@
1
1
  module Contracts
2
- VERSION = "0.6"
2
+ VERSION = "0.7"
3
3
  end
@@ -107,6 +107,14 @@ RSpec.describe "Contracts:" do
107
107
  }.to raise_error(*error)
108
108
  end
109
109
  end
110
+
111
+ describe "builtin contracts usage" do
112
+ it "allows to use builtin contracts without namespacing and redundant Contracts inclusion" do
113
+ expect {
114
+ SingletonClassExample.add("55", 5.6)
115
+ }.to raise_error(ContractError, /Expected: Contracts::Num/)
116
+ end
117
+ end
110
118
  end
111
119
 
112
120
  describe "no contracts feature" do
@@ -449,6 +457,10 @@ RSpec.describe "Contracts:" do
449
457
  it "should raise an error if you try to access a private method" do
450
458
  expect { @o.a_private_method }.to raise_error
451
459
  end
460
+
461
+ it "should raise an error if you try to access a private method" do
462
+ expect { @o.a_really_private_method }.to raise_error
463
+ end
452
464
  end
453
465
 
454
466
  describe "inherited methods" do
@@ -214,6 +214,13 @@ class GenericExample
214
214
  end
215
215
  private :a_private_method
216
216
 
217
+ private
218
+
219
+ Contract nil => String
220
+ def a_really_private_method
221
+ "works for sure"
222
+ end
223
+
217
224
  end
218
225
 
219
226
  # for testing inheritance
@@ -323,6 +330,11 @@ class SingletonClassExample
323
330
  def hoge(str)
324
331
  "super#{str}"
325
332
  end
333
+
334
+ Contract Num, Num => Num
335
+ def add(a, b)
336
+ a + b
337
+ end
326
338
  end
327
339
  end
328
340
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contracts
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.6'
4
+ version: '0.7'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aditya Bhargava
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-07 00:00:00.000000000 Z
11
+ date: 2015-02-16 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This library provides contracts for Ruby. Contracts let you clearly express
14
14
  how your code behaves, and free you from writing tons of boilerplate, defensive