-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Edits to keep code DRY #111
Conversation
JCook21
commented
Apr 20, 2014
- Overrode say_status and system methods to put checks for options[:quiet] and options[:pretend] in one place.
- Edits to make sure that shell.say_status is not called since this does not invoke the overridden say_status method. Edits to tests as a result of this too.
- Removed all uses of 'unless options[:pretend]' and 'unless options[:quiet]' since this is now handled in the overridden methods.
@@ -2,6 +2,14 @@ | |||
class Homesick | |||
# Various utility methods that are used by Homesick | |||
module Utils | |||
def say_status(*args) | |||
super unless options[:quiet] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this try to extract options out of *args
too? That would allow one-off programmatically quiet command, versus globally with --quiet
. Same idea applies to system below.
Definitely cleans things up nicely 👍 |
@technicalpickles I've added some code to allow a final boolean argument to be passed to both say_status and system. I'm not sure if there's a better way to implement this than I've done though. I'd appreciate any comments. |
def system(*args) | ||
pretend = %w(TrueClass FalseClass).include?(args.last.class) ? args.pop : false | ||
super(*args) unless options[:pretend] || pretend | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm thinking that say_status
and the system
methods could be refactored a tiny bit like this:
def boolean? object # it might be best to make this method private or protected
object == true || object == false
end
def say_status(*args)
quiet = args.count == 4 && boolean?(args.last) && args.pop
super(*args) unless options[:quiet] || quiet
end
def system(*args)
pretend = boolean?(args.last) && args.pop
super(*args) unless options[:pretend] || pretend
end
Also, it would probably help to add some brief documentation comments for these methods either way. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nicolasmccurdy I like that, it's neater than what I came up with. I'll add a commit with that.
Looks good. 👍 |
I just realized that due to #74 (and especially #101, which has already been merged), multiple methods would be used to avoid shell calls instead of simply calling I'm thinking about these options:
|
@technicalpickles @nicolasmccurdy I've just had another stab at this, using meta-programming to dynamically define methods that should be 'pretendable' or 'quietable'. A couple of points about this:
|
I've just realised that |
This really isn't working as intended. The way I'm trying to pass a boolean value as the last argument to allow the 'quietable' or 'pretendable' behavior is not working at all. I'll take another look at what we need to achieve and how I'm trying to do it and will reopen this PR then. |
I've given this some more thought have decided to reopen this. The approach I was trying was to try to detect if a final boolean argument was being passed and to use that to set the pretendable or quietable behaviour. I don't think this is going to work due to the fact that many methods can have an indeterminate number of arguments (e.g. system). I think it may be better to keep this simple and just look in the options to see if the pretendable or quietable behaviour should be invoked. This could also be done programmatically by manually setting the option before calling the method that's being overridden, giving us the behaviour that @technicalpickles suggested. Let me know if you can think of a better way to achieve this. |
👍 Looks good to me. By the way, you might want to merge/rebase on master now that #110 is merged. |
I just rebased on master and I think I've fixed the issues that came about as a result of this. @nicolasmccurdy could you have another look now? |
options, skipping their default behaviour if so.
Any more comments on this or should I go ahead and merge it? |
Merge it! |
Merged! |