couchrest_model 1.0.0.beta8 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. data/.gitignore +9 -0
  2. data/{spec/spec.opts → .rspec} +0 -1
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +77 -0
  5. data/README.md +144 -57
  6. data/Rakefile +12 -43
  7. data/VERSION +1 -0
  8. data/couchrest_model.gemspec +35 -0
  9. data/history.txt +23 -1
  10. data/init.rb +1 -0
  11. data/lib/couchrest/model/associations.rb +17 -1
  12. data/lib/couchrest/model/base.rb +5 -5
  13. data/lib/couchrest/model/casted_model.rb +2 -2
  14. data/lib/couchrest/model/class_proxy.rb +6 -0
  15. data/lib/couchrest/model/collection.rb +3 -0
  16. data/lib/couchrest/model/configuration.rb +51 -0
  17. data/lib/couchrest/model/design_doc.rb +2 -5
  18. data/lib/couchrest/model/document_queries.rb +20 -3
  19. data/lib/couchrest/model/persistence.rb +15 -1
  20. data/lib/couchrest/model/properties.rb +97 -23
  21. data/lib/couchrest/model/property.rb +5 -4
  22. data/lib/couchrest/model/property_protection.rb +71 -0
  23. data/lib/couchrest/model/typecast.rb +12 -7
  24. data/lib/couchrest/model/view.rb +190 -0
  25. data/lib/couchrest/model/views.rb +3 -3
  26. data/lib/couchrest/model.rb +1 -1
  27. data/lib/couchrest/railtie.rb +3 -2
  28. data/lib/couchrest_model.rb +7 -14
  29. data/lib/rails/generators/couchrest_model/model/model_generator.rb +2 -1
  30. data/spec/.gitignore +1 -0
  31. data/spec/couchrest/base_spec.rb +3 -3
  32. data/spec/couchrest/casted_model_spec.rb +63 -49
  33. data/spec/couchrest/class_proxy_spec.rb +6 -0
  34. data/spec/couchrest/configuration_spec.rb +78 -0
  35. data/spec/couchrest/persistence_spec.rb +10 -4
  36. data/spec/couchrest/{attribute_protection_spec.rb → property_protection_spec.rb} +29 -2
  37. data/spec/couchrest/property_spec.rb +61 -0
  38. data/spec/couchrest/subclass_spec.rb +2 -2
  39. data/spec/couchrest/view_spec.rb +6 -0
  40. data/spec/fixtures/more/article.rb +1 -1
  41. data/spec/spec_helper.rb +4 -3
  42. metadata +96 -32
  43. data/lib/couchrest/model/attribute_protection.rb +0 -74
  44. data/lib/couchrest/model/attributes.rb +0 -75
@@ -92,8 +92,8 @@ describe "Subclassing a Model" do
92
92
  OnlineCourse.design_doc['views'].keys.should_not include('by_title')
93
93
  end
94
94
 
95
- it "should have an all view with a guard clause for couchrest-type == subclass name in the map function" do
96
- OnlineCourse.design_doc['views']['all']['map'].should =~ /if \(doc\['couchrest-type'\] == 'OnlineCourse'\)/
95
+ it "should have an all view with a guard clause for model == subclass name in the map function" do
96
+ OnlineCourse.design_doc['views']['all']['map'].should =~ /if \(doc\['#{OnlineCourse.model_type_key}'\] == 'OnlineCourse'\)/
97
97
  end
98
98
  end
99
99
 
@@ -273,6 +273,12 @@ describe "Model views" do
273
273
  u = Unattached.first :database=>@db
274
274
  u.title.should =~ /\A...\z/
275
275
  end
276
+
277
+ it "should get last" do
278
+ u = Unattached.last :database=>@db
279
+ u.title.should == "aaa"
280
+ end
281
+
276
282
  it "should barf on all_design_doc_versions if no database given" do
277
283
  lambda{Unattached.all_design_doc_versions}.should raise_error
278
284
  end
@@ -9,7 +9,7 @@ class Article < CouchRest::Model::Base
9
9
  view_by :tags,
10
10
  :map =>
11
11
  "function(doc) {
12
- if (doc['couchrest-type'] == 'Article' && doc.tags) {
12
+ if (doc['#{model_type_key}'] == 'Article' && doc.tags) {
13
13
  doc.tags.forEach(function(tag){
14
14
  emit(tag, 1);
15
15
  });
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
+ require "bundler/setup"
1
2
  require "rubygems"
2
- require "spec" # Satisfies Autotest and anyone else not using the Rake tasks
3
+ require "rspec" # Satisfies Autotest and anyone else not using the Rake tasks
3
4
 
4
5
  require File.join(File.dirname(__FILE__), '..','lib','couchrest_model')
5
6
  # check the following file to see how to use the spec'd features.
@@ -10,7 +11,7 @@ unless defined?(FIXTURE_PATH)
10
11
 
11
12
  COUCHHOST = "http://127.0.0.1:5984"
12
13
  TESTDB = 'couchrest-model-test'
13
- TEST_SERVER = CouchRest.new
14
+ TEST_SERVER = CouchRest.new COUCHHOST
14
15
  TEST_SERVER.default_database = TESTDB
15
16
  DB = TEST_SERVER.database(TESTDB)
16
17
  end
@@ -24,7 +25,7 @@ def reset_test_db!
24
25
  DB
25
26
  end
26
27
 
27
- Spec::Runner.configure do |config|
28
+ RSpec.configure do |config|
28
29
  config.before(:all) { reset_test_db! }
29
30
 
30
31
  config.after(:all) do
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchrest_model
3
3
  version: !ruby/object:Gem::Version
4
- hash: -1848230060
5
- prerelease: true
4
+ hash: 23
5
+ prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
9
  - 0
10
- - beta8
11
- version: 1.0.0.beta8
10
+ version: 1.0.0
12
11
  platform: ruby
13
12
  authors:
14
13
  - J. Chris Anderson
@@ -20,7 +19,7 @@ autorequire:
20
19
  bindir: bin
21
20
  cert_chain: []
22
21
 
23
- date: 2010-08-22 00:00:00 -03:00
22
+ date: 2011-01-16 00:00:00 -02:00
24
23
  default_executable:
25
24
  dependencies:
26
25
  - !ruby/object:Gem::Dependency
@@ -31,12 +30,12 @@ dependencies:
31
30
  requirements:
32
31
  - - ~>
33
32
  - !ruby/object:Gem::Version
34
- hash: 23
33
+ hash: 21
35
34
  segments:
36
35
  - 1
37
36
  - 0
38
- - 0
39
- version: 1.0.0
37
+ - 1
38
+ version: 1.0.1
40
39
  type: :runtime
41
40
  version_requirements: *id001
42
41
  - !ruby/object:Gem::Dependency
@@ -55,54 +54,101 @@ dependencies:
55
54
  type: :runtime
56
55
  version_requirements: *id002
57
56
  - !ruby/object:Gem::Dependency
58
- name: activesupport
57
+ name: activemodel
59
58
  prerelease: false
60
59
  requirement: &id003 !ruby/object:Gem::Requirement
61
60
  none: false
62
61
  requirements:
63
62
  - - ~>
64
63
  - !ruby/object:Gem::Version
65
- hash: 9
64
+ hash: 7
66
65
  segments:
67
- - 2
68
66
  - 3
69
- - 5
70
- version: 2.3.5
67
+ - 0
68
+ - 0
69
+ version: 3.0.0
71
70
  type: :runtime
72
71
  version_requirements: *id003
73
72
  - !ruby/object:Gem::Dependency
74
- name: activemodel
73
+ name: tzinfo
75
74
  prerelease: false
76
75
  requirement: &id004 !ruby/object:Gem::Requirement
77
76
  none: false
78
77
  requirements:
79
78
  - - ~>
80
79
  - !ruby/object:Gem::Version
81
- hash: -1848230024
80
+ hash: 63
82
81
  segments:
83
- - 3
84
- - 0
85
82
  - 0
86
- - beta4
87
- version: 3.0.0.beta4
83
+ - 3
84
+ - 22
85
+ version: 0.3.22
88
86
  type: :runtime
89
87
  version_requirements: *id004
90
88
  - !ruby/object:Gem::Dependency
91
- name: tzinfo
89
+ name: railties
92
90
  prerelease: false
93
91
  requirement: &id005 !ruby/object:Gem::Requirement
94
92
  none: false
95
93
  requirements:
96
94
  - - ~>
97
95
  - !ruby/object:Gem::Version
98
- hash: 63
96
+ hash: 7
99
97
  segments:
100
- - 0
101
98
  - 3
102
- - 22
103
- version: 0.3.22
99
+ - 0
100
+ - 0
101
+ version: 3.0.0
104
102
  type: :runtime
105
103
  version_requirements: *id005
104
+ - !ruby/object:Gem::Dependency
105
+ name: rspec
106
+ prerelease: false
107
+ requirement: &id006 !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 15
113
+ segments:
114
+ - 2
115
+ - 0
116
+ - 0
117
+ version: 2.0.0
118
+ type: :runtime
119
+ version_requirements: *id006
120
+ - !ruby/object:Gem::Dependency
121
+ name: rspec
122
+ prerelease: false
123
+ requirement: &id007 !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ hash: 15
129
+ segments:
130
+ - 2
131
+ - 0
132
+ - 0
133
+ version: 2.0.0
134
+ type: :development
135
+ version_requirements: *id007
136
+ - !ruby/object:Gem::Dependency
137
+ name: rack-test
138
+ prerelease: false
139
+ requirement: &id008 !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ hash: 5
145
+ segments:
146
+ - 0
147
+ - 5
148
+ - 7
149
+ version: 0.5.7
150
+ type: :development
151
+ version_requirements: *id008
106
152
  description: CouchRest Model provides aditional features to the standard CouchRest Document class such as properties, view designs, associations, callbacks, typecasting and validations.
107
153
  email: jchris@apache.org
108
154
  executables: []
@@ -114,21 +160,27 @@ extra_rdoc_files:
114
160
  - README.md
115
161
  - THANKS.md
116
162
  files:
163
+ - .gitignore
164
+ - .rspec
165
+ - Gemfile
166
+ - Gemfile.lock
117
167
  - LICENSE
118
168
  - README.md
119
169
  - Rakefile
120
170
  - THANKS.md
171
+ - VERSION
172
+ - couchrest_model.gemspec
121
173
  - history.txt
174
+ - init.rb
122
175
  - lib/couchrest/model.rb
123
176
  - lib/couchrest/model/associations.rb
124
- - lib/couchrest/model/attribute_protection.rb
125
- - lib/couchrest/model/attributes.rb
126
177
  - lib/couchrest/model/base.rb
127
178
  - lib/couchrest/model/callbacks.rb
128
179
  - lib/couchrest/model/casted_array.rb
129
180
  - lib/couchrest/model/casted_model.rb
130
181
  - lib/couchrest/model/class_proxy.rb
131
182
  - lib/couchrest/model/collection.rb
183
+ - lib/couchrest/model/configuration.rb
132
184
  - lib/couchrest/model/design_doc.rb
133
185
  - lib/couchrest/model/document_queries.rb
134
186
  - lib/couchrest/model/errors.rb
@@ -136,6 +188,7 @@ files:
136
188
  - lib/couchrest/model/persistence.rb
137
189
  - lib/couchrest/model/properties.rb
138
190
  - lib/couchrest/model/property.rb
191
+ - lib/couchrest/model/property_protection.rb
139
192
  - lib/couchrest/model/support/couchrest.rb
140
193
  - lib/couchrest/model/support/hash.rb
141
194
  - lib/couchrest/model/typecast.rb
@@ -143,21 +196,24 @@ files:
143
196
  - lib/couchrest/model/validations/casted_model.rb
144
197
  - lib/couchrest/model/validations/locale/en.yml
145
198
  - lib/couchrest/model/validations/uniqueness.rb
199
+ - lib/couchrest/model/view.rb
146
200
  - lib/couchrest/model/views.rb
147
201
  - lib/couchrest/railtie.rb
148
202
  - lib/couchrest_model.rb
149
203
  - lib/rails/generators/couchrest_model.rb
150
204
  - lib/rails/generators/couchrest_model/model/model_generator.rb
151
205
  - lib/rails/generators/couchrest_model/model/templates/model.rb
206
+ - spec/.gitignore
152
207
  - spec/couchrest/assocations_spec.rb
153
208
  - spec/couchrest/attachment_spec.rb
154
- - spec/couchrest/attribute_protection_spec.rb
155
209
  - spec/couchrest/base_spec.rb
156
210
  - spec/couchrest/casted_model_spec.rb
157
211
  - spec/couchrest/casted_spec.rb
158
212
  - spec/couchrest/class_proxy_spec.rb
213
+ - spec/couchrest/configuration_spec.rb
159
214
  - spec/couchrest/inherited_spec.rb
160
215
  - spec/couchrest/persistence_spec.rb
216
+ - spec/couchrest/property_protection_spec.rb
161
217
  - spec/couchrest/property_spec.rb
162
218
  - spec/couchrest/subclass_spec.rb
163
219
  - spec/couchrest/validations.rb
@@ -184,15 +240,14 @@ files:
184
240
  - spec/fixtures/views/test_view/only-map.js
185
241
  - spec/fixtures/views/test_view/test-map.js
186
242
  - spec/fixtures/views/test_view/test-reduce.js
187
- - spec/spec.opts
188
243
  - spec/spec_helper.rb
189
244
  has_rdoc: true
190
245
  homepage: http://github.com/couchrest/couchrest_model
191
246
  licenses: []
192
247
 
193
248
  post_install_message:
194
- rdoc_options:
195
- - --charset=UTF-8
249
+ rdoc_options: []
250
+
196
251
  require_paths:
197
252
  - lib
198
253
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -218,24 +273,28 @@ required_rubygems_version: !ruby/object:Gem::Requirement
218
273
  requirements: []
219
274
 
220
275
  rubyforge_project:
221
- rubygems_version: 1.3.7
276
+ rubygems_version: 1.4.2
222
277
  signing_key:
223
278
  specification_version: 3
224
279
  summary: Extends the CouchRest Document for advanced modelling.
225
280
  test_files:
226
281
  - spec/couchrest/assocations_spec.rb
227
282
  - spec/couchrest/attachment_spec.rb
228
- - spec/couchrest/attribute_protection_spec.rb
229
283
  - spec/couchrest/base_spec.rb
230
284
  - spec/couchrest/casted_model_spec.rb
231
285
  - spec/couchrest/casted_spec.rb
232
286
  - spec/couchrest/class_proxy_spec.rb
287
+ - spec/couchrest/configuration_spec.rb
233
288
  - spec/couchrest/inherited_spec.rb
234
289
  - spec/couchrest/persistence_spec.rb
290
+ - spec/couchrest/property_protection_spec.rb
235
291
  - spec/couchrest/property_spec.rb
236
292
  - spec/couchrest/subclass_spec.rb
237
293
  - spec/couchrest/validations.rb
238
294
  - spec/couchrest/view_spec.rb
295
+ - spec/fixtures/attachments/README
296
+ - spec/fixtures/attachments/couchdb.png
297
+ - spec/fixtures/attachments/test.html
239
298
  - spec/fixtures/base.rb
240
299
  - spec/fixtures/more/article.rb
241
300
  - spec/fixtures/more/card.rb
@@ -250,4 +309,9 @@ test_files:
250
309
  - spec/fixtures/more/sale_invoice.rb
251
310
  - spec/fixtures/more/service.rb
252
311
  - spec/fixtures/more/user.rb
312
+ - spec/fixtures/views/lib.js
313
+ - spec/fixtures/views/test_view/lib.js
314
+ - spec/fixtures/views/test_view/only-map.js
315
+ - spec/fixtures/views/test_view/test-map.js
316
+ - spec/fixtures/views/test_view/test-reduce.js
253
317
  - spec/spec_helper.rb
@@ -1,74 +0,0 @@
1
- module CouchRest
2
- module Model
3
- module AttributeProtection
4
- # Attribute protection from mass assignment to CouchRest properties
5
- #
6
- # Protected methods will be removed from
7
- # * new
8
- # * update_attributes
9
- # * upate_attributes_without_saving
10
- # * attributes=
11
- #
12
- # There are two modes of protection
13
- # 1) Declare accessible poperties, assume all the rest are protected
14
- # property :name, :accessible => true
15
- # property :admin # this will be automatically protected
16
- #
17
- # 2) Declare protected properties, assume all the rest are accessible
18
- # property :name # this will not be protected
19
- # property :admin, :protected => true
20
- #
21
- # Note: you cannot set both flags in a single class
22
-
23
- def self.included(base)
24
- base.extend(ClassMethods)
25
- end
26
-
27
- module ClassMethods
28
- def accessible_properties
29
- properties.select { |prop| prop.options[:accessible] }
30
- end
31
-
32
- def protected_properties
33
- properties.select { |prop| prop.options[:protected] }
34
- end
35
- end
36
-
37
- def accessible_properties
38
- self.class.accessible_properties
39
- end
40
-
41
- def protected_properties
42
- self.class.protected_properties
43
- end
44
-
45
- def remove_protected_attributes(attributes)
46
- protected_names = properties_to_remove_from_mass_assignment.map { |prop| prop.name }
47
- return attributes if protected_names.empty?
48
-
49
- attributes.reject! do |property_name, property_value|
50
- protected_names.include?(property_name.to_s)
51
- end
52
-
53
- attributes || {}
54
- end
55
-
56
- private
57
-
58
- def properties_to_remove_from_mass_assignment
59
- has_protected = !protected_properties.empty?
60
- has_accessible = !accessible_properties.empty?
61
-
62
- if !has_protected && !has_accessible
63
- []
64
- elsif has_protected && !has_accessible
65
- protected_properties
66
- elsif has_accessible && !has_protected
67
- properties.reject { |prop| prop.options[:accessible] }
68
- else
69
- raise "Set either :accessible or :protected for #{self.class}, but not both"
70
- end
71
- end
72
- end
73
- end
74
- end
@@ -1,75 +0,0 @@
1
- module CouchRest
2
- module Model
3
- module Attributes
4
-
5
- ## Support for handling attributes
6
- #
7
- # This would be better in the properties file, but due to scoping issues
8
- # this is not yet possible.
9
- #
10
-
11
- def prepare_all_attributes(doc = {}, options = {})
12
- apply_all_property_defaults
13
- if options[:directly_set_attributes]
14
- directly_set_read_only_attributes(doc)
15
- else
16
- remove_protected_attributes(doc)
17
- end
18
- directly_set_attributes(doc) unless doc.nil?
19
- end
20
-
21
- # Takes a hash as argument, and applies the values by using writer methods
22
- # for each key. It doesn't save the document at the end. Raises a NoMethodError if the corresponding methods are
23
- # missing. In case of error, no attributes are changed.
24
- def update_attributes_without_saving(hash)
25
- # Remove any protected and update all the rest. Any attributes
26
- # which do not have a property will simply be ignored.
27
- attrs = remove_protected_attributes(hash)
28
- directly_set_attributes(attrs)
29
- end
30
- alias :attributes= :update_attributes_without_saving
31
-
32
- # Takes a hash as argument, and applies the values by using writer methods
33
- # for each key. Raises a NoMethodError if the corresponding methods are
34
- # missing. In case of error, no attributes are changed.
35
- def update_attributes(hash)
36
- update_attributes_without_saving hash
37
- save
38
- end
39
-
40
- private
41
-
42
- def directly_set_attributes(hash)
43
- hash.each do |attribute_name, attribute_value|
44
- if self.respond_to?("#{attribute_name}=")
45
- self.send("#{attribute_name}=", hash.delete(attribute_name))
46
- end
47
- end
48
- end
49
-
50
- def directly_set_read_only_attributes(hash)
51
- property_list = self.properties.map{|p| p.name}
52
- hash.each do |attribute_name, attribute_value|
53
- next if self.respond_to?("#{attribute_name}=")
54
- if property_list.include?(attribute_name)
55
- write_attribute(attribute_name, hash.delete(attribute_name))
56
- end
57
- end
58
- end
59
-
60
- def set_attributes(hash)
61
- attrs = remove_protected_attributes(hash)
62
- directly_set_attributes(attrs)
63
- end
64
-
65
- def check_properties_exist(attrs)
66
- property_list = self.properties.map{|p| p.name}
67
- attrs.each do |attribute_name, attribute_value|
68
- raise NoMethodError, "Property #{attribute_name} not created" unless respond_to?("#{attribute_name}=") or property_list.include?(attribute_name)
69
- end
70
- end
71
-
72
- end
73
- end
74
- end
75
-