Sha256: c038e58febbad0afab6077185623ecad049a4e3675bf0aa074d0ac520e1fa760

Contents?: true

Size: 808 Bytes

Versions: 3

Compression:

Stored size: 808 Bytes

Contents

module Validatable
  module Understandable #:nodoc:
    module ClassMethods
      def understands(*args)
        understandings.concat args
      end

      def understandings
        @understandings ||= []
      end

      def all_understandings
        return understandings + self.superclass.all_understandings if self.superclass.respond_to? :all_understandings
        understandings
      end
    end

    def self.included(klass)
      klass.extend ClassMethods
    end

    def must_understand(hash)
      invalid_options = hash.inject([]) do |errors, (key, value)|
        errors << key.to_s unless self.class.all_understandings.include?(key)
        errors
      end
      raise ArgumentError.new("invalid options: #{invalid_options.join(', ')}") if invalid_options.any?
      true
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
validatable-1.3.4 lib/understandable.rb
validatable-1.4.0 lib/understandable.rb
validatable-1.3.2 lib/understandable.rb