diff --git a/lib/couchrest/mixins/properties.rb b/lib/couchrest/mixins/properties.rb
index d56e9f6..400608b 100644
--- a/lib/couchrest/mixins/properties.rb
+++ b/lib/couchrest/mixins/properties.rb
@@ -2,8 +2,30 @@ module CouchRest
module Mixins
module DocumentProperties
+ class IncludeError < StandardError; end
+
def self.included(base)
base.extend(ClassMethods)
+ raise CouchRest::Mixins::DocumentProporties::InludeError, "You can only mixin Properties in a class responding to [] and []=" unless (base.new.respond_to?(:[]) && base.new.respond_to?(:[]=))
+ end
+
+ def apply_defaults
+ return unless new_document?
+ return if self.class.properties.empty?
+
+ # TODO: cache the default object
+ self.class.properties.each do |property|
+ key = property.name.to_s
+ # let's make sure we have a default and we can assign the value
+ if property.default && (self.respond_to?("#{key}=") || self.key?(key))
+ if property.default.class == Proc
+ self[key] = v.call
+ else
+ self[key] = Marshal.load(Marshal.dump(property.default))
+ end
+ end
+ end
+
end
module ClassMethods
diff --git a/lib/couchrest/more/extended_document.rb b/lib/couchrest/more/extended_document.rb
index 4cbd94d..dbe3f5c 100644
--- a/lib/couchrest/more/extended_document.rb
+++ b/lib/couchrest/more/extended_document.rb
@@ -14,8 +14,8 @@ module CouchRest
# Same as CouchRest::Document but with properties and validations
class ExtendedDocument < Document
include CouchRest::Callbacks
- include CouchRest::Mixins::DocumentProperties
include CouchRest::Mixins::DocumentQueries
+ include CouchRest::Mixins::DocumentProperties
include CouchRest::Mixins::Views
include CouchRest::Mixins::DesignDoc
@@ -23,6 +23,16 @@ module CouchRest
define_callbacks :save
define_callbacks :destroy
+ def initialize(keys={})
+ super
+ apply_defaults # defined in CouchRest::Mixins::DocumentProperties
+ # cast_keys
+ unless self['_id'] && self['_rev']
+ self['couchrest-type'] = self.class.to_s
+ end
+ end
+
+
# Automatically set updated_at and created_at fields
# on the document whenever saving occurs. CouchRest uses a pretty
# decent time format by default. See Time#to_json
diff --git a/lib/couchrest/more/property.rb b/lib/couchrest/more/property.rb
index bbe398d..e3c86d5 100644
--- a/lib/couchrest/more/property.rb
+++ b/lib/couchrest/more/property.rb
@@ -2,7 +2,7 @@ module CouchRest
# Basic attribute support for adding getter/setter + validation
class Property
- attr_reader :name, :type, :read_only, :alias, :options
+ attr_reader :name, :type, :read_only, :alias, :default, :options
# attribute to define
def initialize(name, type = nil, options = {})
@@ -16,9 +16,10 @@ module CouchRest
private
def parse_options(options)
return if options.empty?
- @validation_format = options.delete(:format) if options[:format]
- @read_only = options.delete(:read_only) if options[:read_only]
- @alias = options.delete(:alias) if options[:alias]
+ @validation_format = options.delete(:format) if options[:format]
+ @read_only = options.delete(:read_only) if options[:read_only]
+ @alias = options.delete(:alias) if options[:alias]
+ @default = options.delete(:default) if options[:default]
@options = options
end
diff --git a/lib/couchrest/validation/auto_validate.rb b/lib/couchrest/validation/auto_validate.rb
index d5a96aa..6ccb3aa 100644
--- a/lib/couchrest/validation/auto_validate.rb
+++ b/lib/couchrest/validation/auto_validate.rb
@@ -117,7 +117,6 @@ module CouchRest
opts[:maximum] = len
end
# validates_length property.name, opts
- p "dude: #{options_with_message(opts, property, :length)}"
validates_length property.name, options_with_message(opts, property, :length)
end
diff --git a/spec/couchrest/more/extended_doc_spec.rb b/spec/couchrest/more/extended_doc_spec.rb
new file mode 100644
index 0000000..eca0dfc
--- /dev/null
+++ b/spec/couchrest/more/extended_doc_spec.rb
@@ -0,0 +1,25 @@
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+# require File.join(FIXTURE_PATH, 'more', 'card')
+# require File.join(FIXTURE_PATH, 'more', 'invoice')
+# require File.join(FIXTURE_PATH, 'more', 'service')
+
+
+class WithDefaultValues < CouchRest::ExtendedDocument
+ use_database TEST_SERVER.default_database
+ property :preset, :default => {:right => 10, :top_align => false}
+end
+
+describe "ExtendedDocument" do
+
+ describe "with default" do
+ before(:each) do
+ @obj = WithDefaultValues.new
+ end
+
+ it "should have the default value set an initalization" do
+ @obj.preset.should == {:right => 10, :top_align => false}
+ end
+ end
+
+end
\ No newline at end of file