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

Commit

Permalink
Added Relationship#child_model? and #parent_model?
Browse files Browse the repository at this point in the history
* It is useful to test whether a model is defined before carrying out
  some actions.
* When comparing models between relationships only compare cases
  where both models are defined.
  • Loading branch information
dkubb committed Jul 2, 2009
1 parent e085aa7 commit 2cab5a0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
16 changes: 16 additions & 0 deletions lib/dm-core/associations/many_to_one.rb
Expand Up @@ -14,10 +14,18 @@ class Relationship < Associations::Relationship
# @api semipublic
alias source_model child_model

# TODO: document
# @api semipublic
alias source_model? child_model?

# TODO: document
# @api semipublic
alias source_key child_key

# TODO: document
# @api semipublic
alias source_key? child_key?

# TODO: document
# @api semipublic
alias target_repository_name parent_repository_name
Expand All @@ -26,10 +34,18 @@ class Relationship < Associations::Relationship
# @api semipublic
alias target_model parent_model

# TODO: document
# @api semipublic
alias target_model? parent_model?

# TODO: document
# @api semipublic
alias target_key parent_key

# TODO: document
# @api semipublic
alias target_key? parent_key?

# TODO: document
# @api semipublic
def nullable?
Expand Down
16 changes: 16 additions & 0 deletions lib/dm-core/associations/one_to_many.rb
Expand Up @@ -10,10 +10,18 @@ class Relationship < Associations::Relationship
# @api semipublic
alias target_model child_model

# TODO: document
# @api semipublic
alias target_model? child_model?

# TODO: document
# @api semipublic
alias target_key child_key

# TODO: document
# @api semipublic
alias target_key? child_key?

# TODO: document
# @api semipublic
alias source_repository_name parent_repository_name
Expand All @@ -22,10 +30,18 @@ class Relationship < Associations::Relationship
# @api semipublic
alias source_model parent_model

# TODO: document
# @api semipublic
alias source_model? parent_model?

# TODO: document
# @api semipublic
alias source_key parent_key

# TODO: document
# @api semipublic
alias source_key? parent_key?

##
# Returns a Collection for this relationship with a given source
#
Expand Down
29 changes: 28 additions & 1 deletion lib/dm-core/associations/relationship.rb
Expand Up @@ -145,6 +145,15 @@ def child_model
raise NameError, "Cannot find the child_model #{child_model_name} for #{parent_model_name} in #{name}"
end

# TODO: document
# @api private
def child_model?
child_model
true
rescue NameError
false
end

# TODO: document
# @api private
def child_model_name
Expand Down Expand Up @@ -197,6 +206,15 @@ def parent_model
raise NameError, "Cannot find the parent_model #{parent_model_name} for #{child_model_name} in #{name}"
end

# TODO: document
# @api private
def parent_model?
parent_model
true
rescue NameError
false
end

# TODO: document
# @api private
def parent_model_name
Expand Down Expand Up @@ -585,7 +603,16 @@ def cmp_repository?(other, type, operator = :==)
# TODO: document
# @api private
def cmp_model?(other, type, operator = :==)
method = "#{type}_model".to_sym
method = "#{type}_model".to_sym
defined = "#{method}?".to_sym

unless send(defined)
return false
end

unless other.send(defined)
return false
end

unless send(method).send(operator, other.send(method))
return false
Expand Down

0 comments on commit 2cab5a0

Please sign in to comment.