lab42_basic_constraints 0.1.4 → 0.1.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6e4746d8767d27b4401dac64edbcf42136b8a449d515bac68e21b134fc303da
4
- data.tar.gz: 73f7c0a153ab07bd1b8bb86c8c8fb1704ff69fdfc954bfae71ae0bf4fa49cd33
3
+ metadata.gz: 565e1a087c775182c1acd5da145eb753d5af7c3e9cd1dd15d14faa85f4d19c87
4
+ data.tar.gz: 839aae5929011f9e088db5fdaf869dc050bfcc85ec8286450e97ea6bcd75975f
5
5
  SHA512:
6
- metadata.gz: 656c5bf07850eb5376dd889658264917e65e5c9c51a35f0d520dc8c05c5b601ec175089ef07f09665105bd486dece4d0433a7f33c736d98487ac6671acbef0f3
7
- data.tar.gz: db8f8ede07ecd3f75bf980f4a97e733add8d00136dd67855f162e42f7aa92793fe3e88990478d93ee425c704f1aa9b89b45abb33c0af1914f3cf2d2673f0032d
6
+ metadata.gz: a9185a0999ba47fc278888cd04c02426a4cd02c897221d75a59abdca5166bbfa6ecfa7bf2439b95eb7af6cf0367b13713578f786fc5e40c65795306fa00d7392
7
+ data.tar.gz: 6097207f6d78749e7ef8f6a229f6a6a1ecdd1cf6608416bebbf3061acd193f4f06464e822f8e910692828da1cf364f463c5c91a725251d5a4a334de2ed25dce0
data/README.md CHANGED
@@ -75,6 +75,11 @@ Example: Constraints with and without defaults
75
75
 
76
76
  ```
77
77
 
78
+ Example: Avoiding the Exception, à la `Hash#fetch`
79
+ ```ruby
80
+ expect( BC.from_symbol(:positive_number).default{42} ).to eq(42)
81
+ ```
82
+
78
83
  A detailed description of all constraints can be found
79
84
  [here](speculations/constraints.md)
80
85
 
@@ -12,12 +12,17 @@ module Lab42
12
12
  Result.error("#{_show value} is not a legal #{@name}", error: Lab42::BasicConstraints::ConstraintError)
13
13
  end
14
14
 
15
- def default
16
- case @default
17
- when Proc
18
- @default.()
15
+ def default &blk
16
+ if @has_default
17
+ case @default
18
+ when Proc
19
+ @default.()
20
+ else
21
+ @default
22
+ end
19
23
  else
20
- @default
24
+ return blk.() if blk
25
+ raise Lab42::BasicConstraints::MissingDefaultError, "Constraint #{name} has no predefined default"
21
26
  end
22
27
  end
23
28
 
@@ -28,6 +33,7 @@ module Lab42
28
33
  else
29
34
  blk
30
35
  end
36
+ @has_default = true
31
37
  self
32
38
  end
33
39
 
@@ -43,7 +49,8 @@ module Lab42
43
49
 
44
50
  def initialize name, &blk
45
51
  @constraint = blk
46
- @default = -> {raise Lab42::BasicConstraints::MissingDefaultError, "Constraint #{name} has no predefined default"}
52
+ @has_default = false
53
+ @default = -> {}
47
54
  @name = name
48
55
  end
49
56
  end
@@ -1,5 +1,5 @@
1
1
  module Lab42
2
2
  module BasicConstraints
3
- VERSION = "0.1.4"
3
+ VERSION = "0.1.5"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab42_basic_constraints
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-20 00:00:00.000000000 Z
11
+ date: 2020-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lab42_result