Sha256: 882c0c710ff2e1270bbdc03a334215631744ab42fb56ac7563f32edbde376163

Contents?: true

Size: 1.16 KB

Versions: 6

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

class Serega
  module Plugins
    module Preloads
      # Transforms user provided preloads to hash
      class FormatUserPreloads
        METHODS = {
          Array => :array_to_hash,
          FalseClass => :nil_to_hash,
          Hash => :hash_to_hash,
          NilClass => :nil_to_hash,
          String => :string_to_hash,
          Symbol => :symbol_to_hash
        }.freeze

        module ClassMethods
          def call(value)
            send(METHODS.fetch(value.class), value)
          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)
            symbol_to_hash(value.to_sym)
          end

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

        extend ClassMethods
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

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