Sha256: f67d8e97e2bd7d62381cae3df7ffdc4d7367841732d39ca41c659428630c6794

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

# @api public
# @since 0.2.0
class SmartCore::Operation::Success < SmartCore::Operation::Result
  # @param result_options [Hash<Symbol,Any>]
  # @return [void]
  #
  # @api public
  # @since 0.2.0
  def initialize(**result_options)
    __prevent_method_overlapping__(result_options)
    super(**result_options) # NOTE: initialize result object
    __define_virtual_result_data_accessors__(result_options)
  end

  # @yield [nil]
  # @return [Boolean]
  #
  # @api public
  # @since 0.2.0
  def success?
    true.tap { yield(self) if block_given? }
  end

  private

  # @param result_options [Hash<Symbol,Any>]
  # @return [void]
  #
  # @raise [SmartCore::Operation::IncompatibleResultKeyError]
  # @raise [SmartCore::Operation::ResultMethodIntersectionError]
  #
  # @api private
  # @since 0.2.0
  def __prevent_method_overlapping__(result_options)
    overlappings = result_options.each_key.each_with_object([]) do |key, overlap|
      overlap << key if self.class.__core_methods__.include?(key)
    end

    raise(
      SmartCore::Operation::ResultMethodIntersectionError,
      "Result keys can not overlap core methods " \
      "(overlapping keys: #{overlappings.join(', ')})."
    ) if overlappings.any?
  end

  # @param result_options [Hash<Symbol,Any>]
  # @return [void]
  #
  # @api private
  # @since 0.2.0
  def __define_virtual_result_data_accessors__(result_options)
    result_options.each_key do |result_attribute_name|
      define_singleton_method(result_attribute_name) do
        __result_options__[result_attribute_name]
      end
    end
  end

  core_methods = (
    instance_methods(false) | private_instance_methods(false) |
    superclass.instance_methods(false) | superclass.private_instance_methods(false)
  ).freeze

  # @return [Array<Symbol>]
  #
  # @api private
  # @since 0.2.0
  define_singleton_method(:__core_methods__) { core_methods }
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
smart_core-0.4.0 lib/smart_core/operation/success.rb
smart_core-0.3.0 lib/smart_core/operation/success.rb
smart_core-0.2.0 lib/smart_core/operation/success.rb