Sha256: 7328507b99dedb425290879ad9a8e351e61c239c6a798dfeacc409cc04cb2b34

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

# encoding: utf-8

module Attestor

  module Validations

    # The collection of validations used by class instances
    #
    # @api private
    class Validators
      include Enumerable

      # @!scope class
      # @!method new(items = [])
      # Creates an immutable collection with optional list of items
      #
      # @param [Array<Attestor::Validators::Validator>] items
      #
      # @return [Attestor::Validators]

      # @private
      def initialize(*items)
        @items = items.flatten
        freeze
      end

      # Iterates through the collection
      #
      # @yield the block
      # @yieldparam [Attestor::Validators::Validator] item
      #   items from the collection
      #
      # @return [Enumerator]
      def each
        block_given? ? items.each { |item| yield(item) } : to_enum
      end

      # Returns validators used in given context
      #
      # @param  [#to_sym] context
      #
      # @return [Attestor::Validators]
      def set(context)
        validators = select { |item| item.used_in_context? context }
        (validators == items) ? self : self.class.new(validators)
      end

      # Returns validators updated by a new validator with given args
      #
      # @param  [Array] args
      #
      # @return [Attestor::Validators]
      def add_validator(*args)
        add_item Validator, *args
      end

      # Returns validators updated by a new follower with given args
      #
      # @param  [Array] args
      #
      # @return [Attestor::Validators]
      def add_follower(*args)
        add_item Follower, *args
      end

      private

      attr_reader :items

      def add_item(type, *args)
        item = type.new(*args)
        include?(item) ? self : self.class.new(items, item)
      end

    end # class Validators

  end # module Validations

end # module Attestor

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
attestor-0.3.0 lib/attestor/validations/validators.rb