mongomatic 0.1.4 → 0.1.31

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 +26 -90
  2. data/lib/mongomatic.rb +2 -1
  3. data/lib/mongomatic/base.rb +2 -14
  4. data/lib/mongomatic/validatable.rb +31 -0
  5. data/lib/mongomatic/validatable/child_validation.rb +17 -0
  6. data/lib/mongomatic/validatable/errors.rb +108 -0
  7. data/lib/mongomatic/validatable/included_validation.rb +11 -0
  8. data/lib/mongomatic/validatable/macros.rb +316 -0
  9. data/lib/mongomatic/validatable/object_extension.rb +21 -0
  10. data/lib/mongomatic/validatable/requireable.rb +28 -0
  11. data/lib/mongomatic/validatable/understandable.rb +33 -0
  12. data/lib/mongomatic/validatable/validatable_class_methods.rb +89 -0
  13. data/lib/mongomatic/validatable/validatable_instance_methods.rb +111 -0
  14. data/lib/mongomatic/validatable/validations/validates_acceptance_of.rb +16 -0
  15. data/lib/mongomatic/validatable/validations/validates_associated.rb +15 -0
  16. data/lib/mongomatic/validatable/validations/validates_confirmation_of.rb +25 -0
  17. data/lib/mongomatic/validatable/validations/validates_each.rb +16 -0
  18. data/lib/mongomatic/validatable/validations/validates_exclusion_of.rb +19 -0
  19. data/lib/mongomatic/validatable/validations/validates_format_of.rb +18 -0
  20. data/lib/mongomatic/validatable/validations/validates_inclusion_of.rb +19 -0
  21. data/lib/mongomatic/validatable/validations/validates_length_of.rb +32 -0
  22. data/lib/mongomatic/validatable/validations/validates_numericality_of.rb +28 -0
  23. data/lib/mongomatic/validatable/validations/validates_presence_of.rb +18 -0
  24. data/lib/mongomatic/validatable/validations/validates_true_for.rb +15 -0
  25. data/lib/mongomatic/validatable/validations/validation_base.rb +93 -0
  26. data/test/helper.rb +1 -4
  27. metadata +24 -3
  28. data/lib/mongomatic/errors.rb +0 -7
@@ -0,0 +1,28 @@
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
@@ -0,0 +1,18 @@
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
@@ -0,0 +1,15 @@
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
@@ -0,0 +1,93 @@
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
data/test/helper.rb CHANGED
@@ -10,16 +10,13 @@ require 'mongomatic'
10
10
  Mongomatic.db = Mongo::Connection.new.db("mongomatic_test")
11
11
 
12
12
  class Person < Mongomatic::Base
13
+ validates_presence_of :name
13
14
  attr_accessor :callback_tests
14
15
 
15
16
  def self.create_indexes
16
17
  collection.create_index("name", :unique => true)
17
18
  end
18
19
 
19
- def validate
20
- self.errors << ["Name", "can't be empty"] if self["name"].blank?
21
- end
22
-
23
20
  def before_validate
24
21
  self.callback_tests ||= []
25
22
  self.callback_tests << :before_validate
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 4
9
- version: 0.1.4
8
+ - 31
9
+ version: 0.1.31
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Myles
@@ -105,8 +105,29 @@ files:
105
105
  - lib/mongomatic.rb
106
106
  - lib/mongomatic/base.rb
107
107
  - lib/mongomatic/cursor.rb
108
- - lib/mongomatic/errors.rb
109
108
  - lib/mongomatic/modifiers.rb
109
+ - lib/mongomatic/validatable.rb
110
+ - lib/mongomatic/validatable/child_validation.rb
111
+ - lib/mongomatic/validatable/errors.rb
112
+ - lib/mongomatic/validatable/included_validation.rb
113
+ - lib/mongomatic/validatable/macros.rb
114
+ - lib/mongomatic/validatable/object_extension.rb
115
+ - lib/mongomatic/validatable/requireable.rb
116
+ - lib/mongomatic/validatable/understandable.rb
117
+ - lib/mongomatic/validatable/validatable_class_methods.rb
118
+ - lib/mongomatic/validatable/validatable_instance_methods.rb
119
+ - lib/mongomatic/validatable/validations/validates_acceptance_of.rb
120
+ - lib/mongomatic/validatable/validations/validates_associated.rb
121
+ - lib/mongomatic/validatable/validations/validates_confirmation_of.rb
122
+ - lib/mongomatic/validatable/validations/validates_each.rb
123
+ - lib/mongomatic/validatable/validations/validates_exclusion_of.rb
124
+ - lib/mongomatic/validatable/validations/validates_format_of.rb
125
+ - lib/mongomatic/validatable/validations/validates_inclusion_of.rb
126
+ - lib/mongomatic/validatable/validations/validates_length_of.rb
127
+ - lib/mongomatic/validatable/validations/validates_numericality_of.rb
128
+ - lib/mongomatic/validatable/validations/validates_presence_of.rb
129
+ - lib/mongomatic/validatable/validations/validates_true_for.rb
130
+ - lib/mongomatic/validatable/validations/validation_base.rb
110
131
  - LICENSE
111
132
  - README.rdoc
112
133
  - test/helper.rb
@@ -1,7 +0,0 @@
1
- module Mongomatic
2
- class Errors < Array
3
- def full_messages(sep=" ")
4
- collect { |e| e.join(sep) }
5
- end
6
- end
7
- end