fixed the subclassing of ExtendedDocument classes

This commit is contained in:
Matt Aimonetti 2009-03-19 18:53:17 -07:00
parent bb119ae181
commit 115cb6a7ee
6 changed files with 79 additions and 12 deletions

File diff suppressed because one or more lines are too long

View file

@ -28,7 +28,7 @@ require 'couchrest/monkeypatches'
# = CouchDB, close to the metal
module CouchRest
VERSION = '0.21' unless self.const_defined?("VERSION")
VERSION = '0.22' unless self.const_defined?("VERSION")
autoload :Server, 'couchrest/core/server'
autoload :Database, 'couchrest/core/database'

View file

@ -8,10 +8,10 @@ module CouchRest
class IncludeError < StandardError; end
def self.included(base)
base.cattr_accessor(:properties)
base.class_eval <<-EOS, __FILE__, __LINE__
@@properties = []
EOS
base.class_eval <<-EOS, __FILE__, __LINE__
extlib_inheritable_accessor(:properties)
self.properties ||= []
EOS
base.extend(ClassMethods)
raise CouchRest::Mixins::Properties::IncludeError, "You can only mixin Properties in a class responding to [] and []=, if you tried to mixin CastedModel, make sure your class inherits from Hash or responds to the proper methods" unless (base.new.respond_to?(:[]) && base.new.respond_to?(:[]=))
end
@ -71,7 +71,10 @@ module CouchRest
module ClassMethods
def property(name, options={})
define_property(name, options) unless self.properties.map{|p| p.name}.include?(name.to_s)
existing_property = self.properties.find{|p| p.name == name.to_s}
if existing_property.nil? || (existing_property.default != options[:default])
define_property(name, options)
end
end
protected

View file

@ -49,10 +49,10 @@ module CouchRest
module Validation
def self.included(base)
base.cattr_accessor(:auto_validation)
base.extlib_inheritable_accessor(:auto_validation)
base.class_eval <<-EOS, __FILE__, __LINE__
# Turn off auto validation by default
@@auto_validation = false
self.auto_validation ||= false
# Force the auto validation for the class properties
# This feature is still not fully ported over,
@ -60,6 +60,11 @@ module CouchRest
def self.auto_validate!
self.auto_validation = true
end
# share the validations with subclasses
def self.inherited(subklass)
subklass.instance_variable_set(:@validations, self.validators.dup)
end
EOS
base.extend(ClassMethods)
@ -71,7 +76,7 @@ module CouchRest
base.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def self.define_property(name, options={})
super
auto_generate_validations(properties.last)
auto_generate_validations(properties.last) if properties && properties.size > 0
autovalidation_check = true
end
RUBY_EVAL

View file

@ -14,6 +14,11 @@ module CouchRest
def self.inherited(subklass)
subklass.send(:include, CouchRest::Mixins::Properties)
subklass.class_eval <<-EOS, __FILE__, __LINE__
def self.inherited(subklass)
subklass.properties = self.properties.dup
end
EOS
end
# Accessors

View file

@ -0,0 +1,54 @@
require File.dirname(__FILE__) + '/../../spec_helper'
require File.join(FIXTURE_PATH, 'more', 'card')
# add a default value
Card.property :bg_color, :default => '#ccc'
class BusinessCard < Card
property :extension_code
property :job_title
end
class DesignBusinessCard < BusinessCard
property :bg_color, :default => '#eee'
end
describe "Subclassing an ExtendedDocument" do
before(:each) do
@card = BusinessCard.new
end
it "shouldn't messup the parent's properties" do
Card.properties.should_not == BusinessCard.properties
end
it "should share the same db default" do
@card.database.uri.should == Card.database.uri
end
it "should share the same autovalidation details" do
@card.auto_validation.should be_true
end
it "should have kept the validation details" do
@card.should_not be_valid
end
it "should have added the new validation details" do
validated_fields = @card.class.validators.contexts[:default].map{|v| v.field_name}
validated_fields.should include(:extension_code)
validated_fields.should include(:job_title)
end
it "should inherit default property values" do
@card.bg_color.should == '#ccc'
end
it "should be able to overwrite a default property" do
DesignBusinessCard.new.bg_color.should == '#eee'
end
end