From 89c45ebb87bca80cd6f405cae4b1bead251f9179 Mon Sep 17 00:00:00 2001 From: Sam Lown Date: Thu, 13 May 2010 00:17:30 +0200 Subject: [PATCH] Adding support for setting types with hash --- history.txt | 1 + lib/couchrest/mixins/properties.rb | 14 +++++++++++--- spec/couchrest/extended_doc_spec.rb | 20 ++++++++++++++++++++ spec/fixtures/more/cat.rb | 4 ++-- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/history.txt b/history.txt index d5fd15a..75b13ec 100644 --- a/history.txt +++ b/history.txt @@ -11,6 +11,7 @@ * Added support for instantiation of documents read from database as couchrest-type provided (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) + * Added support for setting type directly on property (Sam Lown) == 1.0.2 diff --git a/lib/couchrest/mixins/properties.rb b/lib/couchrest/mixins/properties.rb index dae915e..744cf95 100644 --- a/lib/couchrest/mixins/properties.rb +++ b/lib/couchrest/mixins/properties.rb @@ -82,10 +82,18 @@ module CouchRest 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} - if existing_property.nil? || (existing_property.default != options[:default]) - define_property(name, options) + if existing_property.nil? || (existing_property.default != opts[:default]) + define_property(name, opts) end end diff --git a/spec/couchrest/extended_doc_spec.rb b/spec/couchrest/extended_doc_spec.rb index e03f1a1..c2712d6 100644 --- a/spec/couchrest/extended_doc_spec.rb +++ b/spec/couchrest/extended_doc_spec.rb @@ -18,6 +18,14 @@ describe "ExtendedDocument" do property :name timestamps! 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 include ::CouchRest::Validation @@ -277,6 +285,18 @@ describe "ExtendedDocument" do obj.read_only_with_default.should == 'generic' 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 before(:all) do diff --git a/spec/fixtures/more/cat.rb b/spec/fixtures/more/cat.rb index 0e2c5ea..7ab5397 100644 --- a/spec/fixtures/more/cat.rb +++ b/spec/fixtures/more/cat.rb @@ -15,8 +15,8 @@ class Cat < CouchRest::ExtendedDocument use_database DB property :name, :accessible => true - property :toys, :cast_as => [CatToy], :default => [], :accessible => true - property :favorite_toy, :cast_as => CatToy, :accessible => true + property :toys, [CatToy], :default => [], :accessible => true + property :favorite_toy, CatToy, :accessible => true property :number end