Sha256: cf3e0869fe9607b75455b343e7162962b35a7d66302077d6b0d7a359851ed638

Contents?: true

Size: 858 Bytes

Versions: 9

Compression:

Stored size: 858 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(nest, db)
      newkey(nest) do |key|
        db.send(@operation, key, *params(nest, db))
      end
    end

    def clean
      keys.each { |key| key.del }
      subcommands.each { |cmd| cmd.clean }
    end

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

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

    def newkey(nest)
      key = nest[SecureRandom.hex(32)]
      keys << key

      yield  key
      return key
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ohm-1.4.0 lib/ohm/command.rb
ohm-1.3.2 lib/ohm/command.rb
ohm-1.3.1 lib/ohm/command.rb
ohm-1.3.0 lib/ohm/command.rb
ohm-1.2.0 lib/ohm/command.rb
ohm-1.1.2 lib/ohm/command.rb
ohm-1.1.1 lib/ohm/command.rb
ohm-1.1.0 lib/ohm/command.rb
ohm-1.1.0.rc1 lib/ohm/command.rb