serega 0.36.0 → 0.37.1

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.
@@ -1,89 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Serega
4
- module SeregaValidations
5
- module Attribute
6
- #
7
- # Validator for attribute :preload_path option
8
- #
9
- class CheckOptPreloadPath
10
- class << self
11
- #
12
- # Checks preload_path option
13
- #
14
- # @param opts [Hash] Attribute options
15
- #
16
- # @raise [SeregaError] validation error
17
- #
18
- # @return [void]
19
- #
20
- def call(opts)
21
- return if exactly_nil?(opts, :preload_path) # allow to provide nil anyway
22
-
23
- path = opts[:preload_path]
24
- check_usage_with_other_options(path, opts)
25
- return unless opts[:serializer]
26
-
27
- check_allowed(path, opts)
28
- end
29
-
30
- private
31
-
32
- def exactly_nil?(opts, opt_name)
33
- opts.fetch(opt_name, false).nil?
34
- end
35
-
36
- def check_allowed(path, opts)
37
- allowed_paths = SeregaUtils::PreloadPaths.call(opts[:preload])
38
- check_required_when_many_allowed(path, allowed_paths)
39
- check_in_allowed(path, allowed_paths)
40
- end
41
-
42
- def check_usage_with_other_options(path, opts)
43
- return unless path
44
-
45
- preload = opts[:preload]
46
- raise SeregaError, "Invalid option preload_path: #{path.inspect}. Can be provided only when :preload option provided" unless preload
47
-
48
- serializer = opts[:serializer]
49
- raise SeregaError, "Invalid option preload_path: #{path.inspect}. Can be provided only when :serializer option provided" unless serializer
50
- end
51
-
52
- def check_required_when_many_allowed(path, allowed)
53
- return if path || (allowed.size < 2)
54
-
55
- raise SeregaError, "Option :preload_path must be provided. Possible values: #{allowed.inspect[1..-2]}"
56
- end
57
-
58
- def check_in_allowed(path, allowed)
59
- return if !path && allowed.size <= 1
60
-
61
- if multiple_preload_paths_provided?(path)
62
- check_many(path, allowed)
63
- else
64
- check_one(path, allowed)
65
- end
66
- end
67
-
68
- def check_one(path, allowed)
69
- formatted_path = Array(path).map(&:to_sym)
70
- return if allowed.include?(formatted_path)
71
-
72
- raise SeregaError,
73
- "Invalid preload_path (#{path.inspect}). " \
74
- "Can be one of #{allowed.inspect[1..-2]}"
75
- end
76
-
77
- def check_many(paths, allowed)
78
- paths.each { |path| check_one(path, allowed) }
79
- end
80
-
81
- # Check value is Array in Array
82
- def multiple_preload_paths_provided?(value)
83
- value.is_a?(Array) && value[0].is_a?(Array)
84
- end
85
- end
86
- end
87
- end
88
- end
89
- end