Permalink
Browse files

Fixed autotest support, corrected some issues with reloading models.

  • Loading branch information...
1 parent 91b823b commit ffa83a43172bbfbd848d2a32698030da40732aec @sam sam committed Jun 26, 2008
Showing with 74 additions and 67 deletions.
  1. +3 −58 lib/dm-core.rb
  2. +35 −3 lib/dm-core/model.rb
  3. +26 −1 lib/dm-core/resource.rb
  4. +10 −5 spec/unit/model_spec.rb
View
@@ -31,63 +31,10 @@
dir = Pathname(__FILE__).dirname.expand_path / 'dm-core'
-# Define an interface for plugins to hook into. I have to add this code up
-# here because it needs to be available to all the files.
-module DataMapper
- module Resource
- @@extra_inclusions = []
-
-
- ##
- #
- # Appends a module for inclusion into the model class after
- # DataMapper::Resource.
- #
- # This is a useful way to extend DataMapper::Resource while still retaining
- # a self.included method.
- #
- # @param [Module] inclusion the module that is to be appended to the module
- # after DataMapper::Resource
- #
- # @return [TrueClass, FalseClass] whether or not the inclusions have been
- # successfully appended to the list
- # @return <TrueClass, FalseClass>
- #-
- # @api public
- def self.append_inclusions(*inclusions)
- @@extra_inclusions.concat inclusions
- true
- end
- end # module Resource
-
- module Model
- @@extra_extensions = []
-
- ##
- #
- # Extends the model with this module after DataMapper::Resource has been
- # included.
- #
- # This is a useful way to extend DataMapper::Model while
- # still retaining a self.extended method.
- #
- # @param [Module] extensions the module that is to be extend the model after
- # after DataMapper::Model
- #
- # @return [TrueClass, FalseClass] whether or not the inclusions have been
- # successfully appended to the list
- #-
- # @api public
- #
- # TODO: Move this do DataMapper::Model when DataMapper::Model is created
- def self.append_extensions(*extensions)
- @@extra_extensions.concat extensions
- true
- end
- end # module Model
-end # module DataMapper
-
require dir / 'support'
+require dir / 'resource'
+require dir / 'model'
+
require dir / 'type'
require dir / 'type_map'
require dir / 'types'
@@ -97,13 +44,11 @@ def self.append_extensions(*extensions)
require dir / 'identity_map'
require dir / 'logger'
require dir / 'migrator'
-require dir / 'model'
require dir / 'naming_conventions'
require dir / 'property_set'
require dir / 'query'
require dir / 'transaction'
require dir / 'repository'
-require dir / 'resource'
require dir / 'scope'
require dir / 'property'
require dir / 'adapters'
View
@@ -1,9 +1,35 @@
module DataMapper
module Model
+ ##
+ #
+ # Extends the model with this module after DataMapper::Resource has been
+ # included.
+ #
+ # This is a useful way to extend DataMapper::Model while
+ # still retaining a self.extended method.
+ #
+ # @param [Module] extensions the module that is to be extend the model after
+ # after DataMapper::Model
+ #
+ # @return [TrueClass, FalseClass] whether or not the inclusions have been
+ # successfully appended to the list
+ #-
+ # @api public
+ #
+ # TODO: Move this do DataMapper::Model when DataMapper::Model is created
+ def self.append_extensions(*extensions)
+ extra_extensions.concat extensions
+ true
+ end
+
+ def self.extra_extensions
+ @extra_extensions ||= []
+ end
+
def self.extended(model)
model.instance_variable_set(:@storage_names, Hash.new { |h,k| h[k] = repository(k).adapter.resource_naming_convention.call(model.instance_eval { default_storage_name }) })
model.instance_variable_set(:@properties, Hash.new { |h,k| h[k] = k == Repository.default_name ? PropertySet.new : h[Repository.default_name].dup })
- @@extra_extensions.each { |extension| model.extend(extension) }
+ extra_extensions.each { |extension| model.extend(extension) }
end
def inherited(target)
@@ -321,8 +347,14 @@ def #{property.name}=(value)
end
end
- def method_missing(method, *args, &block)
- raise NoMethodError.new("#{self.name} does not respond to :relationships") unless self.respond_to?(:relationships)
+ def relationships(*args)
+ # DO NOT REMOVE!
+ # method_missing depends on these existing. Without this stub,
+ # a missing module can cause misleading recursive errors.
+ raise NotImplementedError.new
+ end
+
+ def method_missing(method, *args, &block)
if relationship = self.relationships(repository.name)[method]
klass = self == relationship.child_model ? relationship.parent_model : relationship.child_model
return DataMapper::Query::Path.new(repository, [ relationship ], klass)
View
@@ -2,6 +2,31 @@
module DataMapper
module Resource
+ ##
+ #
+ # Appends a module for inclusion into the model class after
+ # DataMapper::Resource.
+ #
+ # This is a useful way to extend DataMapper::Resource while still retaining
+ # a self.included method.
+ #
+ # @param [Module] inclusion the module that is to be appended to the module
+ # after DataMapper::Resource
+ #
+ # @return [TrueClass, FalseClass] whether or not the inclusions have been
+ # successfully appended to the list
+ # @return <TrueClass, FalseClass>
+ #-
+ # @api public
+ def self.append_inclusions(*inclusions)
+ extra_inclusions.concat inclusions
+ true
+ end
+
+ def self.extra_inclusions
+ @extra_inclusions ||= []
+ end
+
include Assertions
# When Resource is included in a class this method makes sure
@@ -13,7 +38,7 @@ def self.included(model)
model.extend Model
model.extend ClassMethods if defined?(ClassMethods)
model.const_set('Resource', self) unless model.const_defined?('Resource')
- @@extra_inclusions.each { |inclusion| model.send(:include, inclusion) }
+ extra_inclusions.each { |inclusion| model.send(:include, inclusion) }
descendants << model
end
View
@@ -201,10 +201,15 @@ class GasGiant < ModelSpec::Resource
end
describe '#append_inclusions' do
+ before(:all) do
+ @standard_resource_inclusions = DataMapper::Resource.instance_variable_get('@extra_inclusions')
+ @standard_model_extensions = DataMapper::Model.instance_variable_get('@extra_extensions')
+ end
+
before(:each) do
- DataMapper::Resource.send(:class_variable_set, '@@extra_inclusions', [])
- DataMapper::Model.send(:class_variable_set, '@@extra_extensions', [])
-
+ DataMapper::Resource.instance_variable_set('@extra_inclusions', [])
+ DataMapper::Model.instance_variable_set('@extra_extensions', [])
+
@module = Module.new do
def greet
hi_mom!
@@ -226,8 +231,8 @@ def hello
end
after(:each) do
- DataMapper::Resource.send(:class_variable_set, '@@extra_inclusions', [])
- DataMapper::Model.send(:class_variable_set, '@@extra_extensions', [])
+ DataMapper::Resource.instance_variable_set('@extra_inclusions', @standard_resource_inclusions)
+ DataMapper::Model.instance_variable_set('@extra_extensions', @standard_model_extensions)
end
it "should append the module to be included in resources" do

0 comments on commit ffa83a4

Please sign in to comment.