Sha256: e183e1943f82c8955d20dcf47cd9199ea68879ec3dbcacfb05a2c8091fc35842

Contents?: true

Size: 1.52 KB

Versions: 10

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaPlugins
    module Preloads
      #
      # Validator for attribute :preload_path option
      #
      class CheckOptPreloadPath
        class << self
          #
          # Checks preload_path option
          #
          # @param opts [Hash] Attribute options
          #
          # @raise [SeregaError] validation error
          #
          # @return [void]
          #
          def call(opts)
            return unless opts.key?(:preload_path)

            value = opts[:preload_path]
            raise SeregaError, "Invalid option :preload_path => #{value.inspect}. Can be provided only when :preload option provided" unless opts[:preload]
            raise SeregaError, "Invalid option :preload_path => #{value.inspect}. Can be provided only when :serializer option provided" unless opts[:serializer]

            path = Array(value).map!(&:to_sym)
            preloads = FormatUserPreloads.call(opts[:preload])
            allowed_paths = paths(preloads)
            raise SeregaError, "Invalid option :preload_path => #{value.inspect}. Can be one of #{allowed_paths.inspect[1..-2]}" unless allowed_paths.include?(path)
          end

          private

          def paths(preloads, path = [], result = [])
            preloads.each do |key, nested_preloads|
              path << key
              result << path.dup

              paths(nested_preloads, path, result)
              path.pop
            end

            result
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
serega-0.11.2 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
serega-0.11.1 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
serega-0.11.0 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
serega-0.10.0 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
serega-0.9.0 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
serega-0.8.3 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
serega-0.8.2 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
serega-0.8.1 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
serega-0.8.0 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
serega-0.7.0 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb