lib/attestor/validations/validators.rb in attestor-0.2.0 vs lib/attestor/validations/validators.rb in attestor-0.3.0

- old
+ new

@@ -17,12 +17,12 @@ # @param [Array<Attestor::Validators::Validator>] items # # @return [Attestor::Validators] # @private - def initialize(items = []) - @items = items + def initialize(*items) + @items = items.flatten freeze end # Iterates through the collection # @@ -30,42 +30,48 @@ # @yieldparam [Attestor::Validators::Validator] item # items from the collection # # @return [Enumerator] def each - block_given? ? @items.each { |item| yield(item) } : to_enum + block_given? ? items.each { |item| yield(item) } : to_enum end - # Returns validators updated by new item + # Returns validators used in given context # - # @param [#to_sym] name - # @param [Hash] options - # @option options [Array<#to_sym>] :except - # @option options [Array<#to_sym>] :only - # @option options [Symbol, nil] :policy + # @param [#to_sym] context # # @return [Attestor::Validators] - def add(name, options = {}) - item = Validator.new(name, options) - return self if include? item - - self.class.new(@items + [item]) + def set(context) + validators = select { |item| item.used_in_context? context } + (validators == items) ? self : self.class.new(validators) end - # Returns validators used in given context + # Returns validators updated by a new validator with given args # - # @param [#to_sym] context + # @param [Array] args # # @return [Attestor::Validators] - def set(context) - validators = select { |item| item.used_in_context? context } + def add_validator(*args) + add_item Validator, *args + end - self.class.new(validators) + # 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