parametric 0.2.3 → 0.2.4

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: 3b64a30de45ecf284020b1acef587737974cc297895e16d711d3749488af9d38
4
- data.tar.gz: 45ae399dd90e0d986d34d32f9b240088710698fd00bd88310ae11034b821fce9
3
+ metadata.gz: 311f5350815420ada31b971cbb09b920cdbadcd7c67f988dde5f6d23f9ddeaa5
4
+ data.tar.gz: 7e07b16bcbca0f77f6258754d7f735e4c588a81de5cef6fb418bcd34a9cf6883
5
5
  SHA512:
6
- metadata.gz: c2ef5e0b0a3b0a7ad7995b0d873b9acb20d8ccce41fd52f64b14ebceeef19c5915d21bfaf561010714cb07bd05933d684c803612557f03337788c3c8fc1c9ac2
7
- data.tar.gz: a608ba745a4cd49abd6ee9301ef94398a63a629c20af85f97323d81a88af7bf97e8ce0418c7d436ab87b8ee62c7cad0adca9d38a2c4bcf4f1311b0c12e409dcc
6
+ metadata.gz: b83121daea8fb5665b7aee7e6ebcbe87409dee8756d5a62b9a63ab8becb03aeed7abb3202f52f8e14dfc3fe648d5fde97f80e1a51843524e16c828dffa1c442c
7
+ data.tar.gz: fa56771695aabacd98928f8f2e35774e2ddeef2d6655aea3c509fe8d5c8eab7952c44cb388d5e6a1945be5313869b21ef7f2c5854c87affe274c86ea604b39c6
data/README.md CHANGED
@@ -883,6 +883,20 @@ user.friends.first.valid? # false
883
883
  user.friends.first.errors['$.name'] # "is required and must be valid"
884
884
  ```
885
885
 
886
+ ### .new!(hash)
887
+
888
+ Instantiating structs with `.new!(hash)` will raise a `Parametric::InvalidStructError` exception if the data is validations fail. It will return the struct instance otherwise.
889
+
890
+ `Parametric::InvalidStructError` includes an `#errors` property to inspect the errors raised.
891
+
892
+ ```ruby
893
+ begin
894
+ user = User.new!(name: '')
895
+ rescue Parametric::InvalidStructError => e
896
+ e.errors['$.name'] # "is required and must be present"
897
+ end
898
+ ```
899
+
886
900
  ### Nested structs
887
901
 
888
902
  You can also pass separate struct classes in a nested schema definition.
@@ -1,6 +1,17 @@
1
1
  require 'parametric/dsl'
2
2
 
3
3
  module Parametric
4
+ class InvalidStructError < ArgumentError
5
+ attr_reader :errors
6
+ def initialize(struct)
7
+ @errors = struct.errors
8
+ msg = @errors.map do |k, strings|
9
+ "#{k} #{strings.join(', ')}"
10
+ end.join('. ')
11
+ super "#{struct.class} is not a valid struct: #{msg}"
12
+ end
13
+ end
14
+
4
15
  module Struct
5
16
  def self.included(base)
6
17
  base.send(:include, Parametric::DSL)
@@ -37,6 +48,12 @@ module Parametric
37
48
  attr_reader :_graph, :_results
38
49
 
39
50
  module ClassMethods
51
+ def new!(attrs = {})
52
+ st = new(attrs)
53
+ raise InvalidStructError.new(st) unless st.valid?
54
+ st
55
+ end
56
+
40
57
  # this hook is called after schema definition in DSL module
41
58
  def after_define_schema(schema)
42
59
  schema.fields.keys.each do |key|
@@ -1,3 +1,3 @@
1
1
  module Parametric
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
data/spec/struct_spec.rb CHANGED
@@ -274,4 +274,25 @@ describe Parametric::Struct do
274
274
  expect(copy.desc).to eq 'no change'
275
275
  expect(copy.friends.first.name).to eq 'jane'
276
276
  end
277
+
278
+ describe '.new!' do
279
+ it 'raises a useful exception if invalid data' do
280
+ klass = Class.new do
281
+ include Parametric::Struct
282
+
283
+ schema do
284
+ field(:title).type(:string).present
285
+ end
286
+ end
287
+
288
+ begin
289
+ klass.new!(title: '')
290
+ rescue Parametric::InvalidStructError => e
291
+ expect(e.errors['$.title']).not_to be nil
292
+ end
293
+
294
+ valid = klass.new!(title: 'foo')
295
+ expect(valid.title).to eq 'foo'
296
+ end
297
+ end
277
298
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parametric
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismael Celis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-14 00:00:00.000000000 Z
11
+ date: 2018-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler