datacaster 3.2.5 → 3.2.7

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: 6e87035b2c58689f1682294141d8ef6dfe02c6054a810e7bcda3a22f2d934091
4
- data.tar.gz: d6262e0130ef3f831731e9b0d6095a40fa7a19bfa9eecd5a0630c5eb8a3d9513
3
+ metadata.gz: cecd5729ff179f70e6a7530603255f009814b7d4ef1d9dbe576a5f2b9e0bc222
4
+ data.tar.gz: 4e234450bc92faf277789eb21ccbd03ddb544253250978da41d85dd9d12f8e39
5
5
  SHA512:
6
- metadata.gz: 6800f34a95b7e8baaad2d2f093f4c6be58e35904220339c2c8c5452fcea375b169224ebf49c699d965073cf9b3777ab00b91adf48184d539c2ff1616accde73f
7
- data.tar.gz: b8f9fd1227da11c029af5af57fb336f7e10f29b763123517ed4e5fbae0b27ca9ff6a65367841684edc366066ad27c720d367a318578b09637bfa98680bfcb58c
6
+ metadata.gz: 68e2588ec262fad2419fe3adf0a5d87aacfb28375ae47a277c4c8cb03e172913aec763375f1ffce90d2422269d219c1ff0f504921d5afc2be78c1efc8a3a2062
7
+ data.tar.gz: 335ef1ea64bbc63c78b0c6ef44d330967173187d6ac15bdc7b788bee9b38be77ebc63e892d97914f99fa7da1f25a604f10c96038c2f6ad4bb15b6319f5b2db0b
data/README.md CHANGED
@@ -1327,7 +1327,7 @@ CommonValidator =
1327
1327
  end
1328
1328
 
1329
1329
  PersonValidator =
1330
- Datacaster.schema do
1330
+ Datacaster.partial_schema do
1331
1331
  hash_schema(
1332
1332
  name: string
1333
1333
  )
@@ -1487,7 +1487,33 @@ current_user = ...
1487
1487
  schema.with_context(current_user: current_user).(post_id: 15)
1488
1488
  ```
1489
1489
 
1490
- `context` is an [OpenStruct](https://ruby-doc.org/stdlib-3.1.0/libdoc/ostruct/rdoc/OpenStruct.html) instance.
1490
+ `context` behaves similarly to OpenStruct, setter method can be used to set a context value (see also [run](#run--value--) caster):
1491
+
1492
+ ```ruby
1493
+ schema =
1494
+ Datacaster.schema do
1495
+ run { context.five = 5 } & check { context.five == 5 }
1496
+ end
1497
+
1498
+ # Notice that #with_context call is still required, otherwise
1499
+ # #context method will not be available in the caster's runtime
1500
+ schema.with_context.(nil)
1501
+ # => Datacaster::ValidResult(nil)
1502
+ ```
1503
+
1504
+ If there are conflicts between context values, the most specific one (closest to the caster) wins:
1505
+
1506
+ ```ruby
1507
+ schema =
1508
+ Datacaster.schema do
1509
+ check { context.five == 5 }.
1510
+ with_context(five: 5). # this will win
1511
+ with_context(five: 10)
1512
+ end
1513
+
1514
+ schema.with_context(five: 15).(nil)
1515
+ # => Datacaster::ValidResult(nil)
1516
+ ```
1491
1517
 
1492
1518
  **Note**
1493
1519
 
@@ -24,10 +24,15 @@ module Datacaster
24
24
  def transform_result(result)
25
25
  return result unless result.valid?
26
26
  result = cast_success(result)
27
- parent_runtime = @runtime.instance_variable_get(:@parent)
28
- if parent_runtime.respond_to?(:checked_schema!)
29
- parent_runtime.checked_schema!(@runtime.checked_schema)
27
+
28
+ # Notify parent runtime of current runtime's schema
29
+ unless @runtime.unchecked?
30
+ parent_runtime = @runtime.instance_variable_get(:@parent)
31
+ if parent_runtime.respond_to?(:checked_schema!)
32
+ parent_runtime.checked_schema!(@runtime.checked_schema)
33
+ end
30
34
  end
35
+
31
36
  result
32
37
  end
33
38
 
@@ -20,9 +20,9 @@ module Datacaster
20
20
  ThenNode.new(self, DefinitionDSL.expand(other))
21
21
  end
22
22
 
23
- def with_context(context)
24
- unless context.is_a?(Hash)
25
- raise "with_context expected Hash as argument, got #{context.inspect} instead"
23
+ def with_context(context = {})
24
+ unless context.respond_to?(:[])
25
+ raise "with_context expected enumerable as argument, got #{context.inspect} instead"
26
26
  end
27
27
  ContextNodes::UserContext.new(self, context)
28
28
  end
@@ -47,6 +47,7 @@ module Datacaster
47
47
  @pointer_stack[-1] = true
48
48
  # Child runtime's schema should be merged with current runtime's schema
49
49
  else
50
+ will_check!
50
51
  @pointer_stack[-1].merge!(schema)
51
52
  end
52
53
  end
@@ -10,16 +10,23 @@ module Datacaster
10
10
  end
11
11
 
12
12
  def method_missing(m, *args)
13
- if !args.empty? || block_given?
13
+ if args.length > 1 || block_given?
14
14
  return super
15
15
  end
16
16
 
17
- if @context.key?(m)
17
+ key_present = @context.respond_to?(:key?) && @context.key?(m) ||
18
+ @context.to_h.key?(m.to_sym)
19
+
20
+ if key_present && args.empty?
18
21
  return @context[m]
19
22
  end
20
23
 
24
+ if m =~ /\A.+=\z/ && args.length == 1
25
+ return @context[m[0..-2].to_sym] = args[0]
26
+ end
27
+
21
28
  begin
22
- @node.class.send_to_parent(@node, :context).public_send(m)
29
+ @node.class.send_to_parent(@node, :context).public_send(m, *args)
23
30
  rescue NoMethodError
24
31
  raise NoMethodError.new("Key #{m.inspect} is not found in the context")
25
32
  end
@@ -1,3 +1,3 @@
1
1
  module Datacaster
2
- VERSION = "3.2.5"
2
+ VERSION = "3.2.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datacaster
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.5
4
+ version: 3.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Zolotarev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-07 00:00:00.000000000 Z
11
+ date: 2024-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel