Sha256: 1b286ec337978f462495928e35fbcd63c6bb8840a8b2d0d95369cd6ecdf5d721

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module Defi
  # This class contains an object that returned or raised during the initialize.
  #
  # @api private
  #
  class Value
    # @return [#object_id] The returned or the raised object.
    attr_reader :object

    # Initialize the value class.
    #
    # @api public
    #
    # @yieldreturn [#object_id] The challenged code.
    # rubocop:disable Lint/RescueException
    def initialize
      @object = yield
      @raised = false
    rescue ::Exception => e
      @object = e
      @raised = true
    end
    # rubocop:enable Lint/RescueException

    # Raise or return the value.
    #
    # @return [#object_id] Raised exception or returned object.
    def call
      raise object if raised?

      object
    end

    # @api public
    #
    # @return [Boolean] The value was raised (or returned)?
    def raised?
      @raised
    end

    # Properties of the value.
    #
    # @api public
    #
    # @return [Hash] The properties of the value.
    def to_h
      {
        raised: raised?,
        object: object
      }
    end

    # String of the value.
    #
    # @api public
    #
    # @return [String] The string representation of the value.
    def to_s
      string = if raised?
                 'raise'
               else
                 'return'
               end

      "#{string} #{object}"
    end

    # A string containing a human-readable representation of the value.
    #
    # @api public
    #
    # @return [String] The human-readable representation of the value.
    def inspect
      "Value(object: #{object}, raised: #{raised?})"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
defi-2.0.2 lib/defi/value.rb