Sha256: b9764ad1cd7854b7363ef308d5dbaacc5f982992b86886ca77830ce3026ed04f

Contents?: true

Size: 1.74 KB

Versions: 28

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module PgEventstore
  class Utils
    class << self
      # Deep transforms keys of a given Hash
      # @param object [Object]
      # @return [Object] a hash with transformed keys
      def deep_transform_keys(object, &block)
        case object
        when Hash
          object.each_with_object({}) do |(key, value), result|
            result[yield(key)] = deep_transform_keys(value, &block)
          end
        when Array
          object.map { |e| deep_transform_keys(e, &block) }
        else
          object
        end
      end

      # Deep dup Array or Hash
      # @param object [Object]
      # @return [Object]
      def deep_dup(object)
        case object
        when Hash
          object.each_with_object({}) do |(key, value), result|
            result[deep_dup(key)] = deep_dup(value)
          end
        when Array
          object.map { |e| deep_dup(e) }
        else
          object.dup
        end
      end

      # Converts array to the string containing SQL positional variables
      # @param array [Array]
      # @return [String] positional variables, based on array size. Example: "$1, $2, $3"
      def positional_vars(array)
        array.size.times.map { |t| "$#{t + 1}" }.join(', ')
      end

      # Transforms exception instance into a hash
      # @param error [StandardError]
      # @return [Hash]
      def error_info(error)
        {
          class: error.class,
          message: error.message,
          backtrace: error.backtrace
        }
      end

      # @param str [String]
      # @return [String]
      def underscore_str(str)
        str = str.dup
        str[0] = str[0].downcase
        str.gsub!(/[A-Z]/) { |letter| '_' + letter.downcase }
        str
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
pg_eventstore-1.4.0 lib/pg_eventstore/utils.rb
pg_eventstore-1.3.4 lib/pg_eventstore/utils.rb
pg_eventstore-1.3.3 lib/pg_eventstore/utils.rb
pg_eventstore-1.3.2 lib/pg_eventstore/utils.rb
pg_eventstore-1.3.1 lib/pg_eventstore/utils.rb
pg_eventstore-1.3.0 lib/pg_eventstore/utils.rb
pg_eventstore-1.2.0 lib/pg_eventstore/utils.rb
pg_eventstore-1.1.5 lib/pg_eventstore/utils.rb
pg_eventstore-1.1.4 lib/pg_eventstore/utils.rb
pg_eventstore-1.1.3 lib/pg_eventstore/utils.rb
pg_eventstore-1.1.2 lib/pg_eventstore/utils.rb
pg_eventstore-1.1.1 lib/pg_eventstore/utils.rb
pg_eventstore-1.1.0 lib/pg_eventstore/utils.rb
pg_eventstore-1.0.4 lib/pg_eventstore/utils.rb
pg_eventstore-1.0.0.rc2 lib/pg_eventstore/utils.rb
pg_eventstore-1.0.0.rc1 lib/pg_eventstore/utils.rb
pg_eventstore-0.10.2 lib/pg_eventstore/utils.rb
pg_eventstore-0.10.1 lib/pg_eventstore/utils.rb
pg_eventstore-0.9.0 lib/pg_eventstore/utils.rb
pg_eventstore-0.8.0 lib/pg_eventstore/utils.rb