couchrest_extended_document 1.0.0.beta5

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 +68 -0
  3. data/Rakefile +68 -0
  4. data/THANKS.md +19 -0
  5. data/examples/model/example.rb +144 -0
  6. data/history.txt +159 -0
  7. data/lib/couchrest/casted_array.rb +25 -0
  8. data/lib/couchrest/casted_model.rb +55 -0
  9. data/lib/couchrest/extended_document.rb +323 -0
  10. data/lib/couchrest/mixins/attribute_protection.rb +74 -0
  11. data/lib/couchrest/mixins/callbacks.rb +532 -0
  12. data/lib/couchrest/mixins/class_proxy.rb +120 -0
  13. data/lib/couchrest/mixins/collection.rb +260 -0
  14. data/lib/couchrest/mixins/design_doc.rb +127 -0
  15. data/lib/couchrest/mixins/document_queries.rb +82 -0
  16. data/lib/couchrest/mixins/extended_attachments.rb +73 -0
  17. data/lib/couchrest/mixins/properties.rb +162 -0
  18. data/lib/couchrest/mixins/validation.rb +245 -0
  19. data/lib/couchrest/mixins/views.rb +148 -0
  20. data/lib/couchrest/mixins.rb +11 -0
  21. data/lib/couchrest/property.rb +50 -0
  22. data/lib/couchrest/support/couchrest.rb +19 -0
  23. data/lib/couchrest/support/rails.rb +42 -0
  24. data/lib/couchrest/typecast.rb +175 -0
  25. data/lib/couchrest/validation/auto_validate.rb +156 -0
  26. data/lib/couchrest/validation/contextual_validators.rb +78 -0
  27. data/lib/couchrest/validation/validation_errors.rb +125 -0
  28. data/lib/couchrest/validation/validators/absent_field_validator.rb +74 -0
  29. data/lib/couchrest/validation/validators/confirmation_validator.rb +107 -0
  30. data/lib/couchrest/validation/validators/format_validator.rb +122 -0
  31. data/lib/couchrest/validation/validators/formats/email.rb +66 -0
  32. data/lib/couchrest/validation/validators/formats/url.rb +43 -0
  33. data/lib/couchrest/validation/validators/generic_validator.rb +120 -0
  34. data/lib/couchrest/validation/validators/length_validator.rb +139 -0
  35. data/lib/couchrest/validation/validators/method_validator.rb +89 -0
  36. data/lib/couchrest/validation/validators/numeric_validator.rb +109 -0
  37. data/lib/couchrest/validation/validators/required_field_validator.rb +114 -0
  38. data/lib/couchrest/validation.rb +245 -0
  39. data/lib/couchrest_extended_document.rb +21 -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 +406 -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 +868 -0
  46. data/spec/couchrest/extended_doc_subclass_spec.rb +99 -0
  47. data/spec/couchrest/extended_doc_view_spec.rb +529 -0
  48. data/spec/couchrest/property_spec.rb +648 -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 +6 -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 +200 -0
@@ -0,0 +1,22 @@
1
+ class Card < CouchRest::ExtendedDocument
2
+ # Include the validation module to get access to the validation methods
3
+ include CouchRest::Validation
4
+ # set the auto_validation before defining the properties
5
+ auto_validate!
6
+
7
+ # Set the default database to use
8
+ use_database DB
9
+
10
+ # Official Schema
11
+ property :first_name
12
+ property :last_name, :alias => :family_name
13
+ property :read_only_value, :read_only => true
14
+ property :cast_alias, :cast_as => Person, :alias => :calias
15
+
16
+
17
+ timestamps!
18
+
19
+ # Validation
20
+ validates_presence_of :first_name
21
+
22
+ end
@@ -0,0 +1,22 @@
1
+
2
+ class CatToy < Hash
3
+ include ::CouchRest::CastedModel
4
+ include ::CouchRest::Validation
5
+
6
+ property :name
7
+
8
+ validates_presence_of :name
9
+ end
10
+
11
+ class Cat < CouchRest::ExtendedDocument
12
+ include ::CouchRest::Validation
13
+
14
+ # Set the default database to use
15
+ use_database DB
16
+
17
+ property :name, :accessible => true
18
+ property :toys, [CatToy], :default => [], :accessible => true
19
+ property :favorite_toy, CatToy, :accessible => true
20
+ property :number
21
+ end
22
+
@@ -0,0 +1,25 @@
1
+ require File.join(FIXTURE_PATH, 'more', 'question')
2
+ require File.join(FIXTURE_PATH, 'more', 'person')
3
+
4
+ class Course < CouchRest::ExtendedDocument
5
+ use_database TEST_SERVER.default_database
6
+
7
+ property :title, :cast_as => 'String'
8
+ property :questions, :cast_as => ['Question']
9
+ property :professor, :cast_as => 'Person'
10
+ property :participants, :type => ['Object']
11
+ property :ends_at, :type => 'Time'
12
+ property :estimate, :type => 'Float'
13
+ property :hours, :type => 'Integer'
14
+ property :profit, :type => 'BigDecimal'
15
+ property :started_on, :type => 'Date'
16
+ property :updated_at, :type => 'DateTime'
17
+ property :active, :type => 'Boolean'
18
+ property :very_active, :type => TrueClass
19
+ property :klass, :type => 'Class'
20
+
21
+ view_by :title
22
+ view_by :title, :active
23
+ view_by :dept, :ducktype => true
24
+
25
+ end
@@ -0,0 +1,8 @@
1
+ class Event < CouchRest::ExtendedDocument
2
+ use_database DB
3
+
4
+ property :subject
5
+ property :occurs_at, :cast_as => 'Time', :init_method => 'parse'
6
+ property :end_date, :cast_as => 'Date', :init_method => 'parse'
7
+
8
+ end
@@ -0,0 +1,17 @@
1
+ class Invoice < CouchRest::ExtendedDocument
2
+ # Include the validation module to get access to the validation methods
3
+ include CouchRest::Validation
4
+
5
+ # Set the default database to use
6
+ use_database DB
7
+
8
+ # Official Schema
9
+ property :client_name
10
+ property :employee_name
11
+ property :location
12
+
13
+ # Validation
14
+ validates_presence_of :client_name, :employee_name
15
+ validates_presence_of :location, :message => "Hey stupid!, you forgot the location"
16
+
17
+ end
@@ -0,0 +1,9 @@
1
+ class Person < Hash
2
+ include ::CouchRest::CastedModel
3
+ property :pet, :cast_as => 'Cat'
4
+ property :name, :type => ['String']
5
+
6
+ def last_name
7
+ name.last
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ class Question < Hash
2
+ include ::CouchRest::CastedModel
3
+
4
+ property :q
5
+ property :a, :type => 'Object'
6
+ end
@@ -0,0 +1,12 @@
1
+ class Service < CouchRest::ExtendedDocument
2
+ # Include the validation module to get access to the validation methods
3
+ include CouchRest::Validation
4
+ auto_validate!
5
+ # Set the default database to use
6
+ use_database DB
7
+
8
+ # Official Schema
9
+ property :name, :length => 4...20
10
+ property :price, :type => 'Integer'
11
+
12
+ end
@@ -0,0 +1,22 @@
1
+ class User < CouchRest::ExtendedDocument
2
+ # Set the default database to use
3
+ use_database DB
4
+ property :name, :accessible => true
5
+ property :admin # this will be automatically protected
6
+ end
7
+
8
+ class SpecialUser < CouchRest::ExtendedDocument
9
+ # Set the default database to use
10
+ use_database DB
11
+ property :name # this will not be protected
12
+ property :admin, :protected => true
13
+ end
14
+
15
+ # There are two modes of protection
16
+ # 1) Declare accessible poperties, assume all the rest are protected
17
+ # property :name, :accessible => true
18
+ # property :admin # this will be automatically protected
19
+ #
20
+ # 2) Declare protected properties, assume all the rest are accessible
21
+ # property :name # this will not be protected
22
+ # property :admin, :protected => true
@@ -0,0 +1,3 @@
1
+ function globalLib() {
2
+ return "fixture";
3
+ };
@@ -0,0 +1,3 @@
1
+ function justThisView() {
2
+ return "fixture";
3
+ };
@@ -0,0 +1,4 @@
1
+ function(doc) {
2
+ //include-lib
3
+ emit(null, null);
4
+ };
@@ -0,0 +1,3 @@
1
+ function(doc) {
2
+ emit(null, null);
3
+ };
@@ -0,0 +1,3 @@
1
+ function(ks,vs,co) {
2
+ return vs.length;
3
+ };
data/spec/spec.opts ADDED
@@ -0,0 +1,5 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ --loadby
5
+ mtime
@@ -0,0 +1,49 @@
1
+ require "rubygems"
2
+ require "spec" # Satisfies Autotest and anyone else not using the Rake tasks
3
+
4
+ require File.join(File.dirname(__FILE__), '..','lib','couchrest_extended_document')
5
+ # check the following file to see how to use the spec'd features.
6
+
7
+ unless defined?(FIXTURE_PATH)
8
+ FIXTURE_PATH = File.join(File.dirname(__FILE__), '/fixtures')
9
+ SCRATCH_PATH = File.join(File.dirname(__FILE__), '/tmp')
10
+
11
+ COUCHHOST = "http://127.0.0.1:5984"
12
+ TESTDB = 'couchrest-test'
13
+ REPLICATIONDB = 'couchrest-test-replication'
14
+ TEST_SERVER = CouchRest.new
15
+ TEST_SERVER.default_database = TESTDB
16
+ DB = TEST_SERVER.database(TESTDB)
17
+ end
18
+
19
+ class Basic < CouchRest::ExtendedDocument
20
+ use_database TEST_SERVER.default_database
21
+ end
22
+
23
+ def reset_test_db!
24
+ DB.recreate! rescue nil
25
+ DB
26
+ end
27
+
28
+ Spec::Runner.configure do |config|
29
+ config.before(:all) { reset_test_db! }
30
+
31
+ config.after(:all) do
32
+ cr = TEST_SERVER
33
+ test_dbs = cr.databases.select { |db| db =~ /^#{TESTDB}/ }
34
+ test_dbs.each do |db|
35
+ cr.database(db).delete! rescue nil
36
+ end
37
+ end
38
+ end
39
+
40
+ def couchdb_lucene_available?
41
+ lucene_path = "http://localhost:5985/"
42
+ url = URI.parse(lucene_path)
43
+ req = Net::HTTP::Get.new(url.path)
44
+ res = Net::HTTP.new(url.host, url.port).start { |http| http.request(req) }
45
+ true
46
+ rescue Exception => e
47
+ false
48
+ end
49
+
data/utils/remap.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'couchrest'
3
+
4
+ # set the source db and map view
5
+ source = CouchRest.new("http://127.0.0.1:5984").database('source-db')
6
+ source_view = 'mydesign/view-map'
7
+
8
+ # set the target db
9
+ target = CouchRest.new("http://127.0.0.1:5984").database('target-db')
10
+
11
+
12
+ pager = CouchRest::Pager.new(source)
13
+
14
+ # pager will yield once per uniq key in the source view
15
+
16
+ pager.key_reduce(source_view, 10000) do |key, values|
17
+ # create a doc from the key and the values
18
+ example_doc = {
19
+ :key => key,
20
+ :values => values.uniq
21
+ }
22
+
23
+ target.save(example_doc)
24
+
25
+ # keep us up to date with progress
26
+ puts k if (rand > 0.9)
27
+ end
data/utils/subset.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'couchrest'
3
+
4
+ # subset.rb replicates a percentage of a database to a fresh database.
5
+ # use it to create a smaller dataset on which to prototype views.
6
+
7
+ # specify the source database
8
+ source = CouchRest.new("http://127.0.0.1:5984").database('source-db')
9
+
10
+ # specify the target database
11
+ target = CouchRest.new("http://127.0.0.1:5984").database('target-db')
12
+
13
+ # pager efficiently yields all view rows
14
+ pager = CouchRest::Pager.new(source)
15
+
16
+ pager.all_docs(1000) do |rows|
17
+ docs = rows.collect do |r|
18
+ # the percentage of docs to clone
19
+ next if rand > 0.1
20
+ doc = source.get(r['id'])
21
+ doc.delete('_rev')
22
+ doc
23
+ end.compact
24
+ puts docs.length
25
+ next if docs.empty?
26
+
27
+ puts docs.first['_id']
28
+ target.bulk_save(docs)
29
+ end
30
+
metadata ADDED
@@ -0,0 +1,200 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: couchrest_extended_document
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: true
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ - beta5
10
+ version: 1.0.0.beta5
11
+ platform: ruby
12
+ authors:
13
+ - J. Chris Anderson
14
+ - Matt Aimonetti
15
+ - Marcos Tapajos
16
+ - Will Leinweber
17
+ autorequire:
18
+ bindir: bin
19
+ cert_chain: []
20
+
21
+ date: 2010-06-15 00:00:00 -03:00
22
+ default_executable:
23
+ dependencies:
24
+ - !ruby/object:Gem::Dependency
25
+ name: couchrest
26
+ prerelease: false
27
+ requirement: &id001 !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ segments:
32
+ - 1
33
+ - 0
34
+ - 0
35
+ - beta
36
+ version: 1.0.0.beta
37
+ type: :runtime
38
+ version_requirements: *id001
39
+ - !ruby/object:Gem::Dependency
40
+ name: mime-types
41
+ prerelease: false
42
+ requirement: &id002 !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ segments:
47
+ - 1
48
+ - 15
49
+ version: "1.15"
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: activesupport
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ segments:
60
+ - 2
61
+ - 3
62
+ - 0
63
+ version: 2.3.0
64
+ type: :runtime
65
+ version_requirements: *id003
66
+ description: CouchRest::ExtendedDocument provides aditional features to the standard CouchRest::Document class such as properties, view designs, callbacks, typecasting and validations.
67
+ email: jchris@apache.org
68
+ executables: []
69
+
70
+ extensions: []
71
+
72
+ extra_rdoc_files:
73
+ - LICENSE
74
+ - README.md
75
+ - THANKS.md
76
+ files:
77
+ - LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - THANKS.md
81
+ - examples/model/example.rb
82
+ - history.txt
83
+ - lib/couchrest/casted_array.rb
84
+ - lib/couchrest/casted_model.rb
85
+ - lib/couchrest/extended_document.rb
86
+ - lib/couchrest/mixins.rb
87
+ - lib/couchrest/mixins/attribute_protection.rb
88
+ - lib/couchrest/mixins/callbacks.rb
89
+ - lib/couchrest/mixins/class_proxy.rb
90
+ - lib/couchrest/mixins/collection.rb
91
+ - lib/couchrest/mixins/design_doc.rb
92
+ - lib/couchrest/mixins/document_queries.rb
93
+ - lib/couchrest/mixins/extended_attachments.rb
94
+ - lib/couchrest/mixins/properties.rb
95
+ - lib/couchrest/mixins/validation.rb
96
+ - lib/couchrest/mixins/views.rb
97
+ - lib/couchrest/property.rb
98
+ - lib/couchrest/support/couchrest.rb
99
+ - lib/couchrest/support/rails.rb
100
+ - lib/couchrest/typecast.rb
101
+ - lib/couchrest/validation.rb
102
+ - lib/couchrest/validation/auto_validate.rb
103
+ - lib/couchrest/validation/contextual_validators.rb
104
+ - lib/couchrest/validation/validation_errors.rb
105
+ - lib/couchrest/validation/validators/absent_field_validator.rb
106
+ - lib/couchrest/validation/validators/confirmation_validator.rb
107
+ - lib/couchrest/validation/validators/format_validator.rb
108
+ - lib/couchrest/validation/validators/formats/email.rb
109
+ - lib/couchrest/validation/validators/formats/url.rb
110
+ - lib/couchrest/validation/validators/generic_validator.rb
111
+ - lib/couchrest/validation/validators/length_validator.rb
112
+ - lib/couchrest/validation/validators/method_validator.rb
113
+ - lib/couchrest/validation/validators/numeric_validator.rb
114
+ - lib/couchrest/validation/validators/required_field_validator.rb
115
+ - lib/couchrest_extended_document.rb
116
+ - spec/couchrest/attribute_protection_spec.rb
117
+ - spec/couchrest/casted_extended_doc_spec.rb
118
+ - spec/couchrest/casted_model_spec.rb
119
+ - spec/couchrest/extended_doc_attachment_spec.rb
120
+ - spec/couchrest/extended_doc_inherited_spec.rb
121
+ - spec/couchrest/extended_doc_spec.rb
122
+ - spec/couchrest/extended_doc_subclass_spec.rb
123
+ - spec/couchrest/extended_doc_view_spec.rb
124
+ - spec/couchrest/property_spec.rb
125
+ - spec/fixtures/attachments/README
126
+ - spec/fixtures/attachments/couchdb.png
127
+ - spec/fixtures/attachments/test.html
128
+ - spec/fixtures/more/article.rb
129
+ - spec/fixtures/more/card.rb
130
+ - spec/fixtures/more/cat.rb
131
+ - spec/fixtures/more/course.rb
132
+ - spec/fixtures/more/event.rb
133
+ - spec/fixtures/more/invoice.rb
134
+ - spec/fixtures/more/person.rb
135
+ - spec/fixtures/more/question.rb
136
+ - spec/fixtures/more/service.rb
137
+ - spec/fixtures/more/user.rb
138
+ - spec/fixtures/views/lib.js
139
+ - spec/fixtures/views/test_view/lib.js
140
+ - spec/fixtures/views/test_view/only-map.js
141
+ - spec/fixtures/views/test_view/test-map.js
142
+ - spec/fixtures/views/test_view/test-reduce.js
143
+ - spec/spec.opts
144
+ - spec/spec_helper.rb
145
+ - utils/remap.rb
146
+ - utils/subset.rb
147
+ has_rdoc: true
148
+ homepage: http://github.com/couchrest/couchrest_extended_document
149
+ licenses: []
150
+
151
+ post_install_message:
152
+ rdoc_options:
153
+ - --charset=UTF-8
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ segments:
161
+ - 0
162
+ version: "0"
163
+ required_rubygems_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">"
166
+ - !ruby/object:Gem::Version
167
+ segments:
168
+ - 1
169
+ - 3
170
+ - 1
171
+ version: 1.3.1
172
+ requirements: []
173
+
174
+ rubyforge_project:
175
+ rubygems_version: 1.3.6
176
+ signing_key:
177
+ specification_version: 3
178
+ summary: Extend CouchRest Document class with useful features.
179
+ test_files:
180
+ - spec/couchrest/attribute_protection_spec.rb
181
+ - spec/couchrest/casted_extended_doc_spec.rb
182
+ - spec/couchrest/casted_model_spec.rb
183
+ - spec/couchrest/extended_doc_attachment_spec.rb
184
+ - spec/couchrest/extended_doc_inherited_spec.rb
185
+ - spec/couchrest/extended_doc_spec.rb
186
+ - spec/couchrest/extended_doc_subclass_spec.rb
187
+ - spec/couchrest/extended_doc_view_spec.rb
188
+ - spec/couchrest/property_spec.rb
189
+ - spec/fixtures/more/article.rb
190
+ - spec/fixtures/more/card.rb
191
+ - spec/fixtures/more/cat.rb
192
+ - spec/fixtures/more/course.rb
193
+ - spec/fixtures/more/event.rb
194
+ - spec/fixtures/more/invoice.rb
195
+ - spec/fixtures/more/person.rb
196
+ - spec/fixtures/more/question.rb
197
+ - spec/fixtures/more/service.rb
198
+ - spec/fixtures/more/user.rb
199
+ - spec/spec_helper.rb
200
+ - examples/model/example.rb