verquest 0.6.4 → 0.7.0

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.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +43 -1
  3. data/README.md +299 -45
  4. data/Rakefile +5 -0
  5. data/docs/rails.md +169 -0
  6. data/lib/verquest/base/condition_class_methods.rb +211 -0
  7. data/lib/verquest/base/private_class_methods.rb +31 -14
  8. data/lib/verquest/base/public_class_methods.rb +51 -1
  9. data/lib/verquest/base.rb +1 -0
  10. data/lib/verquest/conditions/branch.rb +111 -0
  11. data/lib/verquest/conditions/condition.rb +194 -0
  12. data/lib/verquest/conditions/conjunction.rb +39 -0
  13. data/lib/verquest/conditions/fragment_merge.rb +52 -0
  14. data/lib/verquest/conditions/property_merge.rb +47 -0
  15. data/lib/verquest/conditions/rule_set.rb +58 -0
  16. data/lib/verquest/conditions/rules/absent.rb +25 -0
  17. data/lib/verquest/conditions/rules/alternatives.rb +28 -0
  18. data/lib/verquest/conditions/rules/any.rb +27 -0
  19. data/lib/verquest/conditions/rules/equals.rb +22 -0
  20. data/lib/verquest/conditions/rules/names_rule.rb +23 -0
  21. data/lib/verquest/conditions/rules/none.rb +24 -0
  22. data/lib/verquest/conditions/rules/present.rb +22 -0
  23. data/lib/verquest/conditions/rules/property_rule.rb +48 -0
  24. data/lib/verquest/conditions/rules/satisfies.rb +22 -0
  25. data/lib/verquest/conditions/rules/value_in.rb +22 -0
  26. data/lib/verquest/conditions/rules.rb +17 -0
  27. data/lib/verquest/conditions.rb +36 -0
  28. data/lib/verquest/configuration.rb +19 -1
  29. data/lib/verquest/gem_version.rb +1 -1
  30. data/lib/verquest/helper_methods/conditional_properties.rb +126 -0
  31. data/lib/verquest/helper_methods/unnamed_one_of.rb +35 -0
  32. data/lib/verquest/properties/array.rb +15 -1
  33. data/lib/verquest/properties/base.rb +104 -10
  34. data/lib/verquest/properties/collection.rb +51 -18
  35. data/lib/verquest/properties/const.rb +2 -1
  36. data/lib/verquest/properties/object.rb +38 -4
  37. data/lib/verquest/properties/one_of/discriminated_schema.rb +84 -0
  38. data/lib/verquest/properties/one_of.rb +83 -44
  39. data/lib/verquest/properties/reference.rb +47 -16
  40. data/lib/verquest/query/coercer.rb +165 -0
  41. data/lib/verquest/query/definition_check.rb +91 -0
  42. data/lib/verquest/query/parameters.rb +79 -0
  43. data/lib/verquest/query.rb +25 -0
  44. data/lib/verquest/transformer/collection.rb +32 -0
  45. data/lib/verquest/transformer/const_as_enum.rb +47 -0
  46. data/lib/verquest/transformer/layout.rb +83 -0
  47. data/lib/verquest/transformer/mapping_layout.rb +166 -0
  48. data/lib/verquest/transformer/one_of.rb +111 -0
  49. data/lib/verquest/transformer/path.rb +188 -0
  50. data/lib/verquest/transformer/plan.rb +183 -0
  51. data/lib/verquest/transformer/scope.rb +64 -0
  52. data/lib/verquest/transformer.rb +43 -880
  53. data/lib/verquest/version.rb +152 -79
  54. data/lib/verquest.rb +6 -0
  55. metadata +42 -7
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Verquest
4
+ class Transformer
5
+ # The mapping of the params, or of one collection item
6
+ #
7
+ # When a source path's parent is explicitly null, the corresponding target
8
+ # path is set to null in the result instead of being omitted.
9
+ #
10
+ # Collections whose items hold a oneOf get their own scope for each item,
11
+ # since every item can select a different variant. Those scopes can hold
12
+ # collections again.
13
+ #
14
+ # @!attribute [r] pairs
15
+ # @return [Array<Array(Path, Path)>] The source and target pairs mapped for any input
16
+ # @!attribute [r] one_ofs
17
+ # @return [Array<OneOf>] The oneOfs whose selected variant scope is applied into the result
18
+ # @!attribute [r] collections
19
+ # @return [Array<Collection>] The collections transformed item by item
20
+ #
21
+ # Internal to the transformer, not part of the public API.
22
+ Scope = Data.define(:pairs, :one_ofs, :collections) do
23
+ # Transforms the data
24
+ #
25
+ # @param data [Object] The params or a collection item
26
+ # @return [Hash] The transformed data
27
+ # @raise [Verquest::MappingError] If a oneOf matches no variant or more than one
28
+ def call(data)
29
+ apply(data, {})
30
+ end
31
+
32
+ # Transforms the data into an existing result
33
+ #
34
+ # A oneOf variant is a scope applied into the result of the scope that
35
+ # holds the oneOf.
36
+ #
37
+ # @param data [Object] The params or a collection item
38
+ # @param result [Hash] The hash to write to
39
+ # @return [Hash] The result
40
+ # @raise [Verquest::MappingError] If a oneOf matches no variant or more than one
41
+ def apply(data, result)
42
+ map(pairs, data, result)
43
+ one_ofs.each { |one_of| one_of.variant_for(data).apply(data, result) }
44
+ collections.each { |collection| collection.call(data, result) }
45
+ result
46
+ end
47
+
48
+ private
49
+
50
+ # Copies the value of each source path to its target path
51
+ #
52
+ # @param pairs [Array<Array(Path, Path)>] The source and target pairs
53
+ # @param data [Object] The data to read from
54
+ # @param result [Hash] The hash to write to
55
+ # @return [void]
56
+ def map(pairs, data, result)
57
+ pairs.each do |source, target|
58
+ value = source.extract(data, nulls: true)
59
+ target.set(result, value) unless value.equal?(Path::NOT_FOUND)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end