openlogic-couchrest_model 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. data/.gitignore +11 -0
  2. data/.rspec +4 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +176 -0
  5. data/README.md +137 -0
  6. data/Rakefile +38 -0
  7. data/THANKS.md +21 -0
  8. data/VERSION +1 -0
  9. data/benchmarks/dirty.rb +118 -0
  10. data/couchrest_model.gemspec +36 -0
  11. data/history.md +309 -0
  12. data/init.rb +1 -0
  13. data/lib/couchrest/model.rb +10 -0
  14. data/lib/couchrest/model/associations.rb +231 -0
  15. data/lib/couchrest/model/base.rb +129 -0
  16. data/lib/couchrest/model/callbacks.rb +28 -0
  17. data/lib/couchrest/model/casted_array.rb +83 -0
  18. data/lib/couchrest/model/casted_by.rb +33 -0
  19. data/lib/couchrest/model/casted_hash.rb +84 -0
  20. data/lib/couchrest/model/class_proxy.rb +135 -0
  21. data/lib/couchrest/model/collection.rb +273 -0
  22. data/lib/couchrest/model/configuration.rb +67 -0
  23. data/lib/couchrest/model/connection.rb +70 -0
  24. data/lib/couchrest/model/core_extensions/hash.rb +9 -0
  25. data/lib/couchrest/model/core_extensions/time_parsing.rb +66 -0
  26. data/lib/couchrest/model/design_doc.rb +128 -0
  27. data/lib/couchrest/model/designs.rb +91 -0
  28. data/lib/couchrest/model/designs/view.rb +513 -0
  29. data/lib/couchrest/model/dirty.rb +39 -0
  30. data/lib/couchrest/model/document_queries.rb +99 -0
  31. data/lib/couchrest/model/embeddable.rb +78 -0
  32. data/lib/couchrest/model/errors.rb +25 -0
  33. data/lib/couchrest/model/extended_attachments.rb +83 -0
  34. data/lib/couchrest/model/persistence.rb +178 -0
  35. data/lib/couchrest/model/properties.rb +228 -0
  36. data/lib/couchrest/model/property.rb +114 -0
  37. data/lib/couchrest/model/property_protection.rb +71 -0
  38. data/lib/couchrest/model/proxyable.rb +183 -0
  39. data/lib/couchrest/model/support/couchrest_database.rb +13 -0
  40. data/lib/couchrest/model/support/couchrest_design.rb +33 -0
  41. data/lib/couchrest/model/typecast.rb +154 -0
  42. data/lib/couchrest/model/validations.rb +80 -0
  43. data/lib/couchrest/model/validations/casted_model.rb +16 -0
  44. data/lib/couchrest/model/validations/locale/en.yml +5 -0
  45. data/lib/couchrest/model/validations/uniqueness.rb +69 -0
  46. data/lib/couchrest/model/views.rb +151 -0
  47. data/lib/couchrest/railtie.rb +24 -0
  48. data/lib/couchrest_model.rb +66 -0
  49. data/lib/rails/generators/couchrest_model.rb +16 -0
  50. data/lib/rails/generators/couchrest_model/config/config_generator.rb +18 -0
  51. data/lib/rails/generators/couchrest_model/config/templates/couchdb.yml +21 -0
  52. data/lib/rails/generators/couchrest_model/model/model_generator.rb +27 -0
  53. data/lib/rails/generators/couchrest_model/model/templates/model.rb +2 -0
  54. data/spec/.gitignore +1 -0
  55. data/spec/fixtures/attachments/README +3 -0
  56. data/spec/fixtures/attachments/couchdb.png +0 -0
  57. data/spec/fixtures/attachments/test.html +11 -0
  58. data/spec/fixtures/config/couchdb.yml +10 -0
  59. data/spec/fixtures/models/article.rb +36 -0
  60. data/spec/fixtures/models/base.rb +164 -0
  61. data/spec/fixtures/models/card.rb +19 -0
  62. data/spec/fixtures/models/cat.rb +23 -0
  63. data/spec/fixtures/models/client.rb +6 -0
  64. data/spec/fixtures/models/course.rb +27 -0
  65. data/spec/fixtures/models/event.rb +8 -0
  66. data/spec/fixtures/models/invoice.rb +14 -0
  67. data/spec/fixtures/models/key_chain.rb +5 -0
  68. data/spec/fixtures/models/membership.rb +4 -0
  69. data/spec/fixtures/models/person.rb +11 -0
  70. data/spec/fixtures/models/project.rb +6 -0
  71. data/spec/fixtures/models/question.rb +7 -0
  72. data/spec/fixtures/models/sale_entry.rb +9 -0
  73. data/spec/fixtures/models/sale_invoice.rb +14 -0
  74. data/spec/fixtures/models/service.rb +10 -0
  75. data/spec/fixtures/models/user.rb +22 -0
  76. data/spec/fixtures/views/lib.js +3 -0
  77. data/spec/fixtures/views/test_view/lib.js +3 -0
  78. data/spec/fixtures/views/test_view/only-map.js +4 -0
  79. data/spec/fixtures/views/test_view/test-map.js +3 -0
  80. data/spec/fixtures/views/test_view/test-reduce.js +3 -0
  81. data/spec/functional/validations_spec.rb +8 -0
  82. data/spec/spec_helper.rb +60 -0
  83. data/spec/unit/active_model_lint_spec.rb +30 -0
  84. data/spec/unit/assocations_spec.rb +242 -0
  85. data/spec/unit/attachment_spec.rb +176 -0
  86. data/spec/unit/base_spec.rb +537 -0
  87. data/spec/unit/casted_spec.rb +72 -0
  88. data/spec/unit/class_proxy_spec.rb +167 -0
  89. data/spec/unit/collection_spec.rb +86 -0
  90. data/spec/unit/configuration_spec.rb +77 -0
  91. data/spec/unit/connection_spec.rb +148 -0
  92. data/spec/unit/core_extensions/time_parsing.rb +77 -0
  93. data/spec/unit/design_doc_spec.rb +241 -0
  94. data/spec/unit/designs/view_spec.rb +831 -0
  95. data/spec/unit/designs_spec.rb +134 -0
  96. data/spec/unit/dirty_spec.rb +436 -0
  97. data/spec/unit/embeddable_spec.rb +498 -0
  98. data/spec/unit/inherited_spec.rb +33 -0
  99. data/spec/unit/persistence_spec.rb +481 -0
  100. data/spec/unit/property_protection_spec.rb +192 -0
  101. data/spec/unit/property_spec.rb +481 -0
  102. data/spec/unit/proxyable_spec.rb +376 -0
  103. data/spec/unit/subclass_spec.rb +85 -0
  104. data/spec/unit/typecast_spec.rb +521 -0
  105. data/spec/unit/validations_spec.rb +140 -0
  106. data/spec/unit/view_spec.rb +367 -0
  107. metadata +301 -0
@@ -0,0 +1,11 @@
1
+ .DS_Store
2
+ html/*
3
+ pkg
4
+ *.swp
5
+ .rvmrc*
6
+ .bundle
7
+ couchdb.std*
8
+ *.*~
9
+ Gemfile.lock
10
+ spec.out
11
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format
3
+ progress
4
+ mtime
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+
2
+ source :rubygems
3
+ gemspec
4
+
data/LICENSE ADDED
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,137 @@
1
+ # CouchRest Model: CouchDB, close to shiny metal with rounded edges
2
+
3
+ CouchRest Models adds additional functionality to the standard CouchRest Document class such as
4
+ setting properties, callbacks, typecasting, and validations.
5
+
6
+ ## Documentation
7
+
8
+ Please visit the documentation project at [http://www.couchrest.info](http://www.couchrest.info). You're [contributions](https://github.com/couchrest/couchrest.github.com) to the documentation would be greatly appreciated!
9
+
10
+ General API: [http://rdoc.info/projects/couchrest/couchrest_model](http://rdoc.info/projects/couchrest/couchrest_model)
11
+
12
+ See the [update history](https://github.com/couchrest/couchrest_model/blob/master/history.md) for an up to date list of all the changes we've been working on recently.
13
+
14
+ ## Notes
15
+
16
+ Originally called ExtendedDocument, the new Model structure uses ActiveModel, part of Rails 3,
17
+ for validations and callbacks.
18
+
19
+ If your project is still running Rails 2.3, you'll have to continue using ExtendedDocument as
20
+ it is not possible to load ActiveModel into programs that do not use ActiveSupport 3.0.
21
+
22
+ CouchRest Model is only properly tested on CouchDB version 1.0 or newer.
23
+
24
+ *WARNING:* As of April 2011 and the release of version 1.1.0, the default model type key is 'type' instead of 'couchrest-type'. Simply updating your project will not work unless you migrate your data or set the configuration option in your initializers:
25
+
26
+ CouchRest::Model::Base.configure do |config|
27
+ config.model_type_key = 'couchrest-type'
28
+ end
29
+
30
+ This is because CouchRest Model's are not couchrest specific and may be used in any other systems such as Javascript, the model type should reflect this. Also, we're all used to `type` being a reserved word in ActiveRecord.
31
+
32
+ ## Install
33
+
34
+ ### Gem
35
+
36
+ $ sudo gem install couchrest_model
37
+
38
+ ### Bundler
39
+
40
+ If you're using bundler, define a line similar to the following in your project's Gemfile:
41
+
42
+ gem 'couchrest_model'
43
+
44
+ ### Configuration
45
+
46
+ CouchRest Model is configured to work out the box with no configuration as long as your CouchDB instance is running on the default port (5984) on localhost. The default name of the database is either the name of your application as provided by the `Rails.application.class.to_s` call (with /application removed) or just 'couchrest' if none is available.
47
+
48
+ The library will try to detect a configuration file at `config/couchdb.yml` from the Rails root or `Dir.pwd`. Here you can configuration your database connection in a Rails-like way:
49
+
50
+ development:
51
+ protocol: 'https'
52
+ host: sample.cloudant.com
53
+ port: 443
54
+ prefix: project
55
+ suffix: test
56
+ username: test
57
+ password: user
58
+
59
+ Note that the name of the database is either just the prefix and suffix combined or the prefix plus any text you specifify using `use_database` method in your models with the suffix on the end.
60
+
61
+ The example config above for example would use a database called "project_test". Heres an example using the `use_database` call:
62
+
63
+ class Project < CouchRest::Model::Base
64
+ use_database 'sample'
65
+ end
66
+
67
+ # The database object would be provided as:
68
+ Project.database #=> "https://test:user@sample.cloudant.com:443/project_sample_test"
69
+
70
+
71
+ ## Generators
72
+
73
+ ### Configuration
74
+
75
+ $ rails generate couchrest_model:config
76
+
77
+ ### Model
78
+
79
+ $ rails generate model person --orm=couchrest_model
80
+
81
+ ## General Usage
82
+
83
+ require 'couchrest_model'
84
+
85
+ class Cat < CouchRest::Model::Base
86
+
87
+ property :name, String
88
+ property :lives, Integer, :default => 9
89
+
90
+ property :nicknames, [String]
91
+
92
+ timestamps!
93
+
94
+ view_by :name
95
+
96
+ end
97
+
98
+ @cat = Cat.new(:name => 'Felix', :nicknames => ['so cute', 'sweet kitty'])
99
+
100
+ @cat.new? # true
101
+ @cat.save
102
+
103
+ @cat['name'] # "Felix"
104
+
105
+ @cat.nicknames << 'getoffdamntable'
106
+
107
+ @cat = Cat.new
108
+ @cat.update_attributes(:name => 'Felix', :random_text => 'feline')
109
+ @cat.new? # false
110
+ @cat.random_text # Raises error!
111
+
112
+ ## Development
113
+
114
+ ### Preparations
115
+
116
+ CouchRest Model now comes with a Gemfile to help with development. If you want to make changes to the code, download a copy then run:
117
+
118
+ bundle install
119
+
120
+ That should set everything up for `rake spec` to be run correctly. Update the couchrest_model.gemspec if your alterations
121
+ use different gems.
122
+
123
+ ### Testing
124
+
125
+ The most complete documentation is the spec/ directory. To validate your CouchRest install, from the project root directory run `bundle install` to ensure all the development dependencies are available and then `rspec spec` or `bundle exec rspec spec`.
126
+
127
+ We will not accept pull requests to the project without sufficient tests.
128
+
129
+ ## Contact
130
+
131
+ Please post bugs, suggestions and patches to the bug tracker at [http://github.com/couchrest/couchrest_model/issues](http://github.com/couchrest/couchrest_model/issues).
132
+
133
+ Follow us on Twitter: [http://twitter.com/couchrest](http://twitter.com/couchrest)
134
+
135
+ Also, check [http://twitter.com/#search?q=%23couchrest](http://twitter.com/#search?q=%23couchrest)
136
+
137
+
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'rspec/core/rake_task'
4
+ require "rake/rdoctask"
5
+
6
+ Bundler::GemHelper.install_tasks
7
+
8
+ desc "Run all specs"
9
+ RSpec::Core::RakeTask.new(:spec) do |spec|
10
+ spec.rspec_opts = ["--color"]
11
+ spec.pattern = 'spec/**/*_spec.rb'
12
+ end
13
+
14
+ desc "Print specdocs"
15
+ RSpec::Core::RakeTask.new(:doc) do |spec|
16
+ spec.rspec_opts = ["--format", "specdoc"]
17
+ spec.pattern = 'spec/*_spec.rb'
18
+ end
19
+
20
+ desc "Generate the rdoc"
21
+ Rake::RDocTask.new do |rdoc|
22
+ files = ["README.rdoc", "LICENSE", "lib/**/*.rb"]
23
+ rdoc.rdoc_files.add(files)
24
+ rdoc.main = "README.rdoc"
25
+ rdoc.title = "CouchRest: Ruby CouchDB, close to the metal"
26
+ end
27
+
28
+ desc "Run the rspec"
29
+ task :default => :spec
30
+
31
+ module Rake
32
+ def self.remove_task(task_name)
33
+ Rake.application.instance_variable_get('@tasks').delete(task_name.to_s)
34
+ end
35
+ end
36
+
37
+ Rake.remove_task("github:release")
38
+ Rake.remove_task("release")
@@ -0,0 +1,21 @@
1
+ CouchRest THANKS
2
+ =====================
3
+
4
+ CouchRest was originally developed by J. Chris Anderson <jchris@grabb.it>
5
+ and a number of other contributors. Many people further contributed to
6
+ CouchRest by reporting problems, suggesting various improvements or submitting
7
+ changes. A list of these people is included below.
8
+
9
+ * [Matt Aimonetti](http://merbist.com/about/)
10
+ * [Greg Borenstein](http://ideasfordozens.com)
11
+ * [Geoffrey Grosenbach](http://nubyonrails.com/)
12
+ * [Jonathan S. Katz](http://github.com/jkatz)
13
+ * [Matt Lyon](http://mattly.tumblr.com/)
14
+ * Simon Rozet (simon /at/ rozet /dot/ name)
15
+ * [Marcos Tapajós](http://tapajos.me)
16
+ * [Sam Lown](http://github.com/samlown)
17
+ * [Will Leinweber](http://github.com/will)
18
+
19
+ Patches are welcome. The primary source for this software project is [on Github](http://github.com/couchrest/couchrest)
20
+
21
+ A lot of people have active forks - thank you all - even the patches I don't end up using are helpful.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,118 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'benchmark'
5
+
6
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
7
+ require 'couchrest_model'
8
+
9
+ class BenchmarkCasted < Hash
10
+ include CouchRest::Model::CastedModel
11
+
12
+ property :name
13
+ end
14
+
15
+ class BenchmarkModel < CouchRest::Model::Base
16
+ use_database CouchRest.database!(ENV['BENCHMARK_DB'] || "http://localhost:5984/test")
17
+
18
+ property :string, String
19
+ property :number, Integer
20
+ property :casted, BenchmarkCasted
21
+ property :casted_list, [BenchmarkCasted]
22
+ end
23
+
24
+ # set dirty configuration, return previous configuration setting
25
+ def set_dirty(value)
26
+ orig = nil
27
+ CouchRest::Model::Base.configure do |config|
28
+ orig = config.use_dirty
29
+ config.use_dirty = value
30
+ end
31
+ BenchmarkModel.instance_eval do
32
+ self.use_dirty = value
33
+ end
34
+ orig
35
+ end
36
+
37
+ def supports_dirty?
38
+ CouchRest::Model::Base.respond_to?(:use_dirty)
39
+ end
40
+
41
+ def run_benchmark
42
+ n = 50000 # property operation count
43
+ db_n = 1000 # database operation count
44
+ b = BenchmarkModel.new
45
+
46
+ Benchmark.bm(30) do |x|
47
+
48
+ # property assigning
49
+
50
+ x.report("assign string:") do
51
+ n.times { b.string = "test" }
52
+ end
53
+
54
+ next if ENV["BENCHMARK_STRING"]
55
+
56
+ x.report("assign integer:") do
57
+ n.times { b.number = 1 }
58
+ end
59
+
60
+ x.report("assign casted hash:") do
61
+ n.times { b.casted = { 'name' => 'test' } }
62
+ end
63
+
64
+ x.report("assign casted hash list:") do
65
+ n.times { b.casted_list = [{ 'name' => 'test' }] }
66
+ end
67
+
68
+ # property reading
69
+
70
+ x.report("read string") do
71
+ n.times { b.string }
72
+ end
73
+
74
+ x.report("read integer") do
75
+ n.times { b.number }
76
+ end
77
+
78
+ x.report("read casted hash") do
79
+ n.times { b.casted }
80
+ end
81
+
82
+ x.report("read casted hash list") do
83
+ n.times { b.casted_list }
84
+ end
85
+
86
+ if ENV['BENCHMARK_DB']
87
+ # db writing
88
+ x.report("write changed record to db") do
89
+ db_n.times { |i| b.string = "test#{i}"; b.save }
90
+ end
91
+
92
+ x.report("write unchanged record to db") do
93
+ db_n.times { b.save }
94
+ end
95
+
96
+ # db reading
97
+ x.report("read record from db") do
98
+ db_n.times { BenchmarkModel.find(b.id) }
99
+ end
100
+
101
+ end
102
+
103
+ end
104
+ end
105
+
106
+ begin
107
+ if supports_dirty?
108
+ if !ENV['BENCHMARK_DIRTY_OFF']
109
+ set_dirty(true)
110
+ puts "with use_dirty true"
111
+ run_benchmark
112
+ end
113
+ set_dirty(false)
114
+ puts "\nwith use_dirty false"
115
+ end
116
+
117
+ run_benchmark
118
+ end