Sha256: a53582a2741fb47992b7683e416c2749ebbe6f171124d6852b182056312be45b

Contents?: true

Size: 1.21 KB

Versions: 6

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

class Serega
  module Plugins
    module Preloads
      class CheckOptPreloadPath
        class << self
          def call(opts)
            return unless opts.key?(:preload_path)

            value = opts[:preload_path]
            raise Error, "Invalid option :preload_path => #{value.inspect}. Can be provided only when :preload option provided" unless opts[:preload]
            raise Error, "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 Error, "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

6 entries across 6 versions & 1 rubygems

Version Path
serega-0.1.5 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
serega-0.1.4 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
serega-0.1.3 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
serega-0.1.2 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
serega-0.1.1 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb
serega-0.1.0 lib/serega/plugins/preloads/validations/check_opt_preload_path.rb