Sha256: 20c20af0d34e2a69cab1b557791e9d58204aa81647d716986ad2b1e483a700fd

Contents?: true

Size: 1.1 KB

Versions: 7

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

# @api private
# @since 0.5.0
class SmartCore::Operation::InstanceBuilder
  class << self
    # @param operation [SmartCore::Operation]
    # @return [void]
    #
    # @api private
    # @since 0.5.0
    def call(operation)
      new(operation).call
    end
  end

  # @param operation [SmartCore::Operation]
  # @return [void]
  #
  # @api private
  # @since 0.5.0
  def initialize(operation)
    @operation = operation
  end

  # @return [void]
  #
  # @api private
  # @since 0.5.0
  def call
    make_operation_caller_yieldable
  end

  private

  # @return [SmartCore::Operation]
  #
  # @api private
  # @since 0.5.0
  attr_reader :operation

  # @return [void]
  #
  # @api private
  # @since 0.2.0
  def make_operation_caller_yieldable
    operation.singleton_class.prepend(Module.new do
      def call
        super.tap { |result| yield(result) if block_given? }
      rescue SmartCore::Operation::FatalError => error
        # NOTE: returns SmartCore::Operation::Fatal instance
        error.__operation_result__.tap { |result| yield(result) if block_given? }
      end
    end)
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
smart_core-0.8.1 lib/smart_core/operation/instance_builder.rb
smart_core-0.8.0 lib/smart_core/operation/instance_builder.rb
smart_core-0.7.0 lib/smart_core/operation/instance_builder.rb
smart_core-0.6.0 lib/smart_core/operation/instance_builder.rb
smart_core-0.5.2 lib/smart_core/operation/instance_builder.rb
smart_core-0.5.1 lib/smart_core/operation/instance_builder.rb
smart_core-0.5.0 lib/smart_core/operation/instance_builder.rb