Sha256: 4d9a6a846baad419167eae801564af15692bf9022c53a868105f6cfdf9a6e90f

Contents?: true

Size: 839 Bytes

Versions: 5

Compression:

Stored size: 839 Bytes

Contents

require 'rom/commands/result'

module ROM
  # Command registry exposes "try" interface for executing commands
  #
  # @api public
  class CommandRegistry < Registry
    include Commands

    # Try to execute a command in a block
    #
    # @yield [command] Passes command to the block
    #
    # @example
    #
    #   rom.command(:users).try { create(name: 'Jane') }
    #   rom.command(:users).try { update(:by_id, 1).set(name: 'Jane Doe') }
    #   rom.command(:users).try { delete(:by_id, 1) }
    #
    # @return [Commands::Result]
    #
    # @api public
    def try(&block)
      response = block.call

      if response.is_a?(Command) || response.is_a?(Composite)
        try { response.call }
      else
        Result::Success.new(response)
      end
    rescue CommandError => e
      Result::Failure.new(e)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rom-0.6.0 lib/rom/command_registry.rb
rom-0.6.0.rc1 lib/rom/command_registry.rb
rom-0.6.0.beta3 lib/rom/command_registry.rb
rom-0.6.0.beta2 lib/rom/command_registry.rb
rom-0.6.0.beta1 lib/rom/command_registry.rb