Sha256: 974f40bed59a225296e9a2bc428a267baa1e29dc38a740101380cbdf756bfcb2

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

module DataMapper
  module Validate

    ##
    #
    # @author Guy van den Berg
    # @since  0.9
    class ContextualValidators

      def dump
        contexts.each_pair do |key,context|
          puts "Key=#{key} Context: #{context}"
        end
      end

      # Get a hash of named context validators for the resource
      #
      # @return <Hash> a hash of validators <GenericValidator>
      def contexts
        @contexts ||= @contexts = {}
      end

      # Return an array of validators for a named context
      #
      # @return <Array> An array of validators
      def context(name)
        contexts[name] = [] unless contexts.has_key?(name)
        contexts[name]
      end

      # Clear all named context validators off of the resource
      #
      def clear!
        contexts.clear
      end

      # Execute all validators in the named context against the target
      #
      # @param <Symbol> named_context the context we are validating against
      # @param <Object> target        the resource that we are validating
      # @return <Boolean> true if all are valid, otherwise false
      def execute(named_context, target)
        raise(ArgumentError, 'invalid context specified') if !named_context || (contexts.length > 0 && !contexts[named_context])
        target.errors.clear!
        result = true
        context(named_context).each do |validator|
          next unless validator.execute?(target)
          result = false unless validator.call(target)
        end

        result
      end

    end # module ContextualValidators
  end # module Validate
end # module DataMapper

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
dm-validations-0.9.5 lib/dm-validations/contextual_validators.rb
dm-validations-0.9.4 lib/dm-validations/contextual_validators.rb
dm-validations-0.9.6 lib/dm-validations/contextual_validators.rb
dm-validations-0.9.7 lib/dm-validations/contextual_validators.rb
dm-validations-0.9.8 lib/dm-validations/contextual_validators.rb
mack-data_mapper-0.8.2 lib/gems/dm-validations-0.9.7/lib/dm-validations/contextual_validators.rb