Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Added PropertySet#valid? and refactored code that tests key validity
Browse files Browse the repository at this point in the history
  • Loading branch information
dkubb committed Oct 23, 2009
1 parent 539d861 commit a105712
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/dm-core/associations/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ def valid_collection?(collection)
# @api private
def valid_resource?(resource)
resource.kind_of?(target_model) &&
target_key.zip(target_key.get!(resource)).all? { |property, value| property.valid?(value) }
target_key.valid?(target_key.get!(resource))
end

# TODO: document
Expand Down
6 changes: 6 additions & 0 deletions lib/dm-core/property_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ def loaded?(resource)
all? { |property| property.loaded?(resource) }
end

# TODO: document
# @api semipublic
def valid?(values)
zip(values.nil? ? [] : values).all? { |property, value| property.valid?(value) }
end

# TODO: document
# @api semipublic
def typecast(values)
Expand Down
7 changes: 5 additions & 2 deletions lib/dm-core/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ def repository
def key
return @key if defined?(@key)

key = model.key(repository_name).map do |property|
model_key = model.key(repository_name)

key = model_key.map do |property|
original_attributes[property] || (property.loaded?(self) ? property.get!(self) : nil)
end

@key = key if key.all? { |value| !value.nil? }
# only memoize a valid key
@key = key if model_key.valid?(key)
end

# Checks if this Resource instance is new
Expand Down

0 comments on commit a105712

Please sign in to comment.