mongomatic 0.0.2 → 0.0.3

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