-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update validators to use consistent initializer sig
- Loading branch information
Showing
3 changed files
with
76 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
class TurboBoost::Commands::CommandValidator | ||
def initialize(class_name, method_name) | ||
@class_name = class_name | ||
def initialize(command, method_name) | ||
@command = command | ||
@method_name = method_name.to_sym | ||
@command_class = class_name&.safe_constantize | ||
end | ||
|
||
attr_reader :class_name, :method_name, :command_class | ||
attr_reader :command, :method_name | ||
|
||
def validate | ||
valid_class? && valid_method? | ||
end | ||
alias_method :valid?, :validate | ||
|
||
def validate! | ||
message = "`#{class_name}` is not a subclass of `#{TurboBoost::Commands::Command.name}`!" | ||
raise TurboBoost::Commands::InvalidClassError.new(message, command: command_class) unless valid_class? | ||
message = "`#{command.class.name}` is not a subclass of `#{TurboBoost::Commands::Command.name}`!" | ||
raise TurboBoost::Commands::InvalidClassError.new(message, command: command.class) unless valid_class? | ||
|
||
message = "`#{command_class.name}` does not define the public method `#{method_name}`!" | ||
raise TurboBoost::Commands::InvalidMethodError.new(message, command: command_class) unless valid_method? | ||
message = "`#{command.class.name}` does not define the public method `#{method_name}`!" | ||
raise TurboBoost::Commands::InvalidMethodError.new(message, command: command.class) unless valid_method? | ||
|
||
true | ||
end | ||
|
||
private | ||
|
||
def command_ancestors | ||
range = 0..(command_class&.ancestors&.index(TurboBoost::Commands::Command).to_i - 1) | ||
command_class&.ancestors&.[](range) || [] | ||
range = 0..(command.class&.ancestors&.index(TurboBoost::Commands::Command).to_i - 1) | ||
command.class&.ancestors&.[](range) || [] | ||
end | ||
|
||
def valid_class? | ||
command_class&.ancestors&.include? TurboBoost::Commands::Command | ||
command.class&.ancestors&.include? TurboBoost::Commands::Command | ||
end | ||
|
||
def valid_method? | ||
return false unless valid_class? | ||
return false unless command_ancestors.any? { |a| a.public_instance_methods(false).any? method_name } | ||
|
||
method = command_class.public_instance_method(method_name) | ||
method = command.class.public_instance_method(method_name) | ||
method&.parameters&.none? | ||
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