Sha256: 8425d61d9c8b404822f31baea8ed946e024b20ab90463ebc2369e1e1e27c6455

Contents?: true

Size: 1.58 KB

Versions: 13

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaPlugins
    module Preloads
      #
      # Utility that helps to transform user provided preloads to hash
      #
      class FormatUserPreloads
        class << self
          #
          # Transforms user provided preloads to hash
          #
          # @param value [Array,Hash,String,Symbol,nil,false] preloads
          #
          # @return [Hash] preloads transformed to hash
          #
          def call(value)
            case value
            when Array then array_to_hash(value)
            when FalseClass then nil_to_hash(value)
            when Hash then hash_to_hash(value)
            when NilClass then nil_to_hash(value)
            when String then string_to_hash(value)
            when Symbol then symbol_to_hash(value)
            else raise Serega::SeregaError,
              "Preload option value can consist from Symbols, Arrays, Hashes (#{value.class} #{value.inspect} was provided)"
            end
          end

          private

          def array_to_hash(values)
            values.each_with_object({}) do |value, obj|
              obj.merge!(call(value))
            end
          end

          def hash_to_hash(values)
            values.each_with_object({}) do |(key, value), obj|
              obj[key.to_sym] = call(value)
            end
          end

          def nil_to_hash(_value)
            {}
          end

          def string_to_hash(value)
            {value.to_sym => {}}
          end

          def symbol_to_hash(value)
            {value => {}}
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
serega-0.21.0 lib/serega/plugins/preloads/lib/format_user_preloads.rb
serega-0.20.1 lib/serega/plugins/preloads/lib/format_user_preloads.rb
serega-0.20.0 lib/serega/plugins/preloads/lib/format_user_preloads.rb
serega-0.19.0 lib/serega/plugins/preloads/lib/format_user_preloads.rb
serega-0.18.0 lib/serega/plugins/preloads/lib/format_user_preloads.rb
serega-0.17.0 lib/serega/plugins/preloads/lib/format_user_preloads.rb
serega-0.16.0 lib/serega/plugins/preloads/lib/format_user_preloads.rb
serega-0.15.0 lib/serega/plugins/preloads/lib/format_user_preloads.rb
serega-0.14.0 lib/serega/plugins/preloads/lib/format_user_preloads.rb
serega-0.12.0 lib/serega/plugins/preloads/lib/format_user_preloads.rb
serega-0.11.2 lib/serega/plugins/preloads/lib/format_user_preloads.rb
serega-0.11.1 lib/serega/plugins/preloads/lib/format_user_preloads.rb
serega-0.11.0 lib/serega/plugins/preloads/lib/format_user_preloads.rb