mongomatic 0.1.31 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. data/README.rdoc +90 -26
  2. data/lib/mongomatic.rb +1 -2
  3. data/lib/mongomatic/base.rb +14 -2
  4. data/lib/mongomatic/errors.rb +7 -0
  5. data/test/helper.rb +4 -1
  6. metadata +4 -25
  7. data/lib/mongomatic/validatable.rb +0 -31
  8. data/lib/mongomatic/validatable/child_validation.rb +0 -17
  9. data/lib/mongomatic/validatable/errors.rb +0 -108
  10. data/lib/mongomatic/validatable/included_validation.rb +0 -11
  11. data/lib/mongomatic/validatable/macros.rb +0 -316
  12. data/lib/mongomatic/validatable/object_extension.rb +0 -21
  13. data/lib/mongomatic/validatable/requireable.rb +0 -28
  14. data/lib/mongomatic/validatable/understandable.rb +0 -33
  15. data/lib/mongomatic/validatable/validatable_class_methods.rb +0 -89
  16. data/lib/mongomatic/validatable/validatable_instance_methods.rb +0 -111
  17. data/lib/mongomatic/validatable/validations/validates_acceptance_of.rb +0 -16
  18. data/lib/mongomatic/validatable/validations/validates_associated.rb +0 -15
  19. data/lib/mongomatic/validatable/validations/validates_confirmation_of.rb +0 -25
  20. data/lib/mongomatic/validatable/validations/validates_each.rb +0 -16
  21. data/lib/mongomatic/validatable/validations/validates_exclusion_of.rb +0 -19
  22. data/lib/mongomatic/validatable/validations/validates_format_of.rb +0 -18
  23. data/lib/mongomatic/validatable/validations/validates_inclusion_of.rb +0 -19
  24. data/lib/mongomatic/validatable/validations/validates_length_of.rb +0 -32
  25. data/lib/mongomatic/validatable/validations/validates_numericality_of.rb +0 -28
  26. data/lib/mongomatic/validatable/validations/validates_presence_of.rb +0 -18
  27. data/lib/mongomatic/validatable/validations/validates_true_for.rb +0 -15
  28. data/lib/mongomatic/validatable/validations/validation_base.rb +0 -93
@@ -1,28 +0,0 @@
1
- module Mongomatic
2
- module Validatable
3
- class ValidatesNumericalityOf < ValidationBase #:nodoc:
4
- option :only_integer
5
-
6
- def valid?(instance)
7
- value = value_for(instance)
8
- return true if allow_nil && value.nil?
9
- return true if allow_blank && value.blank?
10
-
11
- value = value.to_s
12
- regex = self.only_integer ? /\A[+-]?\d+\Z/ : /^\d*\.{0,1}\d+$/
13
- not (value =~ regex).nil?
14
- end
15
-
16
- def message(instance)
17
- super || "must be a number"
18
- end
19
-
20
- private
21
- def value_for(instance)
22
- before_typecast_method = "#{self.attribute}_before_typecast"
23
- value_method = instance.respond_to?(before_typecast_method.intern) ? before_typecast_method : self.attribute
24
- instance.send(value_method)
25
- end
26
- end
27
- end
28
- end
@@ -1,18 +0,0 @@
1
- module Mongomatic
2
- module Validatable
3
- class ValidatesPresenceOf < ValidationBase #:nodoc:
4
- def valid?(instance)
5
- value = instance[self.attribute.to_s]
6
- return true if allow_nil && value.nil?
7
- return true if allow_blank && value.blank?
8
-
9
- return false if instance[self.attribute.to_s].nil?
10
- value.respond_to?(:strip) ? instance[self.attribute.to_s].strip.length != 0 : true
11
- end
12
-
13
- def message(instance)
14
- super || "can't be empty"
15
- end
16
- end
17
- end
18
- end
@@ -1,15 +0,0 @@
1
- module Mongomatic
2
- module Validatable
3
- class ValidatesTrueFor < ValidationBase #:nodoc:
4
- required_option :logic
5
-
6
- def valid?(instance)
7
- instance.instance_eval(&logic) == true
8
- end
9
-
10
- def message(instance)
11
- super || "is invalid"
12
- end
13
- end
14
- end
15
- end
@@ -1,93 +0,0 @@
1
- module Mongomatic
2
- module Validatable
3
- class ValidationBase #:nodoc:
4
- def self.required_option(*args)
5
- option(*args)
6
- requires(*args)
7
- end
8
-
9
- def self.option(*args)
10
- attr_accessor(*args)
11
- understands(*args)
12
- end
13
-
14
- def self.default(hash)
15
- defaults.merge! hash
16
- end
17
-
18
- def self.defaults
19
- @defaults ||= {}
20
- end
21
-
22
- def self.all_defaults
23
- return defaults.merge(self.superclass.all_defaults) if self.superclass.respond_to? :all_defaults
24
- defaults
25
- end
26
-
27
- def self.after_validate(&block)
28
- after_validations << block
29
- end
30
-
31
- def self.after_validations
32
- @after_validations ||= []
33
- end
34
-
35
- def self.all_after_validations
36
- return after_validations + self.superclass.all_after_validations if self.superclass.respond_to? :all_after_validations
37
- after_validations
38
- end
39
-
40
- include Understandable
41
- include Requireable
42
-
43
- option :message, :if, :times, :level, :groups, :key, :after_validate, :allow_nil, :allow_blank
44
- default :level => 1, :groups => []
45
- attr_accessor :attribute
46
-
47
- def initialize(klass, attribute, options={})
48
- must_understand options
49
- requires options
50
- self.class.all_understandings.each do |understanding|
51
- options[understanding] = self.class.all_defaults[understanding] unless options.has_key? understanding
52
- self.instance_variable_set("@#{understanding}", options[understanding])
53
- end
54
- self.attribute = attribute
55
- self.groups = [self.groups] unless self.groups.is_a?(Array)
56
- self.key = "#{klass.name}/#{self.class.name}/#{self.key || self.attribute}"
57
- raise_error_if_key_is_dup(klass)
58
- end
59
-
60
- def raise_error_if_key_is_dup(klass)
61
- message = "key #{self.key} must be unique, provide the :key option to specify a unique key"
62
- raise ArgumentError.new(message) if klass.validation_keys_include? self.key
63
- end
64
-
65
- def should_validate?(instance)
66
- result = validate_this_time?(instance)
67
- case self.if
68
- when Proc
69
- result &&= instance.instance_eval(&self.if)
70
- when Symbol, String
71
- result &&= instance.instance_eval(self.if.to_s)
72
- end
73
- result
74
- end
75
-
76
- def message(instance)
77
- @message.respond_to?(:call) ? instance.instance_eval(&@message) : @message
78
- end
79
-
80
- def validate_this_time?(instance)
81
- return true if @times.nil?
82
- self.times > instance.times_validated(self.key)
83
- end
84
-
85
- def run_after_validate(result, instance, attribute)
86
- self.class.all_after_validations.each do |block|
87
- block.call result, instance, attribute
88
- end
89
- instance.instance_eval_with_params result, attribute, &self.after_validate unless self.after_validate.nil?
90
- end
91
- end
92
- end
93
- end