Adding support for setting types with hash

This commit is contained in:
Sam Lown 2010-05-13 00:17:30 +02:00
parent b0d2258bd3
commit 89c45ebb87
4 changed files with 34 additions and 5 deletions

View file

@ -11,6 +11,7 @@
* Added support for instantiation of documents read from database as couchrest-type provided (Sam Lown) * Added support for instantiation of documents read from database as couchrest-type provided (Sam Lown)
* Improved attachment handling for detecting file type (Sam Lown) * Improved attachment handling for detecting file type (Sam Lown)
* Removing some monkey patches and relying on active_support for constantize and humanize (Sam Lown) * Removing some monkey patches and relying on active_support for constantize and humanize (Sam Lown)
* Added support for setting type directly on property (Sam Lown)
== 1.0.2 == 1.0.2

View file

@ -82,10 +82,18 @@ module CouchRest
module ClassMethods module ClassMethods
def property(name, options={}) def property(name, *options)
opts = { }
type = options.shift
if type.class != Hash
opts[:type] = type
opts.merge!(options.shift || {})
else
opts.update(type)
end
existing_property = self.properties.find{|p| p.name == name.to_s} existing_property = self.properties.find{|p| p.name == name.to_s}
if existing_property.nil? || (existing_property.default != options[:default]) if existing_property.nil? || (existing_property.default != opts[:default])
define_property(name, options) define_property(name, opts)
end end
end end

View file

@ -19,6 +19,14 @@ describe "ExtendedDocument" do
timestamps! timestamps!
end end
class WithSimplePropertyType < CouchRest::ExtendedDocument
use_database TEST_SERVER.default_database
property :name, String
property :preset, String, :default => 'none'
property :tags, [String]
timestamps!
end
class WithCallBacks < CouchRest::ExtendedDocument class WithCallBacks < CouchRest::ExtendedDocument
include ::CouchRest::Validation include ::CouchRest::Validation
use_database TEST_SERVER.default_database use_database TEST_SERVER.default_database
@ -278,6 +286,18 @@ describe "ExtendedDocument" do
end end
end end
describe "simplified way of setting property types" do
it "should set defaults" do
obj = WithSimplePropertyType.new
obj.preset.should eql('none')
end
it "should handle arrays" do
obj = WithSimplePropertyType.new(:tags => ['spec'])
obj.tags.should == ['spec']
end
end
describe "a doc with template values (CR::Model spec)" do describe "a doc with template values (CR::Model spec)" do
before(:all) do before(:all) do
WithTemplateAndUniqueID.all.map{|o| o.destroy(true)} WithTemplateAndUniqueID.all.map{|o| o.destroy(true)}

View file

@ -15,8 +15,8 @@ class Cat < CouchRest::ExtendedDocument
use_database DB use_database DB
property :name, :accessible => true property :name, :accessible => true
property :toys, :cast_as => [CatToy], :default => [], :accessible => true property :toys, [CatToy], :default => [], :accessible => true
property :favorite_toy, :cast_as => CatToy, :accessible => true property :favorite_toy, CatToy, :accessible => true
property :number property :number
end end