class Reviewer::Command

The core funtionality to translate a tool, command type, and verbosity into a runnable command

Constants

SEED_SUBSTITUTION_VALUE

Attributes

tool[R]
type[R]

Public Class Methods

new(tool, type, verbosity = Verbosity::TOTAL_SILENCE) click to toggle source

Creates an instance of the Command class to synthesize a command string using the tool, command type, and verbosity. @param tool [Tool, Symbol] a tool or tool key to use to look up the command and options @param type [Symbol] the desired command type (:install, :prepare, :review, :format) @param verbosity = Verbosity::TOTAL_SILENCE [Symbol] the desired verbosity for the command

@return [Command] the intersection of a tool, command type, and verbosity

# File lib/reviewer/command.rb, line 22
def initialize(tool, type, verbosity = Verbosity::TOTAL_SILENCE)
  @tool = Tool(tool)
  @type = type.to_sym
  @verbosity = Verbosity(verbosity)
end

Public Instance Methods

seed() click to toggle source

Generates a seed that can be re-used across runs so that the results are consistent across related runs for tools that would otherwise change the seed automatically every run. Since not all tools will use the seed, there's no need to generate it in the initializer. Instead, it's memoized if it's used.

@return [Integer] a random integer to pass to tools that use seeds

# File lib/reviewer/command.rb, line 62
def seed
  @seed ||= Random.rand(100_000)

  # Store the seed for reference
  Reviewer.history.set(tool.key, :last_seed, @seed)

  @seed
end
string() click to toggle source

The final command string with all of the conditions appled

@return [String] the final, valid command string to run

# File lib/reviewer/command.rb, line 31
def string
  @string ||= seed_substitution? ? seeded_string : raw_string
end
Also aliased as: to_s
to_s()
Alias for: string
verbosity() click to toggle source

Getter for @verbosity. Since the setter is custom, the getter needs to be explicitly declared. Otherwise, using `attr_accessor` and then overriding the setter muddies the waters.

@return [Verbosity] the current verbosity setting for the command

# File lib/reviewer/command.rb, line 40
def verbosity # rubocop:disable Style/TrivialAccessors
  @verbosity
end
verbosity=(verbosity) click to toggle source

Override verbosity assignment to clear the related memoized values when verbosity changes @param verbosity [Verbosity, Symbol] the desired verbosity for the command

@return [Verbosity] the updated verbosity level for the command

# File lib/reviewer/command.rb, line 48
def verbosity=(verbosity)
  # Unmemoize string since the verbosity has been changed
  @raw_string = nil
  @string = nil

  @verbosity = Verbosity(verbosity)
end

Private Instance Methods

raw_string() click to toggle source

The raw command string before any substitutions. For example, since seeds need to remain consistent from one run to the next, they're

@return [type] [description]

# File lib/reviewer/command.rb, line 77
def raw_string
  @raw_string ||= String.new(
    type,
    tool_settings: tool.settings,
    verbosity: verbosity
  ).to_s
end
seed_substitution?() click to toggle source

Determines if the raw command string has a SEED_SUBSTITUTION_VALUE that needs replacing

@return [Boolean] true if the raw command string contains the SEED_SUBSTITUTION_VALUE

# File lib/reviewer/command.rb, line 96
def seed_substitution?
  raw_string.include?(SEED_SUBSTITUTION_VALUE)
end
seeded_string() click to toggle source

The version of the command with the SEED_SUBSTITUTION_VALUE replaced

@return [String] the command string with the SEED_SUBSTITUTION_VALUE replaced

# File lib/reviewer/command.rb, line 88
def seeded_string
  # Update the string with the memoized seed value
  raw_string.gsub(SEED_SUBSTITUTION_VALUE, seed.to_s)
end