[extended-document] added support for property :name, :default => 'Matt" and default couchrest type

This commit is contained in:
Matt Aimonetti 2009-02-05 18:57:11 -08:00
parent 890b60cae4
commit fa7b176fce
5 changed files with 63 additions and 6 deletions

View file

@ -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

View file

@ -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 <tt>updated_at</tt> and <tt>created_at</tt> fields
# on the document whenever saving occurs. CouchRest uses a pretty
# decent time format by default. See Time#to_json

View file

@ -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

View file

@ -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

View file

@ -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