~ubuntu-branches/ubuntu/utopic/ruby-validatable/utopic-proposed

« back to all changes in this revision

Viewing changes to lib/understandable.rb

  • Committer: Package Import Robot
  • Author(s): Cédric Boutillier
  • Date: 2012-05-19 17:34:34 UTC
  • Revision ID: package-import@ubuntu.com-20120519173434-o3pdvc5suz2jzbv4
Tags: upstream-1.6.7
Import upstream version 1.6.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
module Validatable
 
2
  module Understandable #:nodoc:
 
3
    module ClassMethods #:nodoc:
 
4
      def understands(*args)
 
5
        understandings.concat args
 
6
      end
 
7
 
 
8
      def understandings
 
9
        @understandings ||= []
 
10
      end
 
11
 
 
12
      def all_understandings
 
13
        return understandings + self.superclass.all_understandings if self.superclass.respond_to? :all_understandings
 
14
        understandings
 
15
      end
 
16
    end
 
17
 
 
18
    def self.included(klass)
 
19
      klass.extend ClassMethods
 
20
    end
 
21
 
 
22
    def must_understand(hash)
 
23
      invalid_options = hash.inject([]) do |errors, (key, value)|
 
24
        errors << key.to_s unless self.class.all_understandings.include?(key)
 
25
        errors
 
26
      end
 
27
      raise ArgumentError.new("invalid options: #{invalid_options.join(', ')}") if invalid_options.any?
 
28
      true
 
29
    end
 
30
  end
 
31
end
 
 
b'\\ No newline at end of file'