parametric 0.2.8 → 0.2.9

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: ef9d011c06f718caa4de95a60589cd858f693a98085a1962c8882e9213effd26
4
- data.tar.gz: f759c6f6d051935a36d6a080b15602b628e4967b9b052d9ec3c5f912bf5a9b2e
3
+ metadata.gz: 3e5e99f35e7a900299c8ca79bf1edecc57a73209c8fab7c292ef5fff11f1e467
4
+ data.tar.gz: a28ad622475c98957079ee5a000c02f91b1df05c6f3fd9492bcdfd10a6751edc
5
5
  SHA512:
6
- metadata.gz: 8f4867518fb179f164983751d2d6143a324eff9a961b91618e02677c74fe8e596f9f8d48a25582e2955c339f8128565b9cfaee95ca73f3a128a6b5861c5638aa
7
- data.tar.gz: 87c47b5d47339b543027cff38b2f3a8a343ff27342ddfd1611fe0953c5b3169dee3064ac014a865382eebea0a87caab554312539f279648be43c66651c3a18b0
6
+ metadata.gz: 86bcd5fcd359872f8eab2b23206bd359b13ab1363d5b7d29eb8ef9899ed421605d3be10cbf4841d2791eeecf46de8292e63d97180c799ae2febb0c955b054c92
7
+ data.tar.gz: b767db9489841a70489812bd51be1bb167de8a87b9c91a4f21cb82201d7023ed5e0a05747d853332f936bb0d05f2e3be44d6ed64c6bd24820fb19da1902c708f
data/README.md CHANGED
@@ -98,7 +98,7 @@ results.errors # => {"$.friends[0].name" => "is required"}
98
98
  You can optionally use an existing schema instance as a nested schema:
99
99
 
100
100
  ```ruby
101
- friends_schema = Parametric::Schema.new do
101
+ FRIENDS_SCHEMA = Parametric::Schema.new do
102
102
  field(:friends).type(:array).schema do
103
103
  field(:name).type(:string).required
104
104
  field(:email).policy(:email)
@@ -109,10 +109,24 @@ person_schema = Parametric::Schema.new do
109
109
  field(:name).type(:string).required
110
110
  field(:age).type(:integer)
111
111
  # Nest friends_schema
112
- field(:friends).type(:array).schema(friends_schema)
112
+ field(:friends).type(:array).schema(FRIENDS_SCHEMA)
113
113
  end
114
114
  ```
115
115
 
116
+ Note that _person_schema_'s definition has access to `FRIENDS_SCHEMA` because it's a constant.
117
+ Definition blocks are run in the context of the defining schema instance by default.
118
+
119
+ To preserve the original block's context, declare two arguments in your block, the defining schema `sc` and options has.
120
+
121
+ ```ruby
122
+ person_schema = Parametric::Schema.new do |sc, options|
123
+ # this block now preserves its context. Call `sc.field` to add fields to the current schema.
124
+ sc.field(:name).type(:string).required
125
+ sc.field(:age).type(:integer)
126
+ # We now have access to local variables
127
+ sc.field(:friends).type(:array).schema(friends_schema)
128
+ end
129
+ ```
116
130
  ## Built-in policies
117
131
 
118
132
  Type coercions (the `type` method) and validations (the `validate` method) are all _policies_.
@@ -182,7 +182,11 @@ module Parametric
182
182
  def apply!
183
183
  return if @applied
184
184
  definitions.each do |d|
185
- self.instance_exec(options, &d)
185
+ if d.arity == 2 # pass schema instance and options, preserve block context
186
+ d.call(self, options)
187
+ else # run block in context of current instance
188
+ self.instance_exec(options, &d)
189
+ end
186
190
  end
187
191
  @applied = true
188
192
  end
@@ -1,3 +1,3 @@
1
1
  module Parametric
2
- VERSION = "0.2.8"
2
+ VERSION = "0.2.9"
3
3
  end
@@ -237,6 +237,20 @@ describe Parametric::Schema do
237
237
  end
238
238
  end
239
239
 
240
+ context 'yielding schema to definition, to preserve outer context' do
241
+ it 'yields schema instance and options to definition block, can access outer context' do
242
+ schema1 = described_class.new do
243
+ field(:name).type(:string)
244
+ end
245
+ schema2 = described_class.new do |sc, _opts|
246
+ sc.field(:user).schema schema1
247
+ end
248
+
249
+ out = schema2.resolve(user: { name: 'Joe' }).output
250
+ expect(out[:user][:name]).to eq 'Joe'
251
+ end
252
+ end
253
+
240
254
  describe "#ignore" do
241
255
  it "ignores fields" do
242
256
  s1 = described_class.new.ignore(:title, :status) do
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.8
4
+ version: 0.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismael Celis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-05 00:00:00.000000000 Z
11
+ date: 2019-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler