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

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