-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encapsulate input details in Statement objects (#682)
* Introduce Statement class * Split Statement class for better clarity
- Loading branch information
Showing
3 changed files
with
104 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# frozen_string_literal: true | ||
|
||
module IRB | ||
class Statement | ||
attr_reader :code | ||
|
||
def is_assignment? | ||
raise NotImplementedError | ||
end | ||
|
||
def suppresses_echo? | ||
raise NotImplementedError | ||
end | ||
|
||
def should_be_handled_by_debugger? | ||
raise NotImplementedError | ||
end | ||
|
||
def evaluable_code | ||
raise NotImplementedError | ||
end | ||
|
||
class Expression < Statement | ||
def initialize(code, is_assignment) | ||
@code = code | ||
@is_assignment = is_assignment | ||
end | ||
|
||
def suppresses_echo? | ||
@code.match?(/;\s*\z/) | ||
end | ||
|
||
def should_be_handled_by_debugger? | ||
true | ||
end | ||
|
||
def is_assignment? | ||
@is_assignment | ||
end | ||
|
||
def evaluable_code | ||
@code | ||
end | ||
end | ||
|
||
class Command < Statement | ||
def initialize(code, command, arg, command_class) | ||
@code = code | ||
@command = command | ||
@arg = arg | ||
@command_class = command_class | ||
end | ||
|
||
def is_assignment? | ||
false | ||
end | ||
|
||
def suppresses_echo? | ||
false | ||
end | ||
|
||
def should_be_handled_by_debugger? | ||
IRB::ExtendCommand::DebugCommand > @command_class | ||
end | ||
|
||
def evaluable_code | ||
# Hook command-specific transformation to return valid Ruby code | ||
if @command_class.respond_to?(:transform_args) | ||
arg = @command_class.transform_args(@arg) | ||
else | ||
arg = @arg | ||
end | ||
|
||
[@command, arg].compact.join(' ') | ||
end | ||
end | ||
end | ||
end |