yarrow 0.6.0 → 0.6.1
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 +4 -4
- data/lib/yarrow/schema.rb +32 -21
- data/lib/yarrow/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30b0321a3081bb91c9f121e41e360b548eab71f03c4cbfded36709473e3cc586
|
4
|
+
data.tar.gz: 67ed41c8e4579d80e211fc08c125410b1f957adfe8e505d778020f02566efd33
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fcff8cbddd540f7a44d32411c5930a70fff1a86581b5e6efdfc2e1f1eae43afa729b3f254b56581d33bcaf4d30828b2409e3129284a1ba1221823c8a26758fdb
|
7
|
+
data.tar.gz: 485752729e471a4b33f55663f8e762f8d93d1fc9ec29763a2ead843bbda1bb662b210163c723c8dc7d71111693c703e780e8c6f1394c9c317ef254461a3e8d1c
|
data/lib/yarrow/schema.rb
CHANGED
@@ -11,24 +11,16 @@ module Yarrow
|
|
11
11
|
#
|
12
12
|
# Current design throws on error rather than returns a boolean result.
|
13
13
|
class Validator
|
14
|
-
# @param
|
15
|
-
def initialize(
|
16
|
-
@
|
17
|
-
if slot.is_a?(Array)
|
18
|
-
keys[slot[0]] = slot[1]
|
19
|
-
else
|
20
|
-
keys[slot.to_sym] = Type::Any
|
21
|
-
end
|
22
|
-
|
23
|
-
keys
|
24
|
-
end
|
14
|
+
# @param fields_spec [Hash] defines the slots in the schema to validate against
|
15
|
+
def initialize(fields_spec)
|
16
|
+
@spec = fields_spec
|
25
17
|
end
|
26
18
|
|
27
19
|
def check(fields)
|
28
|
-
raise "wrong number of args" unless fields.keys.length == @
|
20
|
+
raise "wrong number of args" unless fields.keys.length == @spec.keys.length
|
29
21
|
|
30
22
|
fields.keys.each do |key|
|
31
|
-
raise "key does not exist" unless @
|
23
|
+
raise "key does not exist" unless @spec.key?(key)
|
32
24
|
end
|
33
25
|
end
|
34
26
|
end
|
@@ -38,19 +30,38 @@ module Yarrow
|
|
38
30
|
# Ruby struct but wraps the constructor with method advice that handles
|
39
31
|
# validation (and eventually type coercion if !yagni).
|
40
32
|
class Value
|
41
|
-
def self.new(*slots, &block)
|
42
|
-
factory(*slots, &block)
|
33
|
+
def self.new(*slots, **fields, &block)
|
34
|
+
factory(*slots, **fields, &block)
|
43
35
|
end
|
44
36
|
|
45
|
-
def self.factory(*slots, &block)
|
46
|
-
|
37
|
+
def self.factory(*slots, **fields, &block)
|
38
|
+
if slots.empty? && fields.empty?
|
39
|
+
raise ArgumentError.new("missing attribute definition")
|
40
|
+
end
|
41
|
+
|
42
|
+
slots_spec, fields_spec = if fields.any?
|
43
|
+
raise ArgumentError.new("cannot use slots when field map is supplied") if slots.any?
|
44
|
+
[fields.keys, fields]
|
45
|
+
else
|
46
|
+
[slots, Hash[slots.map { |s| [s, Type::Any]}]]
|
47
|
+
end
|
48
|
+
|
49
|
+
validator = Validator.new(fields_spec)
|
50
|
+
|
51
|
+
struct = Struct.new(*slots_spec, keyword_init: true, &block)
|
47
52
|
|
48
|
-
struct
|
53
|
+
struct.define_method :initialize do |*args, **kwargs|
|
54
|
+
attr_values = if args.any?
|
55
|
+
raise ArgumentError.new("cannot mix slots and kwargs") if kwargs.any?
|
56
|
+
Hash[slots.zip(args)]
|
57
|
+
else
|
58
|
+
kwargs
|
59
|
+
end
|
49
60
|
|
50
|
-
|
51
|
-
validator.check(kwargs)
|
61
|
+
validator.check(attr_values)
|
52
62
|
# TODO: type coercion or mapping decision goes here
|
53
|
-
super(**
|
63
|
+
super(**attr_values)
|
64
|
+
|
54
65
|
freeze
|
55
66
|
end
|
56
67
|
|
data/lib/yarrow/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yarrow
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Rickerby
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-03-
|
11
|
+
date: 2021-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|