Sha256: 3397552774a123d34144fc3bc3f28454c3a8e4a44410b01cdb912a36629d305d

Contents?: true

Size: 1.94 KB

Versions: 2

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

class RedisClient
  module CommandBuilder
    extend self

    if Symbol.method_defined?(:name)
      def generate!(args, kwargs)
        command = args.flat_map do |element|
          case element
          when Hash
            element.flatten
          when Set
            element.to_a
          else
            element
          end
        end

        kwargs.each do |key, value|
          if value
            if value == true
              command << key.name
            else
              command << key.name << value
            end
          end
        end

        command.map! do |element|
          case element
          when String
            element
          when Symbol
            element.name
          when Integer, Float
            element.to_s
          else
            raise TypeError, "Unsupported command argument type: #{element.class}"
          end
        end

        if command.empty?
          raise ArgumentError, "can't issue an empty redis command"
        end

        command
      end
    else
      def generate!(args, kwargs)
        command = args.flat_map do |element|
          case element
          when Hash
            element.flatten
          when Set
            element.to_a
          else
            element
          end
        end

        kwargs.each do |key, value|
          if value
            if value == true
              command << key.to_s
            else
              command << key.to_s << value
            end
          end
        end

        command.map! do |element|
          case element
          when String
            element
          when Integer, Float, Symbol
            element.to_s
          else
            raise TypeError, "Unsupported command argument type: #{element.class}"
          end
        end

        if command.empty?
          raise ArgumentError, "can't issue an empty redis command"
        end

        command
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redis-client-0.5.1 lib/redis_client/command_builder.rb
redis-client-0.5.0 lib/redis_client/command_builder.rb