forked from neo4jrb/activegraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation onto ActiveRel from/to class arguments. Don't allow co…
…nstants and make sure model class is ActiveNode. DRYing out into ClassArguments module
- Loading branch information
1 parent
2c2953a
commit 26fd7db
Showing
12 changed files
with
110 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Neo4j | ||
module ClassArguments | ||
class << self | ||
INVALID_CLASS_ARGUMENT_ERROR = 'option must by String, Symbol, false, nil, or an Array of Symbols/Strings' | ||
|
||
def valid_argument?(class_argument) | ||
[NilClass, String, Symbol, FalseClass].include?(class_argument.class) || | ||
(class_argument.is_a?(Array) && class_argument.all? { |c| [Symbol, String].include?(c.class) }) | ||
end | ||
|
||
def validate_argument!(class_argument, context) | ||
return if valid_argument?(class_argument) | ||
|
||
fail ArgumentError, "#{context} #{INVALID_CLASS_ARGUMENT_ERROR} (was #{class_argument.inspect})" | ||
end | ||
|
||
def active_node_model?(class_constant) | ||
class_constant.included_modules.include?(Neo4j::ActiveNode) | ||
end | ||
|
||
def constantize_argument(class_argument) | ||
case class_argument | ||
when 'any', :any, false, nil | ||
nil | ||
when Array | ||
class_argument.map(&method(:constantize_argument)) | ||
else | ||
class_argument.to_s.constantize.tap do |class_constant| | ||
if !active_node_model?(class_constant) | ||
fail ArgumentError, "#{class_constant} is not an ActiveNode model" | ||
end | ||
end | ||
end | ||
rescue NameError | ||
raise ArgumentError, "Could not find class: #{class_argument}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters