Skip to content

Commit

Permalink
Setup the allowed writer method in Model#finalize
Browse files Browse the repository at this point in the history
* This is superior to memoizing the value because Model#finalize should only
  be called when the model has all the properties/methods added to it.
* Models must be finalize before instances being used. This was always always
  the contract, however the specs never enforced that. Now that this change
  is applied Model#finalize *must* be performed before using the initialized
  objects. Fixed the specs to comply with this.
  • Loading branch information
dkubb committed Jul 21, 2011
1 parent 6befbe4 commit 2bcb646
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
32 changes: 19 additions & 13 deletions lib/dm-core/model.rb
Expand Up @@ -135,6 +135,7 @@ def raise_on_save_failure=(raise_on_save_failure)
# @api public
def finalize
finalize_relationships
finalize_allowed_writer_methods
assert_valid_name
assert_valid_properties
assert_valid_key
Expand Down Expand Up @@ -642,6 +643,13 @@ def load(records, query)
# @api semipublic
attr_reader :base_model

# The list of writer methods that can be mass-assigned to in #attributes=
#
# @return [Set]
#
# @api private
attr_reader :allowed_writer_methods

# @api semipublic
def default_repository_name
Repository.default_name
Expand Down Expand Up @@ -701,19 +709,6 @@ def repositories
[ repository ].to_set + @properties.keys.map { |repository_name| DataMapper.repository(repository_name) }
end

# The list of writer methods that can be mass-assigned to in #attributes=
#
# @return [Set]
#
# @api private
def allowed_writer_methods
@allowed_writer_methods ||= begin
methods = public_instance_methods.map { |method| method.to_s }.grep(WRITER_METHOD_REGEXP).to_set
methods -= INVALID_WRITER_METHODS
methods.freeze
end
end

private

# @api private
Expand Down Expand Up @@ -787,6 +782,17 @@ def finalize_relationships
relationships(repository_name).each { |relationship| relationship.finalize }
end

# Initialize the list of allowed writer methods
#
# @return [undefined]
#
# @api private
def finalize_allowed_writer_methods
@allowed_writer_methods = public_instance_methods.map { |method| method.to_s }.grep(WRITER_METHOD_REGEXP).to_set
@allowed_writer_methods -= INVALID_WRITER_METHODS
@allowed_writer_methods.freeze
end

# @api private
# TODO: Remove this once appropriate warnings can be added.
def assert_valid(force = false) # :nodoc:
Expand Down
2 changes: 2 additions & 0 deletions spec/semipublic/associations/many_to_many_spec.rb
Expand Up @@ -54,6 +54,8 @@ class WithDefaultCallable
@article_model = Blog::Article
@author_model = Blog::Author

DataMapper.finalize

n = @article_model.n

@default_value = [ @author_model.new(:name => 'Dan Kubb') ]
Expand Down
2 changes: 2 additions & 0 deletions spec/semipublic/associations/many_to_one_spec.rb
Expand Up @@ -23,6 +23,8 @@ class ::Comment
@user_model = User
@comment_model = Comment

DataMapper.finalize

@default_value = @user_model.new(:name => 'dkubb', :age => 34, :description => 'Test')
@default_value_callable = @user_model.new(:name => 'jdoe', :age => 21, :description => 'Test')

Expand Down
2 changes: 2 additions & 0 deletions spec/semipublic/associations/one_to_many_spec.rb
Expand Up @@ -24,6 +24,8 @@ class Author
@article_model = Blog::Article
@author_model = Blog::Author

DataMapper.finalize

n = @article_model.n

@default_value = [ @author_model.new(:name => 'Dan Kubb') ]
Expand Down
2 changes: 2 additions & 0 deletions spec/semipublic/associations/one_to_one_spec.rb
Expand Up @@ -24,6 +24,8 @@ class Author
@article_model = Blog::Article
@author_model = Blog::Author

DataMapper.finalize

@default_value = @author_model.new(:name => 'Dan Kubb')
@default_value_callable = @author_model.new(:name => 'John Doe')

Expand Down

0 comments on commit 2bcb646

Please sign in to comment.