datacaster 3.2.4 → 3.2.6

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: 382b6c6e5194c6746baaef5483ef078a06f0494c1c41798e118bf9b63d554436
4
- data.tar.gz: 1cc94cf3dbe08b9e660bb0e0a164eaf15567408239fa7b8f84f5353256b6c8b7
3
+ metadata.gz: c44e05c449320297096373bd48d942aae81776c5ad358f831215d875628d0ce2
4
+ data.tar.gz: 831752e1d0028d3d9e3d9423e515c34b587d13aecdcd24be4b12a1d74d073b61
5
5
  SHA512:
6
- metadata.gz: a4fc369a63f0a2787d7b7a2706f68cf08a0181771c5c84acb3fa6d60e0f1e2fe2ca097ba611825bfd6e75f344dbf301254d90367cf6ece61264ea29c8b336ea2
7
- data.tar.gz: caed96fc02c8020c951f053fa2a71852623ff8a45ac9ce91235c34d999b2cebd4eb3533bfdbc090aa561f4346bd596b940bcbd169ea81e47f4756430b87e6ede
6
+ metadata.gz: eb18dda544b94751ed2d389e3191d9372f0e9323a162c489fc10731da26cc643d2151ff55a600152dea496d1831251b24bfc00ebf294b13b22a2954f64173cae
7
+ data.tar.gz: ae9c906810ff03d978b5b85c07a6042750ad92ed46dbe7ea55318a3a1a61ab85ba2854482de5d9c34aa234aed1c687730d1ff4ee80b8232a8a42a28daeecdfa0
data/README.md CHANGED
@@ -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
 
@@ -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
@@ -35,8 +35,20 @@ module Datacaster
35
35
  @should_check_stack[-1] = true
36
36
  end
37
37
 
38
+ # Notify current runtime that some child runtime has built schema,
39
+ # child runtime's schema is passed as the argument
38
40
  def checked_schema!(schema)
39
- @pointer_stack[-1].merge!(schema)
41
+ # Current runtime has marked its schema as checked unconditionally
42
+ return if @pointer_stack[-1] == true
43
+
44
+ # Child runtime marks its schema as checked unconditionally, so
45
+ # current runtime should do as well
46
+ if schema == true
47
+ @pointer_stack[-1] = true
48
+ # Child runtime's schema should be merged with current runtime's schema
49
+ else
50
+ @pointer_stack[-1].merge!(schema)
51
+ end
40
52
  end
41
53
 
42
54
  def ignore_checks!(&block)
@@ -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.4"
2
+ VERSION = "3.2.6"
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.4
4
+ version: 3.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Zolotarev
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-07 00:00:00.000000000 Z
11
+ date: 2024-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -106,7 +106,7 @@ dependencies:
106
106
  - - "<"
107
107
  - !ruby/object:Gem::Version
108
108
  version: '3'
109
- description:
109
+ description:
110
110
  email:
111
111
  - eugzol@gmail.com
112
112
  executables: []
@@ -178,7 +178,7 @@ licenses:
178
178
  - MIT
179
179
  metadata:
180
180
  source_code_uri: https://github.com/EugZol/datacaster
181
- post_install_message:
181
+ post_install_message:
182
182
  rdoc_options: []
183
183
  require_paths:
184
184
  - lib
@@ -193,8 +193,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
193
  - !ruby/object:Gem::Version
194
194
  version: '0'
195
195
  requirements: []
196
- rubygems_version: 3.1.6
197
- signing_key:
196
+ rubygems_version: 3.4.6
197
+ signing_key:
198
198
  specification_version: 4
199
199
  summary: Run-time type checker and transformer for Ruby
200
200
  test_files: []