Sha256: bbbfa92b8079d62533c07636f24a6f061139453842a59c2fc94145c80f96e604

Contents?: true

Size: 1.32 KB

Versions: 5

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

# @api private
# @since 0.2.0
# @version 0.3.0
class SmartCore::Types::Primitive::InvariantControl::Chain
  require_relative 'chain/result'

  # @return [String]
  #
  # @api private
  # @since 0.2.0
  attr_reader :name

  # @param name [String]
  # @return [void]
  #
  # @api private
  # @since 0.1.0
  def initialize(name)
    @name = name.dup.tap(&:freeze)
    @invariants = []
  end

  # @param invariant [SmartCore::Types::Primitive::InvariantControl::Single]
  # @return [void]
  #
  # @api private
  # @since 0.2.0
  def add_invariant(invariant)
    invariants << invariant
  end

  # @param value [Any]
  # @param runtime_attributes [Array<Any>]
  # @return [SmartCore::Types::Primitive::InvariantControl::Chain::Result]
  #
  # @api private
  # @since 0.2.0
  # @version 0.3.0
  def check(value, runtime_attributes)
    invariant_results = [].tap do |results|
      invariants.each do |invariant|
        result = invariant.check(value, runtime_attributes).tap { |res| results << res }
        break if result.failure?
      end
    end

    SmartCore::Types::Primitive::InvariantControl::Chain::Result.new(
      self, value, invariant_results
    )
  end

  private

  # @return [Array<SmartCore::Types::Primitive::InvariantControl::Single>]
  #
  # @api private
  # @since 0.2.0
  attr_reader :invariants
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
smart_types-0.7.1 lib/smart_core/types/primitive/invariant_control/chain.rb
smart_types-0.7.0 lib/smart_core/types/primitive/invariant_control/chain.rb
smart_types-0.6.0 lib/smart_core/types/primitive/invariant_control/chain.rb
smart_types-0.4.0 lib/smart_core/types/primitive/invariant_control/chain.rb
smart_types-0.3.0 lib/smart_core/types/primitive/invariant_control/chain.rb