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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b1a14b73412ec27fad2ff75bd7944f1ef25f241779c532d3449339a436e4c32
4
- data.tar.gz: 2cf2db75cc8c107b1d1bcbb6a79ad7fd92a6ba32e27413f2057158f7b8bb3c8a
3
+ metadata.gz: 30b0321a3081bb91c9f121e41e360b548eab71f03c4cbfded36709473e3cc586
4
+ data.tar.gz: 67ed41c8e4579d80e211fc08c125410b1f957adfe8e505d778020f02566efd33
5
5
  SHA512:
6
- metadata.gz: 436cec2447208c2641d4ac332cbb1910b7b016983a0630782282a28fe34e7a929b4661f424f0002d937854316d00f6d822ec05407f40185a7d5bbd642a2b88d2
7
- data.tar.gz: bd798c5d999b7d2665209778d507bfe9b54da98473850dd66b62c60391a270f93aa00d913315a7b29e6c92e7cb436f64fb89b692ecb4ea2da8bad5387d43b0b6
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 slots [Array<Symbol>, <Hash>] defines the slots in the schema to validate against
15
- def initialize(slots)
16
- @keys = slots.reduce({}) do |keys, slot|
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 == @keys.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 @keys.key?(key)
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
- validator = Validator.new(slots)
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 = Struct.new(*slots, keyword_init: true, &block)
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
- struct.define_method :initialize do |kwargs|
51
- validator.check(kwargs)
61
+ validator.check(attr_values)
52
62
  # TODO: type coercion or mapping decision goes here
53
- super(**kwargs)
63
+ super(**attr_values)
64
+
54
65
  freeze
55
66
  end
56
67
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  module Yarrow
3
3
  APP_NAME = 'Yarrow'
4
- VERSION = '0.6.0'
4
+ VERSION = '0.6.1'
5
5
  end
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.0
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-02 00:00:00.000000000 Z
11
+ date: 2021-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie