Skip to content

Commit

Permalink
Rubocop: prefer class << self syntax (#95)
Browse files Browse the repository at this point in the history
**Why**

If we want to have private methods, then we have to use `class << self`.
Having all modules use this format consistently makes diffs smaller when
new private methods are added, since we don't need to switch syntax.
  • Loading branch information
mockdeep authored Sep 4, 2024
1 parent 88a156f commit 1c1c0aa
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 197 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Bundler/GemVersion: { EnforcedStyle: forbidden }
Metrics/BlockLength: { Exclude: ["spec/**/*_spec.rb"] }
RSpec/MessageExpectation: { EnforcedStyle: expect }
Style/ClassAndModuleChildren: { EnforcedStyle: compact }
Style/ClassMethodsDefinitions: { EnforcedStyle: self_class }

Style/MethodCallWithArgsParentheses:
AllowedMethods:
Expand Down
30 changes: 16 additions & 14 deletions lib/baes/actions/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

# top-level class to parse options and run the command
module Baes::Actions::Run
# parse options and execute command
def self.call(options)
case options.first
when "bisect"
Baes::Actions::Bisect.call(options[1..].join(" "))
when "rebase"
Baes::Actions::LoadRebaseConfiguration.call(options)
class << self
# parse options and execute command
def call(options)
case options.first
when "bisect"
Baes::Actions::Bisect.call(options[1..].join(" "))
when "rebase"
Baes::Actions::LoadRebaseConfiguration.call(options)

Baes::Actions::Rebase.call
when nil
Baes::Actions::LoadConfiguration.call(["-h"])
when /^-/
Baes::Actions::LoadConfiguration.call(options)
else
abort("Invalid command #{options.inspect}")
Baes::Actions::Rebase.call
when nil
Baes::Actions::LoadConfiguration.call(["-h"])
when /^-/
Baes::Actions::LoadConfiguration.call(options)
else
abort("Invalid command #{options.inspect}")
end
end
end
end
120 changes: 54 additions & 66 deletions lib/baes/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,76 @@

# module to allow configuring various Baes settings
module Baes::Configuration
# return the git wrapper in use, Baes::Git by default
def self.git
@git ||= Baes::Git
end
class << self
# return the git wrapper in use, Baes::Git by default
def git
@git ||= Baes::Git
end

# allow setting the git wrapper, useful in tests
def self.git=(git)
@git = git
end
# allow setting the git wrapper, useful in tests
attr_writer :git

# return the input stream, $stdin by default
def self.input
@input ||= $stdin
end
# return the input stream, $stdin by default
def input
@input ||= $stdin
end

# allow setting the input stream, useful in tests
def self.input=(input)
@input = input
end
# allow setting the input stream, useful in tests
attr_writer :input

# return the output stream, $stdout by default
def self.output
@output ||= $stdout
end
# return the output stream, $stdout by default
def output
@output ||= $stdout
end

# allow setting the output stream, useful in tests
def self.output=(output)
@output = output
end
# allow setting the output stream, useful in tests
attr_writer :output

# return the configured root_name
def self.root_name
@root_name ||=
begin
root = (["main", "master"] & git.branch_names).first
# return the configured root_name
def root_name
@root_name ||=
begin
root = (["main", "master"] & git.branch_names).first

message = "unable to infer root branch, please specify with -r"
raise Baes::Git::GitError, message unless root
message = "unable to infer root branch, please specify with -r"
raise Baes::Git::GitError, message unless root

root
end
end
root
end
end

# allow setting the root_name
def self.root_name=(root_name)
@root_name = root_name
end
# allow setting the root_name
attr_writer :root_name

# return the configured ignored branches
def self.ignored_branch_names
@ignored_branch_names ||= []
end
# return the configured ignored branches
def ignored_branch_names
@ignored_branch_names ||= []
end

# allow setting the ignored branch names
def self.ignored_branch_names=(ignored_branch_names)
@ignored_branch_names = ignored_branch_names
end
# allow setting the ignored branch names
attr_writer :ignored_branch_names

# return whether dry run mode has been enabled
def self.dry_run?
!!@dry_run
end
# return whether dry run mode has been enabled
def dry_run?
!!@dry_run
end

# allow setting dry run mode
def self.dry_run=(dry_run)
@dry_run = dry_run
end
# allow setting dry run mode
attr_writer :dry_run

# return whether auto skip mode has been enabled
def self.auto_skip?
!!@auto_skip
end
# return whether auto skip mode has been enabled
def auto_skip?
!!@auto_skip
end

# allow setting auto skip mode
def self.auto_skip=(auto_skip)
@auto_skip = auto_skip
end
# allow setting auto skip mode
attr_writer :auto_skip

# clear all configuration
def self.reset
instance_variables.each do |ivar|
remove_instance_variable(ivar)
# clear all configuration
def reset
instance_variables.each do |ivar|
remove_instance_variable(ivar)
end
end
end
end
100 changes: 51 additions & 49 deletions lib/baes/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,75 +9,77 @@ module Baes::Git

class GitError < StandardError; end

# checkout git branch and raise error on failure
def self.checkout(branch_name)
stdout, stderr, status = Open3.capture3("git checkout #{branch_name}")
class << self
# checkout git branch and raise error on failure
def checkout(branch_name)
stdout, stderr, status = Open3.capture3("git checkout #{branch_name}")

output.puts(stdout)
output.puts(stderr) unless stderr.empty?
output.puts(stdout)
output.puts(stderr) unless stderr.empty?

return if status.success?
return if status.success?

raise GitError, "failed to rebase on '#{branch_name}'"
end
raise GitError, "failed to rebase on '#{branch_name}'"
end

# rebase current branch on given branch name and return status
def self.rebase(branch_name)
stdout, stderr, status = Open3.capture3("git rebase #{branch_name}")
# rebase current branch on given branch name and return status
def rebase(branch_name)
stdout, stderr, status = Open3.capture3("git rebase #{branch_name}")

output.puts(stdout)
output.puts(stderr) unless stderr.empty?
output.puts(stdout)
output.puts(stderr) unless stderr.empty?

status
end
status
end

# get current branch name and raise on error
def self.current_branch_name
stdout, stderr, status = Open3.capture3("git rev-parse --abbrev-ref HEAD")
# get current branch name and raise on error
def current_branch_name
stdout, stderr, status = Open3.capture3("git rev-parse --abbrev-ref HEAD")

output.puts(stderr) unless stderr.empty?
output.puts(stderr) unless stderr.empty?

raise GitError, "failed to get current branch" unless status.success?
raise GitError, "failed to get current branch" unless status.success?

stdout.strip
end
stdout.strip
end

# list branch names and raise on failure
def self.branch_names
stdout, stderr, status = Open3.capture3("git branch")
# list branch names and raise on failure
def branch_names
stdout, stderr, status = Open3.capture3("git branch")

output.puts(stderr) unless stderr.empty?
output.puts(stderr) unless stderr.empty?

raise GitError, "failed to get branches" unless status.success?
raise GitError, "failed to get branches" unless status.success?

stdout.lines.map { |line| line.sub(/^\*/, "").strip }
end
stdout.lines.map { |line| line.sub(/^\*/, "").strip }
end

# skip current commit during rebase and return status
def self.rebase_skip
stdout, stderr, status = Open3.capture3("git rebase --skip")
# skip current commit during rebase and return status
def rebase_skip
stdout, stderr, status = Open3.capture3("git rebase --skip")

output.puts(stdout)
output.puts(stderr) unless stderr.empty?
output.puts(stdout)
output.puts(stderr) unless stderr.empty?

status
end
status
end

# return the commit number the rebase is currently halted on
def self.next_rebase_step
if Dir.exist?("./.git/rebase-apply")
Integer(File.read("./.git/rebase-apply/next"))
else
Integer(File.read("./.git/rebase-merge/msgnum"))
# return the commit number the rebase is currently halted on
def next_rebase_step
if Dir.exist?("./.git/rebase-apply")
Integer(File.read("./.git/rebase-apply/next"))
else
Integer(File.read("./.git/rebase-merge/msgnum"))
end
end
end

# return the number of commits in the rebase
def self.last_rebase_step
if Dir.exist?("./.git/rebase-apply")
Integer(File.read("./.git/rebase-apply/last"))
else
Integer(File.read("./.git/rebase-merge/end"))
# return the number of commits in the rebase
def last_rebase_step
if Dir.exist?("./.git/rebase-apply")
Integer(File.read("./.git/rebase-apply/last"))
else
Integer(File.read("./.git/rebase-merge/end"))
end
end
end
end
Loading

0 comments on commit 1c1c0aa

Please sign in to comment.