Sha256: bfce8fa10b2282d33e4cef6c233d1a3157b7178c99126d1548dd4715ce6708d9
Contents?: true
Size: 1.24 KB
Versions: 6
Compression:
Stored size: 1.24 KB
Contents
# frozen_string_literal: true class Serega module Utils class ToHash module ClassMethods def call(value) case value when Array then array_to_hash(value) when Hash then hash_to_hash(value) when NilClass, FalseClass then nil_to_hash(value) when String then string_to_hash(value) when Symbol then symbol_to_hash(value) else raise Error, "Cant convert #{value.class} class object to hash" end end private def array_to_hash(values) return Serega::FROZEN_EMPTY_HASH if values.empty? values.each_with_object({}) do |value, obj| obj.merge!(call(value)) end end def hash_to_hash(values) return Serega::FROZEN_EMPTY_HASH if values.empty? values.each_with_object({}) do |(key, value), obj| obj[key.to_sym] = call(value) end end def nil_to_hash(_value) Serega::FROZEN_EMPTY_HASH end def string_to_hash(value) symbol_to_hash(value.to_sym) end def symbol_to_hash(value) {value => Serega::FROZEN_EMPTY_HASH} end end extend ClassMethods end end end
Version data entries
6 entries across 6 versions & 1 rubygems