Sha256: f1767449126dbb683059334dacf4a8b401a6f19f648bd019812dc24b937d6a23

Contents?: true

Size: 936 Bytes

Versions: 11

Compression:

Stored size: 936 Bytes

Contents

module Ohm
  class Command
    def self.[](operation, head, *tail)
      return head if tail.empty?

      new(operation, head, *tail)
    end

    attr :operation
    attr :args
    attr :keys

    def initialize(operation, *args)
      @operation = operation
      @args = args
      @keys = []
    end

    def call(nido, redis)
      newkey(nido, redis) do |key|
        redis.call(@operation, key, *params(nido, redis))
      end
    end

    def clean
      keys.each do |key, redis|
        redis.call("DEL", key)
      end

      subcommands.each { |cmd| cmd.clean }
    end

  private
    def subcommands
      args.select { |arg| arg.respond_to?(:call) }
    end

    def params(nido, redis)
      args.map { |arg| arg.respond_to?(:call) ? arg.call(nido, redis) : arg }
    end

    def newkey(nido, redis)
      key = nido[SecureRandom.hex(32)]
      keys << [key, redis]

      yield key

      return key
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
sohm-0.0.1 lib/sohm/command.rb
ohm-2.1.0 lib/ohm/command.rb
ohm-2.0.1 lib/ohm/command.rb
ohm-2.0.0 lib/ohm/command.rb
ohm-2.0.0.rc2 lib/ohm/command.rb
ohm-2.0.0.rc1 lib/ohm/command.rb
ohm-2.0.0.alpha5 lib/ohm/command.rb
ohm-2.0.0.alpha4 lib/ohm/command.rb
ohm-2.0.0.alpha3 lib/ohm/command.rb
ohm-2.0.0.alpha2 lib/ohm/command.rb
ohm-2.0.0.alpha1 lib/ohm/command.rb