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 +4 -4
- data/README.md +14 -0
- data/lib/parametric/struct.rb +17 -0
- data/lib/parametric/version.rb +1 -1
- data/spec/struct_spec.rb +21 -0
- 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: 311f5350815420ada31b971cbb09b920cdbadcd7c67f988dde5f6d23f9ddeaa5
|
4
|
+
data.tar.gz: 7e07b16bcbca0f77f6258754d7f735e4c588a81de5cef6fb418bcd34a9cf6883
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
data/lib/parametric/struct.rb
CHANGED
@@ -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|
|
data/lib/parametric/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2018-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|