samlown-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 (70) hide show
  1. data/LICENSE +176 -0
  2. data/README.md +46 -0
  3. data/Rakefile +67 -0
  4. data/THANKS.md +19 -0
  5. data/examples/model/example.rb +144 -0
  6. data/history.txt +130 -0
  7. data/lib/couchrest/casted_array.rb +25 -0
  8. data/lib/couchrest/casted_model.rb +59 -0
  9. data/lib/couchrest/extended_document.rb +318 -0
  10. data/lib/couchrest/mixins.rb +12 -0
  11. data/lib/couchrest/mixins/attribute_protection.rb +74 -0
  12. data/lib/couchrest/mixins/callbacks.rb +532 -0
  13. data/lib/couchrest/mixins/class_proxy.rb +120 -0
  14. data/lib/couchrest/mixins/collection.rb +260 -0
  15. data/lib/couchrest/mixins/design_doc.rb +121 -0
  16. data/lib/couchrest/mixins/document_queries.rb +80 -0
  17. data/lib/couchrest/mixins/extended_attachments.rb +70 -0
  18. data/lib/couchrest/mixins/properties.rb +154 -0
  19. data/lib/couchrest/mixins/validation.rb +245 -0
  20. data/lib/couchrest/mixins/views.rb +148 -0
  21. data/lib/couchrest/monkeypatches.rb +5 -0
  22. data/lib/couchrest/property.rb +50 -0
  23. data/lib/couchrest/support/couchrest.rb +56 -0
  24. data/lib/couchrest/support/rails.rb +42 -0
  25. data/lib/couchrest/typecast.rb +175 -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/spec/couchrest/attribute_protection_spec.rb +150 -0
  40. data/spec/couchrest/casted_extended_doc_spec.rb +79 -0
  41. data/spec/couchrest/casted_model_spec.rb +406 -0
  42. data/spec/couchrest/extended_doc_attachment_spec.rb +135 -0
  43. data/spec/couchrest/extended_doc_inherited_spec.rb +40 -0
  44. data/spec/couchrest/extended_doc_spec.rb +824 -0
  45. data/spec/couchrest/extended_doc_subclass_spec.rb +99 -0
  46. data/spec/couchrest/extended_doc_view_spec.rb +473 -0
  47. data/spec/couchrest/property_spec.rb +628 -0
  48. data/spec/fixtures/attachments/README +3 -0
  49. data/spec/fixtures/attachments/couchdb.png +0 -0
  50. data/spec/fixtures/attachments/test.html +11 -0
  51. data/spec/fixtures/more/article.rb +35 -0
  52. data/spec/fixtures/more/card.rb +22 -0
  53. data/spec/fixtures/more/cat.rb +22 -0
  54. data/spec/fixtures/more/course.rb +22 -0
  55. data/spec/fixtures/more/event.rb +8 -0
  56. data/spec/fixtures/more/invoice.rb +17 -0
  57. data/spec/fixtures/more/person.rb +9 -0
  58. data/spec/fixtures/more/question.rb +6 -0
  59. data/spec/fixtures/more/service.rb +12 -0
  60. data/spec/fixtures/more/user.rb +22 -0
  61. data/spec/fixtures/views/lib.js +3 -0
  62. data/spec/fixtures/views/test_view/lib.js +3 -0
  63. data/spec/fixtures/views/test_view/only-map.js +4 -0
  64. data/spec/fixtures/views/test_view/test-map.js +3 -0
  65. data/spec/fixtures/views/test_view/test-reduce.js +3 -0
  66. data/spec/spec.opts +5 -0
  67. data/spec/spec_helper.rb +49 -0
  68. data/utils/remap.rb +27 -0
  69. data/utils/subset.rb +30 -0
  70. metadata +181 -0
@@ -0,0 +1,3 @@
1
+ This is an example README file.
2
+
3
+ More of the README, whee.
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Test</title>
5
+ </head>
6
+ <body>
7
+ <p>
8
+ Test
9
+ </p>
10
+ </body>
11
+ </html>
@@ -0,0 +1,35 @@
1
+ class Article < CouchRest::ExtendedDocument
2
+ use_database DB
3
+ unique_id :slug
4
+
5
+ provides_collection :article_details, 'Article', 'by_date', :descending => true, :include_docs => true
6
+ view_by :date, :descending => true
7
+ view_by :user_id, :date
8
+
9
+ view_by :tags,
10
+ :map =>
11
+ "function(doc) {
12
+ if (doc['couchrest-type'] == 'Article' && doc.tags) {
13
+ doc.tags.forEach(function(tag){
14
+ emit(tag, 1);
15
+ });
16
+ }
17
+ }",
18
+ :reduce =>
19
+ "function(keys, values, rereduce) {
20
+ return sum(values);
21
+ }"
22
+
23
+ property :date, :type => 'Date'
24
+ property :slug, :read_only => true
25
+ property :title
26
+ property :tags, :type => ['String']
27
+
28
+ timestamps!
29
+
30
+ before_save :generate_slug_from_title
31
+
32
+ def generate_slug_from_title
33
+ self['slug'] = title.downcase.gsub(/[^a-z0-9]/,'-').squeeze('-').gsub(/^\-|\-$/,'') if new?
34
+ end
35
+ end
@@ -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, :cast_as => [CatToy], :default => [], :accessible => true
19
+ property :favorite_toy, :cast_as => CatToy, :accessible => true
20
+ property :number
21
+ end
22
+
@@ -0,0 +1,22 @@
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 :klass, :type => 'Class'
19
+
20
+ view_by :title
21
+ view_by :dept, :ducktype => true
22
+ 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
+ };
@@ -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
+
@@ -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
@@ -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,181 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: samlown-couchrest_extended_document
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - J. Chris Anderson
13
+ - Matt Aimonetti
14
+ - Marcos Tapajos
15
+ - Will Leinweber
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2010-05-10 00:00:00 +02:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ name: samlown-couchrest
25
+ prerelease: false
26
+ requirement: &id001 !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ version: 1.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: mime-types
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ segments:
45
+ - 1
46
+ - 15
47
+ version: "1.15"
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: CouchRest::ExtendedDocument provides aditional features to the standard CouchRest::Document class such as properties, view designs, callbacks, typecasting and validations.
51
+ email: jchris@apache.org
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files:
57
+ - LICENSE
58
+ - README.md
59
+ - THANKS.md
60
+ files:
61
+ - LICENSE
62
+ - README.md
63
+ - Rakefile
64
+ - THANKS.md
65
+ - examples/model/example.rb
66
+ - history.txt
67
+ - lib/couchrest/casted_array.rb
68
+ - lib/couchrest/casted_model.rb
69
+ - lib/couchrest/extended_document.rb
70
+ - lib/couchrest/mixins.rb
71
+ - lib/couchrest/mixins/attribute_protection.rb
72
+ - lib/couchrest/mixins/callbacks.rb
73
+ - lib/couchrest/mixins/class_proxy.rb
74
+ - lib/couchrest/mixins/collection.rb
75
+ - lib/couchrest/mixins/design_doc.rb
76
+ - lib/couchrest/mixins/document_queries.rb
77
+ - lib/couchrest/mixins/extended_attachments.rb
78
+ - lib/couchrest/mixins/properties.rb
79
+ - lib/couchrest/mixins/validation.rb
80
+ - lib/couchrest/mixins/views.rb
81
+ - lib/couchrest/monkeypatches.rb
82
+ - lib/couchrest/property.rb
83
+ - lib/couchrest/support/couchrest.rb
84
+ - lib/couchrest/support/rails.rb
85
+ - lib/couchrest/typecast.rb
86
+ - lib/couchrest/validation/auto_validate.rb
87
+ - lib/couchrest/validation/contextual_validators.rb
88
+ - lib/couchrest/validation/validation_errors.rb
89
+ - lib/couchrest/validation/validators/absent_field_validator.rb
90
+ - lib/couchrest/validation/validators/confirmation_validator.rb
91
+ - lib/couchrest/validation/validators/format_validator.rb
92
+ - lib/couchrest/validation/validators/formats/email.rb
93
+ - lib/couchrest/validation/validators/formats/url.rb
94
+ - lib/couchrest/validation/validators/generic_validator.rb
95
+ - lib/couchrest/validation/validators/length_validator.rb
96
+ - lib/couchrest/validation/validators/method_validator.rb
97
+ - lib/couchrest/validation/validators/numeric_validator.rb
98
+ - lib/couchrest/validation/validators/required_field_validator.rb
99
+ - spec/couchrest/attribute_protection_spec.rb
100
+ - spec/couchrest/casted_extended_doc_spec.rb
101
+ - spec/couchrest/casted_model_spec.rb
102
+ - spec/couchrest/extended_doc_attachment_spec.rb
103
+ - spec/couchrest/extended_doc_inherited_spec.rb
104
+ - spec/couchrest/extended_doc_spec.rb
105
+ - spec/couchrest/extended_doc_subclass_spec.rb
106
+ - spec/couchrest/extended_doc_view_spec.rb
107
+ - spec/couchrest/property_spec.rb
108
+ - spec/fixtures/attachments/README
109
+ - spec/fixtures/attachments/couchdb.png
110
+ - spec/fixtures/attachments/test.html
111
+ - spec/fixtures/more/article.rb
112
+ - spec/fixtures/more/card.rb
113
+ - spec/fixtures/more/cat.rb
114
+ - spec/fixtures/more/course.rb
115
+ - spec/fixtures/more/event.rb
116
+ - spec/fixtures/more/invoice.rb
117
+ - spec/fixtures/more/person.rb
118
+ - spec/fixtures/more/question.rb
119
+ - spec/fixtures/more/service.rb
120
+ - spec/fixtures/more/user.rb
121
+ - spec/fixtures/views/lib.js
122
+ - spec/fixtures/views/test_view/lib.js
123
+ - spec/fixtures/views/test_view/only-map.js
124
+ - spec/fixtures/views/test_view/test-map.js
125
+ - spec/fixtures/views/test_view/test-reduce.js
126
+ - spec/spec.opts
127
+ - spec/spec_helper.rb
128
+ - utils/remap.rb
129
+ - utils/subset.rb
130
+ has_rdoc: true
131
+ homepage: http://github.com/samlown/couchrest_extended_document
132
+ licenses: []
133
+
134
+ post_install_message:
135
+ rdoc_options:
136
+ - --charset=UTF-8
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ segments:
151
+ - 0
152
+ version: "0"
153
+ requirements: []
154
+
155
+ rubyforge_project:
156
+ rubygems_version: 1.3.6
157
+ signing_key:
158
+ specification_version: 3
159
+ summary: Extend CouchRest Document class with useful features.
160
+ test_files:
161
+ - spec/spec_helper.rb
162
+ - spec/couchrest/extended_doc_inherited_spec.rb
163
+ - spec/couchrest/extended_doc_attachment_spec.rb
164
+ - spec/couchrest/attribute_protection_spec.rb
165
+ - spec/couchrest/extended_doc_subclass_spec.rb
166
+ - spec/couchrest/extended_doc_spec.rb
167
+ - spec/couchrest/property_spec.rb
168
+ - spec/couchrest/casted_model_spec.rb
169
+ - spec/couchrest/extended_doc_view_spec.rb
170
+ - spec/couchrest/casted_extended_doc_spec.rb
171
+ - spec/fixtures/more/invoice.rb
172
+ - spec/fixtures/more/service.rb
173
+ - spec/fixtures/more/course.rb
174
+ - spec/fixtures/more/article.rb
175
+ - spec/fixtures/more/user.rb
176
+ - spec/fixtures/more/person.rb
177
+ - spec/fixtures/more/cat.rb
178
+ - spec/fixtures/more/event.rb
179
+ - spec/fixtures/more/question.rb
180
+ - spec/fixtures/more/card.rb
181
+ - examples/model/example.rb