bm-typed 0.1.0 → 0.1.1

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
  SHA256:
3
- metadata.gz: 28fd1838d9a1659a003182955c8bb7d72e15cd231a0d68ab536373b188d200dc
4
- data.tar.gz: f40f3685b87a1271f2d516ca9b0718f5543da5422f106bf27d81f42404bf6dfc
3
+ metadata.gz: 9f429141987a2845ff72402f8fb9b49e052f92da390b4c7922a35c14f936cbfc
4
+ data.tar.gz: bd6e44b3def8e439828aeedad4b64cc4397d3bfdd1b90fe54ca0b3b733fa4de3
5
5
  SHA512:
6
- metadata.gz: d9d8e1a676753700e5301b84ebc14d0c63593958b6ff71e997e08d9053049956a34700bd566441b96e0048918bd6ad95bae7fae723be1d9c031edc36c59dee50
7
- data.tar.gz: e28ef39291fb44dd1f75e72773288c12d61c77aeab0d0d6be97aa4567e342bc0491dbbb731410a406113f23fbe191b7654f97730502f9dd41b8d951eaa3e1e22
6
+ metadata.gz: 7eea4da91207c22cda05f28bc37fd10990e0a582b1fae2acb238edbb5861dcc32a07dc94da543efa779b2309ffeaa0566f5aa088b00ebd811359cd9934a88660
7
+ data.tar.gz: a4f708f98dfcbb1e8cbb03188b6d6aa49969e8e22d6ea31a22d0cc95d1c7d4b7b32f030851a5e92f1988826a2d9150f58cb1c044becb879706202a4ed208bad0
data/lib/typed.rb CHANGED
@@ -28,7 +28,7 @@ module Typed
28
28
  end
29
29
 
30
30
  def value(expected_value)
31
- dry_constrained(eql: call(expected_value))
31
+ constrained(eql: call(expected_value))
32
32
  end
33
33
  end
34
34
 
@@ -99,8 +99,8 @@ module Typed
99
99
  }
100
100
  .constructor(input: Int | Float, swallow: [TypeError, ArgumentError]) { |value| ::Time.at(value) }
101
101
 
102
- UUID = String.dry_constrained(format: /\A[a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}\z/)
102
+ UUID = String.constrained(format: /\A[a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}\z/)
103
103
  .constructor(input: String, &:downcase)
104
104
 
105
- URL = String.dry_constrained(format: URI::DEFAULT_PARSER.make_regexp(%w[http https]))
105
+ URL = String.constrained(format: URI::DEFAULT_PARSER.make_regexp(%w[http https]))
106
106
  end
data/lib/typed/builder.rb CHANGED
@@ -52,11 +52,11 @@ module Typed
52
52
  end
53
53
 
54
54
  def instance(expected_class)
55
- dry_constrained(type: expected_class)
55
+ constrained(type: expected_class)
56
56
  end
57
57
 
58
58
  def enum(*values)
59
- dry_constrained(included_in: values.map { |value| call(value) })
59
+ constrained(included_in: values.map { |value| call(value) })
60
60
  end
61
61
 
62
62
  def |(other)
@@ -72,6 +72,25 @@ module Typed
72
72
  CoerceType.new(input, self, swallow, &block)
73
73
  end
74
74
 
75
+ def constrained(**dry_options, &constraint)
76
+ base = constraint ? ConstrainedType.new(self, &constraint) : self
77
+ base = base.dry_constrained(**dry_options) unless dry_options.empty?
78
+ base
79
+ end
80
+
81
+ def call(*args)
82
+ result = process((args + [Typed::Undefined]).first)
83
+ return result.value if result.ok
84
+
85
+ raise InvalidValue, result.message
86
+ end
87
+
88
+ def process(value)
89
+ Typed::Builder::Result.success(value)
90
+ end
91
+
92
+ protected
93
+
75
94
  def dry_constrained(**options)
76
95
  predicate = ::Dry::Logic::RuleCompiler.new(::Dry::Logic::Predicates).call(
77
96
  options.map { |key, val|
@@ -86,23 +105,6 @@ module Typed
86
105
  end
87
106
  end
88
107
 
89
- def constrained(&constraint)
90
- return self unless constraint
91
-
92
- ConstrainedType.new(self, &constraint)
93
- end
94
-
95
- def call(*args)
96
- result = process((args + [Typed::Undefined]).first)
97
- return result.value if result.ok
98
-
99
- raise InvalidValue, result.message
100
- end
101
-
102
- def process(value)
103
- Typed::Builder::Result.success(value)
104
- end
105
-
106
108
  private
107
109
 
108
110
  def expected_type(type)
data/lib/typed/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Typed
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bm-typed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frederic Terrazzoni