lab42_basic_constraints 0.1.5 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 565e1a087c775182c1acd5da145eb753d5af7c3e9cd1dd15d14faa85f4d19c87
4
- data.tar.gz: 839aae5929011f9e088db5fdaf869dc050bfcc85ec8286450e97ea6bcd75975f
3
+ metadata.gz: 672bcf05278d0a451a503939dd0ebf0f69b95914be02d466c0024717747fab76
4
+ data.tar.gz: d398d43d6dcbb335df544545e4b6c1ea4795c7a4b086725e9fcb4595f87b3c48
5
5
  SHA512:
6
- metadata.gz: a9185a0999ba47fc278888cd04c02426a4cd02c897221d75a59abdca5166bbfa6ecfa7bf2439b95eb7af6cf0367b13713578f786fc5e40c65795306fa00d7392
7
- data.tar.gz: 6097207f6d78749e7ef8f6a229f6a6a1ecdd1cf6608416bebbf3061acd193f4f06464e822f8e910692828da1cf364f463c5c91a725251d5a4a334de2ed25dce0
6
+ metadata.gz: 98c2067be96925b8724360ed748b91fcb606ecce255867f1da5e11958148cf55006e61a6d1f9397d47c66e552698d9b9131475389dbad69c2c128ab57dabca52
7
+ data.tar.gz: 7839dd0e966966d27cd560bb9b28d43b1181ca1547f116058a6923176aa04ae0a5bc81fa6a812fa75372fb2fc57951f7a4966e0c45461dbd6af0aee7e93d379d
data/README.md CHANGED
@@ -17,28 +17,46 @@ Given That we alias `Lab42::BasicConstraints` to `BC`
17
17
  require "lab42/basic_constraints/alias"
18
18
  ```
19
19
 
20
+ Given
21
+ ```ruby
22
+ let :all_basic_constraints do
23
+ %i[
24
+ all_digits_string alphanumeric_string
25
+ bool
26
+ date date_time day
27
+ hour
28
+ int
29
+ lowercase_string
30
+ minute month
31
+ non_negative_int non_negative_number number
32
+ positive_int positive_number
33
+ second string
34
+ uppercase_string
35
+ year
36
+ ]
37
+ end
38
+ let :all_parametrized_constraints do
39
+ %i[ int_range limited_string ]
40
+ end
41
+
42
+ ```
20
43
  Then All Basic Constraints can be listed by means of `BC.all_constraints`
21
44
 
22
45
  ```ruby
23
- all_basic_constraints = %i[
24
- all_digits_string alphanumeric_string
25
- bool
26
- date date_time day
27
- hour
28
- int
29
- lowercase_string
30
- minute month
31
- non_empty_string non_negative_float non_negative_int non_negative_number number
32
- positive_float positive_int positive_number
33
- second string
34
- time
35
- uppercase_string
36
- year
37
- ]
38
-
39
- expect( BC.all_constraints ).to eq(all_basic_constraints)
46
+
47
+ expect( Set.new(BC.all_constraints) ).to eq(Set.new(all_basic_constraints + all_parametrized_constraints))
48
+ ```
49
+
50
+ And they are implemented of course
51
+ ```ruby
52
+ all_basic_constraints.each do | constraint_name |
53
+ expect( BC.from_symbol(constraint_name) ).to be_a(BC::Constraint)
54
+ end
55
+ expect( BC.int_range(min: 0) ).to be_a(BC::Constraint)
56
+ expect( BC.limited_string(max: 10) ).to be_a(BC::Constraint)
40
57
  ```
41
58
 
59
+
42
60
  ## So we got the ball, now use it!
43
61
 
44
62
  The ball, BTW, is a `Lab42::Result` see [lab42_result gem](https://rubygems.org/gems/lab42_result/) for more info
@@ -80,16 +98,6 @@ Example: Avoiding the Exception, à la `Hash#fetch`
80
98
  expect( BC.from_symbol(:positive_number).default{42} ).to eq(42)
81
99
  ```
82
100
 
83
- A detailed description of all constraints can be found
84
- [here](speculations/constraints.md)
85
-
86
- ## Context We are still in Beta and therefor
87
-
88
- Example: Some constraints are not yet implemented
89
-
90
- ```ruby
91
- expect{ BC.all_digits_string }.to raise_error(NotImplementedError)
92
- ```
93
101
 
94
102
  ## Context Time's a Sticky Wicket
95
103
 
@@ -118,7 +126,7 @@ Example: Illegal Dates
118
126
  ```ruby
119
127
  d.("2020-00-01") in [raised, message ]
120
128
  expect( raised ).to eq(error)
121
- expect( message ).to eq("2020-00-01 is not a legal date")
129
+ expect( message ).to eq(%{"2020-00-01" is not a legal date})
122
130
  ```
123
131
 
124
132
  ## Context Equality More Of A Tricky Sticker
@@ -145,6 +153,12 @@ Then we can compare as follows
145
153
  expect( def42.default ).to eq(42)
146
154
  ```
147
155
 
156
+ ## Detailed Description of all Constraints
157
+
158
+ [date_and_time_constraints](date_and_time_constraints.md )
159
+ [div_constraints](div_constraints.md )
160
+ [string_constraints](string_constraints.md )
161
+
148
162
  # LICENSE
149
163
 
150
164
  Copyright 2020 Robert Dober robert.dober@gmail.com
@@ -2,40 +2,37 @@ require_relative "basic_constraints/implementation.rb"
2
2
  module Lab42
3
3
  module BasicConstraints extend self
4
4
  Constraints = {
5
- all_digits_string: :not_yet_implemented,
6
- alphanumeric_string: :not_yet_implemented,
5
+ all_digits_string: :all_digits_string,
6
+ alphanumeric_string: :alphanumeric_string,
7
7
 
8
8
  bool: ->(x){[false, true].include? x},
9
9
 
10
10
  date: :date,
11
- date_time: :not_yet_implemented,
12
- day: :not_yet_implemented,
11
+ date_time: :date_time,
12
+ day: :day,
13
13
 
14
- hour: :not_yet_implemented,
14
+ hour: :hour,
15
15
 
16
16
  int: Integer,
17
+ int_range: :int_range,
17
18
 
18
- lowercase_string: :not_yet_implemented,
19
+ limited_string: :limited_string,
20
+ lowercase_string: :lowercase_string,
19
21
 
20
- minute: :not_yet_implemented,
21
- month: :not_yet_implemented,
22
+ minute: :minute,
23
+ month: :month,
22
24
 
23
- non_empty_string: :not_yet_implemented,
24
- non_negative_float: :not_yet_implemented,
25
25
  non_negative_int: :non_negative_int,
26
- non_negative_number: :not_yet_implemented,
27
- number: :not_yet_implemented,
26
+ non_negative_number: :non_negative_number,
27
+ number: Numeric,
28
28
 
29
- positive_float: :not_yet_implemented,
30
29
  positive_int: :positive_int,
31
30
  positive_number: :positive_number,
32
31
 
33
- second: :not_yet_implemented,
32
+ second: :second,
34
33
  string: String,
35
34
 
36
- time: :not_yet_implemented,
37
-
38
- uppercase_string: :not_yet_implemented,
35
+ uppercase_string: :uppercase_string,
39
36
 
40
37
  year: :year
41
38
  }
@@ -42,6 +42,8 @@ module Lab42
42
42
  case value
43
43
  when NilClass
44
44
  "nil"
45
+ when String
46
+ value.inspect
45
47
  else
46
48
  value
47
49
  end
@@ -56,3 +58,4 @@ module Lab42
56
58
  end
57
59
  end
58
60
  end
61
+ require_relative "constraints/int"
@@ -0,0 +1,19 @@
1
+ require_relative "../helpers/range_helper.rb"
2
+ module Lab42
3
+ module BasicConstraints
4
+ class Constraint
5
+ class IntConstraint < Constraint
6
+
7
+ private
8
+ def initialize name, range: nil, min: nil, max: nil
9
+ super(name)
10
+ range = Helpers::RangeHelper.make_range!(range: range, min: min, max: max)
11
+ @constraint = -> (value) {
12
+ Integer === value && range === value
13
+ }
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,29 @@
1
+ module Lab42
2
+ module BasicConstraints
3
+ module Helpers
4
+ module RangeHelper extend self
5
+
6
+ def make_range(range: nil, min: nil, max: nil)
7
+ return unless range || min || max
8
+ raise ArgumentError, "cannot provide min or max with range" if
9
+ range && (min || max)
10
+ range || _make_min_max_range(min, max)
11
+ end
12
+
13
+ def make_range!(range: nil, min: nil, max: nil)
14
+ make_range(range: range, min: min, max: max)
15
+ .tap do |range|
16
+ raise ArgumentError, "Must provide either range or min or max" unless range
17
+ end
18
+ end
19
+
20
+ private
21
+ def _make_min_max_range(min, max)
22
+ min ||= 0
23
+ max ||= Float::INFINITY
24
+ min..max
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,9 +1,38 @@
1
1
  require_relative "constraint"
2
+ require_relative "helpers/range_helper"
2
3
  module Lab42
3
4
  module BasicConstraints
4
5
 
5
6
  module Implementation extend self
6
7
 
8
+ IntConstraints = {
9
+ day: 1..31,
10
+ hour: 0..23,
11
+ minute: 0..59,
12
+ month: 1..12,
13
+ second: 0..59
14
+ }
15
+ IntConstraints.each do |constraint_name, constraint_range|
16
+ define_method constraint_name do
17
+ Constraint::IntConstraint.new(constraint_name, range: constraint_range)
18
+ end
19
+ end
20
+
21
+ def int_range(range: nil, min: nil, max: nil)
22
+ Constraint::IntConstraint.new(:int_range, range: range, min: min, max: max)
23
+ end
24
+
25
+ StringConstraints = {
26
+ all_digits_string: %r{\A\d*\z},
27
+ alphanumeric_string: %r{\A[[:alnum:]]*\z}
28
+ }
29
+
30
+ StringConstraints.each do |constraint_name, constraint_rgx|
31
+ define_method constraint_name do |size: nil, min: nil, max: nil|
32
+ _regex_constraint(constraint_name, constraint_rgx, size: size, min: min, max: max)
33
+ end
34
+ end
35
+
7
36
  def date default: nil
8
37
  require "date"
9
38
  Constraint.new(:date) do |value|
@@ -14,29 +43,55 @@ module Lab42
14
43
  end
15
44
  end
16
45
 
17
- def non_negative_int
18
- Constraint.new :non_negative_int do |value|
19
- Integer === value && value >= 0
46
+ def date_time default: nil
47
+ require "date"
48
+ Constraint.new(:date_time) do |value|
49
+ DateTime.parse(value) rescue false
50
+ end
51
+ .set_default default do
52
+ DateTime.now.iso8601
53
+ end
54
+ end
55
+
56
+ def limited_string size: nil, min: nil, max: nil
57
+ size_range = Helpers::RangeHelper.make_range!(range: size, min: min, max: max)
58
+ Constraint.new(:limited_string) do |value|
59
+ String === value && size_range === value.size
60
+ end
61
+ end
62
+
63
+ def lowercase_string
64
+ Constraint.new :lowercase_string do |value|
65
+ String === value && value.downcase == value
20
66
  end
67
+ end
68
+
69
+ def non_negative_int
70
+ Constraint::IntConstraint.new(:non_negative_int, min: 0)
21
71
  .set_default(0)
22
72
  end
23
73
 
24
- def positive_int
25
- Constraint.new :positive_int do
26
- Integer === _1 && _1 > 0
74
+ def non_negative_number
75
+ Constraint.new :non_negative_number do |value|
76
+ Numeric === value && !(Complex === value) && value >= 0
27
77
  end
78
+ end
79
+
80
+ def positive_int
81
+ Constraint::IntConstraint.new(:positive_int, min: 1)
28
82
  .set_default(1)
29
83
  end
30
84
 
31
85
  def positive_number
32
86
  Constraint.new :positive_number do |value|
33
- Integer === value && value > 0 ||
34
- Float === value && value > 0.0
87
+ Numeric === value && value > 0
35
88
  end
36
89
  end
37
90
 
38
- def not_yet_implemented
39
- raise NotImplementedError
91
+ def uppercase_string
92
+ Constraint.new :uppercase_string do |value|
93
+ String === value && value.upcase == value
94
+ end
40
95
  end
41
96
 
42
97
  def year
@@ -47,6 +102,22 @@ module Lab42
47
102
  Time.now.utc.year
48
103
  end
49
104
  end
105
+
106
+
107
+ private
108
+
109
+ def _regex_constraint(name, rgx, size: nil, min: nil, max: nil)
110
+ range_size = Helpers::RangeHelper.make_range(range: size, min: min, max: max)
111
+ if range_size
112
+ Constraint.new(name) do |value|
113
+ String === value && range_size === value.size && rgx === value
114
+ end
115
+ else
116
+ Constraint.new(name) do |value|
117
+ String === value && rgx === value
118
+ end
119
+ end
120
+ end
50
121
  end
51
122
  end
52
123
  end
@@ -1,5 +1,5 @@
1
1
  module Lab42
2
2
  module BasicConstraints
3
- VERSION = "0.1.5"
3
+ VERSION = "0.2.0"
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.5
4
+ version: 0.2.0
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-22 00:00:00.000000000 Z
11
+ date: 2020-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lab42_result
@@ -91,6 +91,8 @@ files:
91
91
  - lib/lab42/basic_constraints.rb
92
92
  - lib/lab42/basic_constraints/alias.rb
93
93
  - lib/lab42/basic_constraints/constraint.rb
94
+ - lib/lab42/basic_constraints/constraints/int.rb
95
+ - lib/lab42/basic_constraints/helpers/range_helper.rb
94
96
  - lib/lab42/basic_constraints/implementation.rb
95
97
  - lib/lab42/basic_constraints/version.rb
96
98
  homepage: https://bitbucket.org/robertdober/lab42_basic_constraints