davber_couchrest_extended_document 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/LICENSE +176 -0
  2. data/README.md +250 -0
  3. data/Rakefile +69 -0
  4. data/THANKS.md +22 -0
  5. data/examples/model/example.rb +144 -0
  6. data/history.txt +165 -0
  7. data/lib/couchrest/casted_array.rb +39 -0
  8. data/lib/couchrest/casted_model.rb +53 -0
  9. data/lib/couchrest/extended_document.rb +262 -0
  10. data/lib/couchrest/mixins.rb +12 -0
  11. data/lib/couchrest/mixins/attribute_protection.rb +74 -0
  12. data/lib/couchrest/mixins/attributes.rb +75 -0
  13. data/lib/couchrest/mixins/callbacks.rb +534 -0
  14. data/lib/couchrest/mixins/class_proxy.rb +120 -0
  15. data/lib/couchrest/mixins/collection.rb +260 -0
  16. data/lib/couchrest/mixins/design_doc.rb +159 -0
  17. data/lib/couchrest/mixins/document_queries.rb +82 -0
  18. data/lib/couchrest/mixins/extended_attachments.rb +73 -0
  19. data/lib/couchrest/mixins/properties.rb +130 -0
  20. data/lib/couchrest/mixins/typecast.rb +174 -0
  21. data/lib/couchrest/mixins/views.rb +148 -0
  22. data/lib/couchrest/property.rb +96 -0
  23. data/lib/couchrest/support/couchrest.rb +19 -0
  24. data/lib/couchrest/support/rails.rb +42 -0
  25. data/lib/couchrest/validation.rb +246 -0
  26. data/lib/couchrest/validation/auto_validate.rb +156 -0
  27. data/lib/couchrest/validation/contextual_validators.rb +78 -0
  28. data/lib/couchrest/validation/validation_errors.rb +125 -0
  29. data/lib/couchrest/validation/validators/absent_field_validator.rb +74 -0
  30. data/lib/couchrest/validation/validators/confirmation_validator.rb +107 -0
  31. data/lib/couchrest/validation/validators/format_validator.rb +122 -0
  32. data/lib/couchrest/validation/validators/formats/email.rb +66 -0
  33. data/lib/couchrest/validation/validators/formats/url.rb +43 -0
  34. data/lib/couchrest/validation/validators/generic_validator.rb +120 -0
  35. data/lib/couchrest/validation/validators/length_validator.rb +139 -0
  36. data/lib/couchrest/validation/validators/method_validator.rb +89 -0
  37. data/lib/couchrest/validation/validators/numeric_validator.rb +109 -0
  38. data/lib/couchrest/validation/validators/required_field_validator.rb +114 -0
  39. data/lib/couchrest_extended_document.rb +23 -0
  40. data/spec/couchrest/attribute_protection_spec.rb +150 -0
  41. data/spec/couchrest/casted_extended_doc_spec.rb +79 -0
  42. data/spec/couchrest/casted_model_spec.rb +424 -0
  43. data/spec/couchrest/extended_doc_attachment_spec.rb +148 -0
  44. data/spec/couchrest/extended_doc_inherited_spec.rb +40 -0
  45. data/spec/couchrest/extended_doc_spec.rb +869 -0
  46. data/spec/couchrest/extended_doc_subclass_spec.rb +101 -0
  47. data/spec/couchrest/extended_doc_view_spec.rb +529 -0
  48. data/spec/couchrest/property_spec.rb +790 -0
  49. data/spec/fixtures/attachments/README +3 -0
  50. data/spec/fixtures/attachments/couchdb.png +0 -0
  51. data/spec/fixtures/attachments/test.html +11 -0
  52. data/spec/fixtures/more/article.rb +35 -0
  53. data/spec/fixtures/more/card.rb +22 -0
  54. data/spec/fixtures/more/cat.rb +22 -0
  55. data/spec/fixtures/more/course.rb +25 -0
  56. data/spec/fixtures/more/event.rb +8 -0
  57. data/spec/fixtures/more/invoice.rb +17 -0
  58. data/spec/fixtures/more/person.rb +9 -0
  59. data/spec/fixtures/more/question.rb +7 -0
  60. data/spec/fixtures/more/service.rb +12 -0
  61. data/spec/fixtures/more/user.rb +22 -0
  62. data/spec/fixtures/views/lib.js +3 -0
  63. data/spec/fixtures/views/test_view/lib.js +3 -0
  64. data/spec/fixtures/views/test_view/only-map.js +4 -0
  65. data/spec/fixtures/views/test_view/test-map.js +3 -0
  66. data/spec/fixtures/views/test_view/test-reduce.js +3 -0
  67. data/spec/spec.opts +5 -0
  68. data/spec/spec_helper.rb +49 -0
  69. data/utils/remap.rb +27 -0
  70. data/utils/subset.rb +30 -0
  71. metadata +225 -0
@@ -0,0 +1,89 @@
1
+ # Extracted from dm-validations 0.9.10
2
+ #
3
+ # Copyright (c) 2007 Guy van den Berg
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ module CouchRest
25
+ module Validation
26
+
27
+ ##
28
+ #
29
+ # @author Guy van den Berg
30
+ # @since 0.9
31
+ class MethodValidator < GenericValidator
32
+
33
+ def initialize(field_name, options={})
34
+ super
35
+ @field_name, @options = field_name, options.clone
36
+ @options[:method] = @field_name unless @options.has_key?(:method)
37
+ end
38
+
39
+ def call(target)
40
+ result, message = target.send(@options[:method])
41
+ add_error(target, message, field_name) unless result
42
+ result
43
+ end
44
+
45
+ def ==(other)
46
+ @options[:method] == other.instance_variable_get(:@options)[:method] && super
47
+ end
48
+ end # class MethodValidator
49
+
50
+ module ValidatesWithMethod
51
+
52
+ ##
53
+ # Validate using the given method. The method given needs to return:
54
+ # [result::<Boolean>, Error Message::<String>]
55
+ #
56
+ # @example [Usage]
57
+ #
58
+ # class Page
59
+ #
60
+ # property :zip_code, String
61
+ #
62
+ # validates_with_method :in_the_right_location?
63
+ #
64
+ # def in_the_right_location?
65
+ # if @zip_code == "94301"
66
+ # return true
67
+ # else
68
+ # return [false, "You're in the wrong zip code"]
69
+ # end
70
+ # end
71
+ #
72
+ # # A call to valid? will return false and
73
+ # # populate the object's errors with "You're in the
74
+ # # wrong zip code" unless zip_code == "94301"
75
+ #
76
+ # # You can also specify field:
77
+ #
78
+ # validates_with_method :zip_code, :in_the_right_location?
79
+ #
80
+ # # it will add returned error message to :zip_code field
81
+ #
82
+ def validates_with_method(*fields)
83
+ opts = opts_from_validator_args(fields)
84
+ add_validator_to_context(opts, fields, CouchRest::Validation::MethodValidator)
85
+ end
86
+
87
+ end # module ValidatesWithMethod
88
+ end # module Validation
89
+ end # module CouchRest
@@ -0,0 +1,109 @@
1
+ # Extracted from dm-validations 0.9.10
2
+ #
3
+ # Copyright (c) 2007 Guy van den Berg
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ module CouchRest
25
+ module Validation
26
+
27
+ ##
28
+ #
29
+ # @author Guy van den Berg
30
+ # @since 0.9
31
+ class NumericValidator < GenericValidator
32
+
33
+ def initialize(field_name, options={})
34
+ super
35
+ @field_name, @options = field_name, options
36
+ @options[:integer_only] = false unless @options.has_key?(:integer_only)
37
+ end
38
+
39
+ def call(target)
40
+ value = target.send(field_name)
41
+ return true if @options[:allow_nil] && value.nil?
42
+
43
+ value = (defined?(BigDecimal) && value.kind_of?(BigDecimal)) ? value.to_s('F') : value.to_s
44
+
45
+ error_message = @options[:message]
46
+ precision = @options[:precision]
47
+ scale = @options[:scale]
48
+
49
+ if @options[:integer_only]
50
+ return true if value =~ /\A[+-]?\d+\z/
51
+ error_message ||= ValidationErrors.default_error_message(:not_an_integer, field_name)
52
+ else
53
+ # FIXME: if precision and scale are not specified, can we assume that it is an integer?
54
+ # probably not, as floating point numbers don't have hard
55
+ # defined scale. the scale floats with the length of the
56
+ # integral and precision. Ie. if precision = 10 and integral
57
+ # portion of the number is 9834 (4 digits), the max scale will
58
+ # be 6 (10 - 4). But if the integral length is 1, max scale
59
+ # will be (10 - 1) = 9, so 1.234567890.
60
+ if precision && scale
61
+ #handles both Float when it has scale specified and BigDecimal
62
+ if precision > scale && scale > 0
63
+ return true if value =~ /\A[+-]?(?:\d{1,#{precision - scale}}|\d{0,#{precision - scale}}\.\d{1,#{scale}})\z/
64
+ elsif precision > scale && scale == 0
65
+ return true if value =~ /\A[+-]?(?:\d{1,#{precision}}(?:\.0)?)\z/
66
+ elsif precision == scale
67
+ return true if value =~ /\A[+-]?(?:0(?:\.\d{1,#{scale}})?)\z/
68
+ else
69
+ raise ArgumentError, "Invalid precision #{precision.inspect} and scale #{scale.inspect} for #{field_name} (value: #{value.inspect} #{value.class})"
70
+ end
71
+ elsif precision && scale.nil?
72
+ # for floats, if scale is not set
73
+
74
+ #total number of digits is less or equal precision
75
+ return true if value.gsub(/[^\d]/, '').length <= precision
76
+
77
+ #number of digits before decimal == precision, and the number is x.0. same as scale = 0
78
+ return true if value =~ /\A[+-]?(?:\d{1,#{precision}}(?:\.0)?)\z/
79
+ else
80
+ return true if value =~ /\A[+-]?(?:\d+|\d*\.\d+)\z/
81
+ end
82
+ error_message ||= ValidationErrors.default_error_message(:not_a_number, field_name)
83
+ end
84
+
85
+ add_error(target, error_message, field_name)
86
+
87
+ # TODO: check the gt, gte, lt, lte, and eq options
88
+
89
+ return false
90
+ end
91
+ end # class NumericValidator
92
+
93
+ module ValidatesIsNumber
94
+
95
+ # Validate whether a field is numeric
96
+ #
97
+ def validates_numericality_of(*fields)
98
+ opts = opts_from_validator_args(fields)
99
+ add_validator_to_context(opts, fields, CouchRest::Validation::NumericValidator)
100
+ end
101
+
102
+ def validates_is_number(*fields)
103
+ warn "[DEPRECATION] `validates_is_number` is deprecated. Please use `validates_numericality_of` instead."
104
+ validates_numericality_of(*fields)
105
+ end
106
+
107
+ end # module ValidatesIsNumber
108
+ end # module Validation
109
+ end # module CouchRest
@@ -0,0 +1,114 @@
1
+ # Extracted from dm-validations 0.9.10
2
+ #
3
+ # Copyright (c) 2007 Guy van den Berg
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining
6
+ # a copy of this software and associated documentation files (the
7
+ # "Software"), to deal in the Software without restriction, including
8
+ # without limitation the rights to use, copy, modify, merge, publish,
9
+ # distribute, sublicense, and/or sell copies of the Software, and to
10
+ # permit persons to whom the Software is furnished to do so, subject to
11
+ # the following conditions:
12
+ #
13
+ # The above copyright notice and this permission notice shall be
14
+ # included in all copies or substantial portions of the Software.
15
+ #
16
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ module CouchRest
25
+ module Validation
26
+
27
+ ##
28
+ #
29
+ # @author Guy van den Berg
30
+ # @since 0.9
31
+ class RequiredFieldValidator < GenericValidator
32
+
33
+ def initialize(field_name, options={})
34
+ super
35
+ @field_name, @options = field_name, options
36
+ end
37
+
38
+ def call(target)
39
+ value = target.validation_property_value(field_name)
40
+ property = target.validation_property(field_name.to_s)
41
+ return true if present?(value, property)
42
+
43
+ error_message = @options[:message] || default_error(property)
44
+ add_error(target, error_message, field_name)
45
+
46
+ false
47
+ end
48
+
49
+ protected
50
+
51
+ # Boolean property types are considered present if non-nil.
52
+ # Other property types are considered present if non-blank.
53
+ # Non-properties are considered present if non-blank.
54
+ def present?(value, property)
55
+ boolean_type?(property) ? !value.nil? : !value.blank?
56
+ end
57
+
58
+ def default_error(property)
59
+ actual = boolean_type?(property) ? :nil : :blank
60
+ ValidationErrors.default_error_message(actual, field_name)
61
+ end
62
+
63
+ # Is +property+ a boolean property?
64
+ #
65
+ # Returns true for Boolean, ParanoidBoolean, TrueClass, etc. properties.
66
+ # Returns false for other property types.
67
+ # Returns false for non-properties.
68
+ def boolean_type?(property)
69
+ property ? property.type == 'Boolean' : false
70
+ end
71
+
72
+ end # class RequiredFieldValidator
73
+
74
+ module ValidatesPresent
75
+
76
+ ##
77
+ # Validates that the specified attribute is present.
78
+ #
79
+ # For most property types "being present" is the same as being "not
80
+ # blank" as determined by the attribute's #blank? method. However, in
81
+ # the case of Boolean, "being present" means not nil; i.e. true or
82
+ # false.
83
+ #
84
+ # @note
85
+ # dm-core's support lib adds the blank? method to many classes,
86
+ # @see lib/dm-core/support/blank.rb (dm-core) for more information.
87
+ #
88
+ # @example [Usage]
89
+ #
90
+ # class Page
91
+ #
92
+ # property :required_attribute, String
93
+ # property :another_required, String
94
+ # property :yet_again, String
95
+ #
96
+ # validates_presence_of :required_attribute
97
+ # validates_presence_of :another_required, :yet_again
98
+ #
99
+ # # a call to valid? will return false unless
100
+ # # all three attributes are !blank?
101
+ # end
102
+ def validates_presence_of(*fields)
103
+ opts = opts_from_validator_args(fields)
104
+ add_validator_to_context(opts, fields, CouchRest::Validation::RequiredFieldValidator)
105
+ end
106
+
107
+ def validates_present(*fields)
108
+ warn "[DEPRECATION] `validates_present` is deprecated. Please use `validates_presence_of` instead."
109
+ validates_presence_of(*fields)
110
+ end
111
+
112
+ end # module ValidatesPresent
113
+ end # module Validation
114
+ end # module CouchRest
@@ -0,0 +1,23 @@
1
+ gem 'couchrest', ">=1.0.0.beta"
2
+ require 'couchrest'
3
+
4
+ gem "builder", ">=2.1.2"
5
+
6
+ gem 'activesupport', ">= 2.3.0"
7
+ require 'active_support/core_ext'
8
+ require 'active_support/json'
9
+
10
+ gem "mime-types", ">= 1.15"
11
+ require 'mime/types'
12
+ require "enumerator"
13
+
14
+ # Monkey patches applied to couchrest
15
+ require File.join(File.dirname(__FILE__), 'couchrest', 'support', 'couchrest')
16
+
17
+ # Base libraries
18
+ require File.join(File.dirname(__FILE__), 'couchrest', 'extended_document')
19
+ require File.join(File.dirname(__FILE__), 'couchrest', 'casted_model')
20
+
21
+ # Add rails support *after* everything has loaded
22
+ require File.join(File.dirname(__FILE__), 'couchrest', 'support', 'rails') if defined?(Rails)
23
+
@@ -0,0 +1,150 @@
1
+ require File.expand_path("../../spec_helper", __FILE__)
2
+
3
+ describe "ExtendedDocument", "no declarations" do
4
+ class NoProtection < CouchRest::ExtendedDocument
5
+ use_database TEST_SERVER.default_database
6
+ property :name
7
+ property :phone
8
+ end
9
+
10
+ it "should not protect anything through new" do
11
+ user = NoProtection.new(:name => "will", :phone => "555-5555")
12
+
13
+ user.name.should == "will"
14
+ user.phone.should == "555-5555"
15
+ end
16
+
17
+ it "should not protect anything through attributes=" do
18
+ user = NoProtection.new
19
+ user.attributes = {:name => "will", :phone => "555-5555"}
20
+
21
+ user.name.should == "will"
22
+ user.phone.should == "555-5555"
23
+ end
24
+
25
+ it "should recreate from the database properly" do
26
+ user = NoProtection.new
27
+ user.name = "will"
28
+ user.phone = "555-5555"
29
+ user.save!
30
+
31
+ user = NoProtection.get(user.id)
32
+ user.name.should == "will"
33
+ user.phone.should == "555-5555"
34
+ end
35
+ end
36
+
37
+ describe "ExtendedDocument", "accessible flag" do
38
+ class WithAccessible < CouchRest::ExtendedDocument
39
+ use_database TEST_SERVER.default_database
40
+ property :name, :accessible => true
41
+ property :admin, :default => false
42
+ end
43
+
44
+ it "should recognize accessible properties" do
45
+ props = WithAccessible.accessible_properties.map { |prop| prop.name}
46
+ props.should include("name")
47
+ props.should_not include("admin")
48
+ end
49
+
50
+ it "should protect non-accessible properties set through new" do
51
+ user = WithAccessible.new(:name => "will", :admin => true)
52
+
53
+ user.name.should == "will"
54
+ user.admin.should == false
55
+ end
56
+
57
+ it "should protect non-accessible properties set through attributes=" do
58
+ user = WithAccessible.new
59
+ user.attributes = {:name => "will", :admin => true}
60
+
61
+ user.name.should == "will"
62
+ user.admin.should == false
63
+ end
64
+ end
65
+
66
+ describe "ExtendedDocument", "protected flag" do
67
+ class WithProtected < CouchRest::ExtendedDocument
68
+ use_database TEST_SERVER.default_database
69
+ property :name
70
+ property :admin, :default => false, :protected => true
71
+ end
72
+
73
+ it "should recognize protected properties" do
74
+ props = WithProtected.protected_properties.map { |prop| prop.name}
75
+ props.should_not include("name")
76
+ props.should include("admin")
77
+ end
78
+
79
+ it "should protect non-accessible properties set through new" do
80
+ user = WithProtected.new(:name => "will", :admin => true)
81
+
82
+ user.name.should == "will"
83
+ user.admin.should == false
84
+ end
85
+
86
+ it "should protect non-accessible properties set through attributes=" do
87
+ user = WithProtected.new
88
+ user.attributes = {:name => "will", :admin => true}
89
+
90
+ user.name.should == "will"
91
+ user.admin.should == false
92
+ end
93
+ end
94
+
95
+ describe "ExtendedDocument", "protected flag" do
96
+ class WithBoth < CouchRest::ExtendedDocument
97
+ use_database TEST_SERVER.default_database
98
+ property :name, :accessible => true
99
+ property :admin, :default => false, :protected => true
100
+ end
101
+
102
+ it "should raise an error when both are set" do
103
+ lambda { WithBoth.new }.should raise_error
104
+ end
105
+ end
106
+
107
+ describe "ExtendedDocument", "from database" do
108
+ class WithProtected < CouchRest::ExtendedDocument
109
+ use_database TEST_SERVER.default_database
110
+ property :name
111
+ property :admin, :default => false, :protected => true
112
+ view_by :name
113
+ end
114
+
115
+ before(:each) do
116
+ @user = WithProtected.new
117
+ @user.name = "will"
118
+ @user.admin = true
119
+ @user.save!
120
+ end
121
+
122
+ def verify_attrs(user)
123
+ user.name.should == "will"
124
+ user.admin.should == true
125
+ end
126
+
127
+ it "ExtendedDocument#get should not strip protected attributes" do
128
+ reloaded = WithProtected.get( @user.id )
129
+ verify_attrs reloaded
130
+ end
131
+
132
+ it "ExtendedDocument#get! should not strip protected attributes" do
133
+ reloaded = WithProtected.get!( @user.id )
134
+ verify_attrs reloaded
135
+ end
136
+
137
+ it "ExtendedDocument#all should not strip protected attributes" do
138
+ # all creates a CollectionProxy
139
+ docs = WithProtected.all(:key => @user.id)
140
+ docs.size.should == 1
141
+ reloaded = docs.first
142
+ verify_attrs reloaded
143
+ end
144
+
145
+ it "views should not strip protected attributes" do
146
+ docs = WithProtected.by_name(:startkey => "will", :endkey => "will")
147
+ reloaded = docs.first
148
+ verify_attrs reloaded
149
+ end
150
+ end