README.markdown in couchpillow-0.3.10 vs README.markdown in couchpillow-0.4.0

- old
+ new

@@ -28,100 +28,119 @@ ## How To Use require 'couchpillow' + class MyDocument < CouchPillow::Document + type :my_document + attribute(:stuff) + end + CouchPillow.db = Couchbase.connect( bucket: 'default', host: 'localhost' ) - doc = CouchPillow::Document.new( { :stuff => 'hello' }, '123' ) + doc = CouchPillow::Document.new( { :stuff => 'hello' } ) doc.save! # { - # '_type': 'default', + # '_id': 'my_document_fb579b265cc005c47ff420a5c2a15d2b', + # '_type': 'my_document', # 'stuff': 'hello', - # 'created_at': '2014-07-04 00:00:00 UTC' - # 'updated_at': '2014-07-04 00:00:00 UTC' + # '_created_at': '2014-07-04 00:00:00 UTC' + # '_updated_at': '2014-07-04 00:00:00 UTC' # } Retrieving Documents: - doc = CouchPillow::Document.get('123') + doc = MyDocument.get('123') doc.stuff # 'hello' - -Overriding `CouchPillow::Document`: +Specifying custom id: class User < CouchPillow::Document type :user + attribute(:email) end CouchPillow.db = Couchbase.connect( bucket: 'default', host: 'localhost' ) - doc = User.new( { :email => 'john@email.com' } ) + doc = User.new( { :email => 'john@email.com' }, '123' ) doc.email # 'john@email.com' doc.save! # { - # '_id': 'fb579b265cc005c47ff420a5c2a15d2b', + # '_id': '123', # '_type': 'user', # 'email': 'john@email.com', # 'created_at': '2014-07-04 00:00:00 UTC' # 'updated_at': '2014-07-04 00:00:00 UTC' # } -Using built-in validation methods: +### Attributes +Using Attribute Directives: + class User < CouchPillow::Document type :user - validate_presence :email - validate_type :first_name, String + attribute(:email) + .required + + attribute(:first_name) + .type(String) end CouchPillow.db = Couchbase.connect( bucket: 'default', host: 'localhost' ) doc = User.new( { :first_name => 'John' } ) - doc.save! # raises ValidationError('email is missing') + doc.save! # raises ValidationError "Attribute 'email' is missing" doc.email = 'john@email.com' doc.save! # Success! -Using custom validation blocks: +List of Attribute Directives: - class User < CouchPillow::Document - type :user - validate :phone, 'is not a number', lambda { |v| v.is_a? Numeric } - end +* `required` - CouchPillow.db = Couchbase.connect( bucket: 'default', host: 'localhost' ) - doc = User.new - doc.phone = '123' - doc.save! # raises ValidationError('phone is not a number') - doc.phone = 123 - doc.save! # Success! + Sets this attribute as required. Will raise an error if attribute is missing + upon `save!` or `update!` -Using `rename` to rename keys. Useful to maintain document integrity. +* `type(T)` + Sets the type of this attribute. Will perform type check if specified. + +* `auto_convert` + + Enables auto-conversion to the specified type. This gets ignored if `type` + directive is not specified. + +* `default(&block)` + + Runs the block to set the default value for this attribute, if it's missing + or nil. + +* `content(&block)` + + Custom validation method to check the value of the attribute. This is useful + in cases where you only want certain values to be stored (e.g a number between + 1-10 only) + + +### Migration + +Using `rename` to rename keys. Useful to maintain document integrity +from a migration. + class User < CouchPillow::Document rename :username, :nickname + attribute(:nickname) end u = User.new( { :username => 'jdoe' } ) u.nickname # 'jdoe' -Using `whitelist` to whitelist keys. Also useful to maintain document integrity -and keeping other fields from being saved. Keep in mind that when loading -existing documents and if they have keys that are not listed on the whitelist, -those keys will be dropped upon `save!`. - class User < CouchPillow::Document - whitelist :name - end - u = User.new( { :age => 10, :name => 'John' } ) - u.has?(:age) # false - ## Design Docs and Views What about Design Docs and Views? They are outside the scope of CouchPillow. However, given a design doc named `my_design_doc` and a View named `by_email`, that returns documents as values, you can easily use it like this: CouchPillow.db.design_docs['my_design_doc']. by_email(:body => { :key => 'john@email.com' }).map do |v| - new User(v.value, v.id) + new User(v.doc, v.id) end