Sha256: 20fc3718c62139a813d8f7c837df3c9cd28ea49a82b0310ec97b2235dc9db46a

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

# @api private
# @since 0.2.0
class SmartCore::Types::Primitive::InvariantControl
  require_relative 'invariant_control/result'
  require_relative 'invariant_control/single'
  require_relative 'invariant_control/chain'
  require_relative 'invariant_control/factory'

  class << self
    # @param invariant_chains [Hash<String,Array<Proc>>]
    # @param invariants [Hash<String,Proc>]
    # @return [SmartCore::Types::Primitive::InvariantControl]
    #
    # @api private
    # @since 0.2.0
    def create(invariant_chains, invariants)
      Factory.create(invariant_chains, invariants)
    end
  end

  # @param invariant_chains [Array<SmartCore::Types::Primitive::InvariantControl::Chain>]
  # @param invariants [Array<SmartCore::Types::Primitive::InvariantControl::Single>]
  # @return [void]
  #
  # @api private
  # @since 0.2.0
  def initialize(invariant_chains, invariants)
    @invariant_chains = invariant_chains
    @invariants = invariants
  end

  # @param value [Any]
  # @return [SmartCore::Types::Primitive::InvariantControl::Result]
  #
  # @api private
  # @since 0.2.0
  def check(value)
    Result.new(self, value).tap do |result|
      invariant_chains.each do |chain|
        result.add_chain_result(chain.check(value))
      end

      invariants.each do |invariant|
        result.add_single_result(invariant.check(value))
      end
    end
  end

  private

  # @return [Array<SmartCore::Types::Primitive::InvariantControl::Chain>]
  #
  # @api private
  # @since 0.2.0
  attr_reader :invariant_chains

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smart_types-0.2.0 lib/smart_core/types/primitive/invariant_control.rb