Sha256: 42f00a6d96a21cd74f40d8be04607ec238f518eaa08653a4a9a14d30e8c6c7a5

Contents?: true

Size: 1.4 KB

Versions: 52

Compression:

Stored size: 1.4 KB

Contents

module Refinery #:nodoc:
  # Error that is raised when a message is invalid.
  class InvalidMessageError < RuntimeError
  end
  
  # Module containing all validations.
  module Validations
    def self.included(base) # :nodoc:
      base.extend(ClassMethods)
    end
    
    # Class methods that are added to the worker.
    module ClassMethods
      # A list of all of the validators. Validators are lambdas
      # that will be called with the message as its only arg.
      # Note that the order of validators is retained.
      def validators
        @validators ||= []
      end
      
      # Validate with the given block. The block must receive a single
      # argument that is the message
      def validate_with(&block)
        validators << block
      end
      alias :validate :validate_with
      
      # Validate that each of the keys exists in the message.
      def validate_key_exists(*args)
        args.each do |key|
          validators << lambda do |message|
            raise Refinery::InvalidMessageError, "Key does not exist in message: #{key}" unless message[key]
          end
        end
      end
      alias :validates_key_exists :validate_key_exists
      alias :validates_presence_of :validate_key_exists
    end
    
    # Validate the given message
    protected
    def validate(message)
      self.class.validators.each do |validator|
        validator.call(message)
      end
    end
  end
end

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
aeden-refinery-0.10.0 lib/refinery/validations.rb
aeden-refinery-0.10.1 lib/refinery/validations.rb
aeden-refinery-0.10.10 lib/refinery/validations.rb
aeden-refinery-0.10.2 lib/refinery/validations.rb
aeden-refinery-0.10.3 lib/refinery/validations.rb
aeden-refinery-0.10.5 lib/refinery/validations.rb
aeden-refinery-0.10.6 lib/refinery/validations.rb
aeden-refinery-0.10.8 lib/refinery/validations.rb
aeden-refinery-0.9.1 lib/refinery/validations.rb
aeden-refinery-0.9.10 lib/refinery/validations.rb
aeden-refinery-0.9.11 lib/refinery/validations.rb
aeden-refinery-0.9.12 lib/refinery/validations.rb
aeden-refinery-0.9.13 lib/refinery/validations.rb
aeden-refinery-0.9.14 lib/refinery/validations.rb
aeden-refinery-0.9.15 lib/refinery/validations.rb
aeden-refinery-0.9.2 lib/refinery/validations.rb
aeden-refinery-0.9.4 lib/refinery/validations.rb
aeden-refinery-0.9.5 lib/refinery/validations.rb
aeden-refinery-0.9.6 lib/refinery/validations.rb
aeden-refinery-0.9.7 lib/refinery/validations.rb