speedmax-couch_potato 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/MIT-LICENSE.txt +19 -0
  2. data/README.md +260 -0
  3. data/VERSION.yml +4 -0
  4. data/init.rb +3 -0
  5. data/lib/core_ext/date.rb +10 -0
  6. data/lib/core_ext/object.rb +5 -0
  7. data/lib/core_ext/string.rb +15 -0
  8. data/lib/core_ext/time.rb +11 -0
  9. data/lib/couch_potato/database.rb +90 -0
  10. data/lib/couch_potato/persistence/belongs_to_property.rb +58 -0
  11. data/lib/couch_potato/persistence/callbacks.rb +108 -0
  12. data/lib/couch_potato/persistence/dirty_attributes.rb +26 -0
  13. data/lib/couch_potato/persistence/inline_collection.rb +14 -0
  14. data/lib/couch_potato/persistence/json.rb +41 -0
  15. data/lib/couch_potato/persistence/magic_timestamps.rb +13 -0
  16. data/lib/couch_potato/persistence/properties.rb +42 -0
  17. data/lib/couch_potato/persistence/simple_property.rb +64 -0
  18. data/lib/couch_potato/persistence.rb +57 -0
  19. data/lib/couch_potato/view/base_view_spec.rb +20 -0
  20. data/lib/couch_potato/view/custom_view_spec.rb +26 -0
  21. data/lib/couch_potato/view/custom_views.rb +30 -0
  22. data/lib/couch_potato/view/model_view_spec.rb +39 -0
  23. data/lib/couch_potato/view/properties_view_spec.rb +35 -0
  24. data/lib/couch_potato/view/raw_view_spec.rb +21 -0
  25. data/lib/couch_potato/view/view_query.rb +45 -0
  26. data/lib/couch_potato.rb +37 -0
  27. data/rails/init.rb +7 -0
  28. data/spec/callbacks_spec.rb +245 -0
  29. data/spec/create_spec.rb +22 -0
  30. data/spec/custom_view_spec.rb +118 -0
  31. data/spec/destroy_spec.rb +29 -0
  32. data/spec/property_spec.rb +64 -0
  33. data/spec/spec.opts +4 -0
  34. data/spec/spec_helper.rb +30 -0
  35. data/spec/unit/attributes_spec.rb +26 -0
  36. data/spec/unit/create_spec.rb +58 -0
  37. data/spec/unit/dirty_attributes_spec.rb +100 -0
  38. data/spec/unit/string_spec.rb +13 -0
  39. data/spec/unit/view_query_spec.rb +9 -0
  40. data/spec/update_spec.rb +40 -0
  41. metadata +127 -0
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2007 Bryan Helmkamp, Seth Fitzsimmons
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,260 @@
1
+ ## Couch Potato
2
+
3
+ ... is a persistence layer written in ruby for CouchDB.
4
+
5
+ ### Mission
6
+
7
+ The goal of Couch Potato is to create a minimal framework in order to store and retrieve Ruby objects to/from CouchDB and create and query views.
8
+
9
+ It follows the document/view/querying semantics established by CouchDB and won't try to mimic ActiveRecord behavior in any way as that IS BAD.
10
+
11
+ Code that uses Couch Potato should be easy to test.
12
+
13
+ Lastly Couch Potato aims to provide a seamless integration with Ruby on Rails, e.g. routing, form helpers etc.
14
+
15
+ ### Core Features
16
+
17
+ * persisting objects by including the CouchPotato::Persistence module
18
+ * declarative views with either custom or generated map/reduce functions
19
+ * extensive spec suite
20
+
21
+ ### Installation
22
+
23
+ Couch Potato requires Ruby 1.9.
24
+
25
+ Couch Potato is hosted as a gem on github which you can install like this:
26
+
27
+ sudo gem source --add http://gems.github.com # if you haven't already
28
+ sudo gem install langalex-couch_potato
29
+
30
+ #### Using with your ruby application:
31
+
32
+ require 'rubygems'
33
+ gem 'langalex-couch_potato'
34
+ require 'couch_potato'
35
+ CouchPotato::Config.database_name = 'name of the db'
36
+
37
+ Alternatively you can download or clone the source repository and then require lib/couch_potato.rb.
38
+
39
+ #### Using with Rails
40
+
41
+ Add to your config/environment.rb:
42
+
43
+ config.gem 'langalex-couch_potato', :lib => 'couch_potato', :source => 'http://gems.github.com'
44
+
45
+ Then create a config/couchdb.yml:
46
+
47
+ development: development_db_name
48
+ test: test_db_name
49
+ production: http://db.server/production_db_name
50
+
51
+ Alternatively you can also install Couch Potato directly as a plugin.
52
+
53
+ ### Introduction
54
+
55
+ This is a basic tutorial on how to use Couch Potato. If you want to know all the details feel free to read the specs.
56
+
57
+ #### Save, load objects
58
+
59
+ First you need a class.
60
+
61
+ class User
62
+ end
63
+
64
+ To make instances of this class persistent include the persistence module:
65
+
66
+ class User
67
+ include CouchPotato::Persistence
68
+ end
69
+
70
+ If you want to store any properties you have to declare them:
71
+
72
+ class User
73
+ include CouchPotato::Persistence
74
+
75
+ property :name
76
+ end
77
+
78
+ Properties can be of any type:
79
+
80
+ class User
81
+ include CouchPotato::Persistence
82
+
83
+ property :address, :type => Address
84
+ end
85
+
86
+ Now you can save your objects. All database operations are encapsulated in the CouchPotato::Database class. This separates your domain logic from the database access logic which makes it easier to write tests and also keeps you models smaller and cleaner.
87
+
88
+ user = User.new :name => 'joe'
89
+ CouchPotato.database.save_document user # or save_document!
90
+
91
+ You can of course also retrieve your instance:
92
+
93
+ CouchPotato.database.load_document "id_of_the_user_document" # => <#User 0x3075>
94
+
95
+
96
+ #### Properties
97
+
98
+ You can access the properties you declared above through normal attribute accessors.
99
+
100
+ user.name # => 'joe'
101
+ user.name = {:first => ['joe', 'joey'], :last => 'doe', :middle => 'J'} # you can set any ruby object that responds_to :to_json (includes all core objects)
102
+ user._id # => "02097f33a0046123f1ebc0ebb6937269"
103
+ user._rev # => "2769180384"
104
+ user.created_at # => Fri Oct 24 19:05:54 +0200 2008
105
+ user.updated_at # => Fri Oct 24 19:05:54 +0200 2008
106
+ user.new? # => false
107
+
108
+ If you want to have properties that don't map to any JSON type, i.e. other than String, Number, Boolean, Hash or Array you have to define the type like this:
109
+
110
+ class User
111
+ property :date_of_birth, :type => Date
112
+ end
113
+
114
+ The date_of_birth property is now automatically serialized to JSON and back when storing/retrieving objects.
115
+
116
+ #### Dirty tracking
117
+
118
+ CouchPotato tracks the dirty state of attributes in the same way ActiveRecord does:
119
+
120
+ user = User.create :name => 'joe'
121
+ user.name # => 'joe'
122
+ user.name_changed? # => false
123
+ user.name_was # => nil
124
+
125
+ You can also force a dirty state:
126
+
127
+ user.name = 'jane'
128
+ user.name_changed? # => true
129
+ user.name_not_changed
130
+ user.name_changed? # => false
131
+ CouchPotato.database.save_document user # does nothing as no attributes are dirty
132
+
133
+
134
+ #### Object validations
135
+
136
+ Couch Potato uses the validatable library for vaidation (http://validatable.rubyforge.org/)\
137
+
138
+ class User
139
+ property :name
140
+ validates_presence_of :name
141
+ end
142
+
143
+ user = User.new
144
+ user.valid? # => false
145
+ user.errors.on(:name) # => [:name, 'can't be blank']
146
+
147
+ #### Finding stuff
148
+
149
+ In order to find data in your CouchDB you have to create a view first. Couch Potato offers you to create and manage those views for you. All you have to do is declare them in your classes:
150
+
151
+ class User
152
+ include CouchPotato::Persistence
153
+ property :name
154
+
155
+ view :all, :key => :created_at
156
+ end
157
+
158
+ This will create a view called "all" in the "user" design document with a map function that emits "created_at" for every user document.
159
+
160
+ CouchPotato.database.view User.all
161
+
162
+ This will load all user documents in your database sorted by created_at.
163
+
164
+ CouchPotato.database.view User.all(:key => (Time.now- 10)..(Time.now), :descending => true)
165
+
166
+ Any options you pass in will be passed onto CouchDB.
167
+
168
+ Composite keys are also possible:
169
+
170
+ class User
171
+ property :name
172
+
173
+ view :all, :key => [:created_at, :name]
174
+ end
175
+
176
+ The creation of views is based on view specification classes (see the CouchPotato::View) module. The above code used the ModelViewSpec class which is used to the simple find model by property searches. For more sophisticated searches you can use other view specifications (either use the built-in or provide your own) by passing a type parameter:
177
+
178
+ If you have larger structures and you only want to load some attributes you can customize the view you can use the PropertiesViewSpec (the full class name is automatically derived):
179
+
180
+ class User
181
+ property :name
182
+ property :bio
183
+
184
+ view :all, :key => :created_at, :properties => [:name], :type => :properties
185
+ end
186
+
187
+ CouchPotato.database.view(User.everyone).first.name # => "joe"
188
+ CouchPotato.database.view(User.everyone).first.bio # => nil
189
+
190
+ You can also pass in custom map/reduce functions with the custom view spec:
191
+
192
+ class User
193
+ view :all, :map => "function(doc) { emit(doc.created_at, null)}", :include_docs => true, :type => :custom
194
+ end
195
+
196
+ If you don't want the results to be converted into models the raw view is your friend:
197
+
198
+ class User
199
+ view :all, :map => "function(doc) { emit(doc.created_at, doc.name)}", :type => :raw
200
+ end
201
+
202
+ When querying this view you will get the raw data returned by CouchDB which looks something like this: {'total_entries': 2, 'rows': [{'value': 'alex', 'key': '2009-01-03 00:02:34 +000', 'id': '75976rgi7546gi02a'}]}
203
+
204
+ To process this raw data you can also pass in a results filter:
205
+
206
+ class User
207
+ view :all, :map => "function(doc) { emit(doc.created_at, doc.name)}", :type => :raw, :results_filter => lambda {|results| results['rows'].map{|row| row['value']}}
208
+ end
209
+
210
+ In this case querying the view would only return the emitted value for each row.
211
+
212
+ #### Associations
213
+
214
+ Not supported. Not sure if they ever will be. You can implement those yourself using views and custom methods on your models.
215
+
216
+ #### Callbacks
217
+
218
+ Couch Potato supports the usual lifecycle callbacks known from ActiveRecord:
219
+
220
+ class User
221
+ include CouchPotato::Persistence
222
+
223
+ before_create :do_something_before_create
224
+ after_update :do_something_else
225
+ end
226
+
227
+ This will call the method do_something_before_create before creating an object and do_something_else after updating one. Supported callbacks are: :before_validation_on_create, :before_validation_on_update, :before_validation_on_save, :before_create, :after_create, :before_update, :after_update, :before_save, :after_save, :before_destroy, :after_destroy. You can also pass a Proc instead of a method name.
228
+
229
+ #### Testing
230
+
231
+ To make testing easier and faster database logic has been put into its own class, which you can replace and stub out in whatever way you want:
232
+
233
+ class User
234
+ include CouchPotato::Persistence
235
+ end
236
+
237
+ # RSpec
238
+ describe 'save a user' do
239
+ it 'should save' do
240
+ couchrest_db = stub 'couchrest_db',
241
+ database = CouchPotato::Database.new couchrest_db
242
+ user = User.new
243
+ couchrest_db.should_receive(:save_doc).with(...)
244
+ database.save_document user
245
+ end
246
+ end
247
+
248
+ By creating you own instances of CouchPotato::Database and passing them a fake CouchRest database instance you can completely disconnect your unit tests/spec from the database.
249
+
250
+ ### Helping out
251
+
252
+ Please fix bugs, add more specs, implement new features by forking the github repo at http://github.com/langalex/couch_potato.
253
+
254
+ You can run all the specs by calling 'rake spec_unit' and 'rake spec_functional' in the root folder of Couch Potato. The specs require a running CouchDB instance at http://localhost:5984
255
+
256
+ I will only accept patches that are covered by specs - sorry.
257
+
258
+ ### Contact
259
+
260
+ If you have any questions/suggestions etc. please contact me at alex at upstream-berlin.com or @langalex on twitter.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 2
4
+ :patch: 0
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ # this is for rails only
2
+
3
+ require File.dirname(__FILE__) + '/rails/init'
@@ -0,0 +1,10 @@
1
+ class Date
2
+ def to_json(*a)
3
+ %("#{strftime("%Y/%m/%d")}")
4
+ end
5
+
6
+ def self.json_create string
7
+ return nil if string.nil?
8
+ Date.parse(string)
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ Object.class_eval do
2
+ def try(method, *args)
3
+ self.send method, *args if self.respond_to?(method)
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveSupportMethods
2
+ def camelize
3
+ sub(/^([a-z])/) {$1.upcase}.gsub(/_([a-z])/) do
4
+ $1.upcase
5
+ end
6
+ end
7
+
8
+ def underscore
9
+ gsub(/([A-Z])/) do
10
+ '_' + $1.downcase
11
+ end.sub(/^_/, '')
12
+ end
13
+ end
14
+
15
+ String.send :include, ActiveSupportMethods #unless String.new.respond_to?(:camelize)
@@ -0,0 +1,11 @@
1
+ class Time
2
+ def to_json(*a)
3
+ %("#{strftime("%Y/%m/%d %H:%M:%S +0000")}")
4
+ end
5
+
6
+ def self.json_create string
7
+ return nil if string.nil?
8
+ d = DateTime.parse(string).new_offset
9
+ self.utc(d.year, d.month, d.day, d.hour, d.min, d.sec)
10
+ end
11
+ end
@@ -0,0 +1,90 @@
1
+ module CouchPotato
2
+ class Database
3
+
4
+ class ValidationsFailedError < ::Exception; end
5
+
6
+ def initialize(couchrest_database)
7
+ @database = couchrest_database
8
+ end
9
+
10
+ def view(spec)
11
+ results = CouchPotato::View::ViewQuery.new(database,
12
+ spec.design_document, spec.view_name, spec.map_function,
13
+ spec.reduce_function).query_view!(spec.view_parameters)
14
+ spec.process_results results
15
+ end
16
+
17
+ def save_document(document)
18
+ return unless document.dirty?
19
+ if document.new?
20
+ create_document document
21
+ else
22
+ update_document document
23
+ end
24
+ end
25
+ alias_method :save, :save_document
26
+
27
+ def save_document!(document)
28
+ save_document(document) || raise(ValidationsFailedError.new(document.errors.full_messages))
29
+ end
30
+ alias_method :save!, :save_document!
31
+
32
+ def destroy_document(document)
33
+ document.run_callbacks(:before_destroy)
34
+ document._deleted = true
35
+ database.delete_doc document.to_hash
36
+ document.run_callbacks(:after_destroy)
37
+ document._id = nil
38
+ document._rev = nil
39
+ end
40
+ alias_method :destroy, :destroy_document
41
+
42
+ def load_document(id)
43
+ begin
44
+ json = database.get(id)
45
+ Class.const_get(json['ruby_class']).json_create json
46
+ rescue(RestClient::ResourceNotFound)
47
+ nil
48
+ end
49
+ end
50
+ alias_method :load, :load_document
51
+
52
+ def inspect
53
+ "#<CouchPotato::Database>"
54
+ end
55
+
56
+ private
57
+
58
+ def create_document(document)
59
+ document.run_callbacks :before_validation_on_save
60
+ document.run_callbacks :before_validation_on_create
61
+ return unless document.valid?
62
+ document.run_callbacks :before_save
63
+ document.run_callbacks :before_create
64
+ res = database.save_doc document.to_hash
65
+ document._rev = res['rev']
66
+ document._id = res['id']
67
+ document.run_callbacks :after_save
68
+ document.run_callbacks :after_create
69
+ true
70
+ end
71
+
72
+ def update_document(document)
73
+ document.run_callbacks(:before_validation_on_save)
74
+ document.run_callbacks(:before_validation_on_update)
75
+ return unless document.valid?
76
+ document.run_callbacks :before_save
77
+ document.run_callbacks :before_update
78
+ res = database.save_doc document.to_hash
79
+ document._rev = res['rev']
80
+ document.run_callbacks :after_save
81
+ document.run_callbacks :after_update
82
+ true
83
+ end
84
+
85
+ def database
86
+ @database
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,58 @@
1
+ module CouchPotato
2
+ module Persistence
3
+ class BelongsToProperty
4
+ attr_accessor :name
5
+
6
+ def initialize(owner_clazz, name, options = {})
7
+ self.name = name
8
+ accessors = <<-ACCESSORS
9
+ def #{name}
10
+ return @#{name} if instance_variable_defined?(:@#{name})
11
+ @#{name} = @#{name}_id ? #{item_class_name}.find(@#{name}_id) : nil
12
+ end
13
+
14
+ def #{name}=(value)
15
+ @#{name} = value
16
+ if value.nil?
17
+ @#{name}_id = nil
18
+ else
19
+ @#{name}_id = value.id
20
+ end
21
+ end
22
+
23
+ def #{name}_id=(id)
24
+ remove_instance_variable(:@#{name}) if instance_variable_defined?(:@#{name})
25
+ @#{name}_id = id
26
+ end
27
+ ACCESSORS
28
+ owner_clazz.class_eval accessors
29
+ owner_clazz.send :attr_reader, "#{name}_id"
30
+ end
31
+
32
+ def save(object)
33
+
34
+ end
35
+
36
+ def dirty?(object)
37
+ false
38
+ end
39
+
40
+ def destroy(object)
41
+
42
+ end
43
+
44
+ def build(object, json)
45
+ object.send "#{name}_id=", json["#{name}_id"]
46
+ end
47
+
48
+ def serialize(json, object)
49
+ json["#{name}_id"] = object.send("#{name}_id") if object.send("#{name}_id")
50
+ end
51
+
52
+ def item_class_name
53
+ @name.to_s.camelize
54
+ end
55
+
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,108 @@
1
+ module CouchPotato
2
+ module Persistence
3
+ module Callbacks
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+
7
+ base.class_eval do
8
+ attr_accessor :skip_callbacks
9
+ def self.callbacks
10
+ @callbacks ||= {}
11
+ @callbacks[self.name] ||= {:before_validation_on_create => [],
12
+ :before_validation_on_update => [], :before_validation_on_save => [], :before_create => [],
13
+ :after_create => [], :before_update => [], :after_update => [],
14
+ :before_save => [], :after_save => [],
15
+ :before_destroy => [], :after_destroy => []}
16
+ end
17
+ end
18
+ end
19
+
20
+ def run_callbacks(name)
21
+ return if skip_callbacks
22
+ self.class.callbacks[name].uniq.each do |callback|
23
+ run_callback callback
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def run_callback(name)
30
+ if name.is_a?(Symbol)
31
+ self.send name
32
+ elsif name.is_a?(Proc)
33
+ name.call self
34
+ else
35
+ raise "Don't know how to handle callback of type #{name.class.name}"
36
+ end
37
+ end
38
+
39
+ module ClassMethods
40
+ def before_validation_on_create(*names)
41
+ names.each do |name|
42
+ callbacks[:before_validation_on_create] << name
43
+ end
44
+ end
45
+
46
+ def before_validation_on_update(*names)
47
+ names.each do |name|
48
+ callbacks[:before_validation_on_update] << name
49
+ end
50
+ end
51
+
52
+ def before_validation_on_save(*names)
53
+ names.each do |name|
54
+ callbacks[:before_validation_on_save] << name
55
+ end
56
+ end
57
+
58
+ def before_create(*names)
59
+ names.each do |name|
60
+ callbacks[:before_create] << name
61
+ end
62
+ end
63
+
64
+ def before_save(*names)
65
+ names.each do |name|
66
+ callbacks[:before_save] << name
67
+ end
68
+ end
69
+
70
+ def before_update(*names)
71
+ names.each do |name|
72
+ callbacks[:before_update] << name
73
+ end
74
+ end
75
+
76
+ def before_destroy(*names)
77
+ names.each do |name|
78
+ callbacks[:before_destroy] << name
79
+ end
80
+ end
81
+
82
+ def after_update(*names)
83
+ names.each do |name|
84
+ callbacks[:after_update] << name
85
+ end
86
+ end
87
+
88
+ def after_save(*names)
89
+ names.each do |name|
90
+ callbacks[:after_save] << name
91
+ end
92
+ end
93
+
94
+ def after_create(*names)
95
+ names.each do |name|
96
+ callbacks[:after_create] << name
97
+ end
98
+ end
99
+
100
+ def after_destroy(*names)
101
+ names.each do |name|
102
+ callbacks[:after_destroy] << name
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,26 @@
1
+ module CouchPotato
2
+ module Persistence
3
+ module DirtyAttributes
4
+
5
+ def self.included(base)
6
+ base.class_eval do
7
+ after_save :reset_dirty_attributes
8
+ end
9
+ end
10
+
11
+ def dirty?
12
+ new? || self.class.properties.inject(false) do |res, property|
13
+ res || property.dirty?(self)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def reset_dirty_attributes
20
+ self.class.properties.each do |property|
21
+ instance_variable_set("@#{property.name}_was", send(property.name))
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/collection'
2
+
3
+ module CouchPotato
4
+ module Persistence
5
+ class InlineCollection < Collection
6
+
7
+ def to_json(*args)
8
+ @items.to_json(*args)
9
+ end
10
+
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,41 @@
1
+ module CouchPotato
2
+ module Persistence
3
+ module Json
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ def to_json(*args)
9
+ to_hash.to_json(*args)
10
+ end
11
+
12
+ def to_hash
13
+ (self.class.properties).inject({}) do |props, property|
14
+ property.serialize(props, self)
15
+ props
16
+ end.merge('ruby_class' => self.class.name).merge(id_and_rev_json)
17
+ end
18
+
19
+ private
20
+
21
+ def id_and_rev_json
22
+ ['_id', '_rev', '_deleted'].inject({}) do |hash, key|
23
+ hash[key] = self.send(key) unless self.send(key).nil?
24
+ hash
25
+ end
26
+ end
27
+
28
+ module ClassMethods
29
+ def json_create(json)
30
+ instance = self.new
31
+ instance._id = json[:_id] || json['_id']
32
+ instance._rev = json[:_rev] || json['_rev']
33
+ properties.each do |property|
34
+ property.build(instance, json)
35
+ end
36
+ instance
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,13 @@
1
+ module CouchPotato
2
+ module MagicTimestamps
3
+ def self.included(base)
4
+ base.instance_eval do
5
+ property :created_at, :type => Time
6
+ property :updated_at, :type => Time
7
+
8
+ before_create lambda {|model| model.created_at = Time.now; model.created_at_not_changed}
9
+ before_save lambda {|model| model.updated_at = Time.now; model.updated_at_not_changed}
10
+ end
11
+ end
12
+ end
13
+ end