Class: Reviewer::Arguments::Keywords

Inherits:
Object
  • Object
show all
Defined in:
lib/reviewer/arguments/keywords.rb

Overview

Handles interpreting all ‘leftover’ arguments and translating them to file-related, tag-related, or tool-related arguments

Constant Summary collapse

RESERVED =
%w[staged].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*provided) ⇒ self

Generates an instance of parsed keywords from the provided arguments

Parameters:

  • *provided (Array<String>)

    the leftover (non-flag) arguments from the command line



21
22
23
# File 'lib/reviewer/arguments/keywords.rb', line 21

def initialize(*provided)
  @provided = Array(provided.flatten)
end

Instance Attribute Details

#providedArray<String> Also known as: raw

Returns the keywords extracted from the command-line arguments.

Returns:

  • (Array<String>)

    the keywords extracted from the command-line arguments



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/reviewer/arguments/keywords.rb', line 10

class Keywords
  RESERVED = %w[staged].freeze

  attr_accessor :provided

  alias raw provided

  # Generates an instance of parsed keywords from the provided arguments
  # @param *provided [Array<String>] the leftover (non-flag) arguments from the command line
  #
  # @return [self]
  def initialize(*provided)
    @provided = Array(provided.flatten)
  end

  # Proves the full list of raw keyword arguments explicitly passed via command-line as an array
  #
  # @return [Array] full collection of the provided keyword arguments as a string
  def to_a
    provided
  end

  # Provides the full list of raw keyword arguments explicitly passed via command-line as a
  #   comma-separated string
  #
  # @return [String] comma-separated list of the file arguments as a string
  def to_s
    to_a.join(',')
  end

  # Summary of the state of keyword arguments based on how Reviewer parsed them
  #
  # @return [Hash] represents the summary of the keyword values parsed from the command-line and
  #   grouped based on how they were parsed
  def to_h
    {
      provided: provided,
      recognized: recognized,
      unrecognized: unrecognized,
      reserved: reserved,
      for_tags: for_tags,
      for_tool_names: for_tool_names
    }
  end
  alias inspect to_h

  # Extracts reserved keywords from the provided arguments
  #
  # @return [Array<String>] intersection of provided arguments and reserved keywords
  def reserved
    intersection_with RESERVED
  end

  # Extracts keywords that match configured tags for enabled tools
  #
  # @return [Array<String>] intersection of provided arguments and configured tags for tools
  def for_tags
    intersection_with configured_tags
  end

  # Extracts keywords that match configured tool keys
  #
  # @return [Array<String>] intersection of provided arguments and configured tool names
  def for_tool_names
    intersection_with configured_tool_names
  end

  # Extracts keywords that match any possible recognized keyword values
  #
  # @return [Array<String>] intersection of provided arguments and recognizable keywords
  def recognized
    intersection_with possible
  end

  # Extracts keywords that don't match any possible recognized keyword values
  #
  # @return [Array<String>] leftover keywords that weren't recognized
  def unrecognized
    (provided - recognized).uniq.sort
  end

  # Provides the complete list of all recognized keywords based on configuration
  #
  # @return [Array<String>] all keywords that Reviewer can recognized
  def possible
    (RESERVED + configured_tags + configured_tool_names).uniq.sort
  end

  # Provides the complete list of all configured tags for enabled tools
  #
  # @return [Array<String>] all unique configured tags
  def configured_tags
    tools.enabled.map(&:tags).flatten.uniq.sort
  end

  # Provides the complete list of all configured tool names for enabled tools
  #
  # @return [Array<String>] all unique configured tools
  def configured_tool_names
    # We explicitly don't sort the tool names list because Reviewer uses the configuration order
    # to determine the execution order. So not sorting maintains the predicted order it will run
    # in and leaves the option to sort to the consuming code if needed
    tools.all.map { |tool| tool.key.to_s }
  end

  private

  # Provides a collection of enabled Tools for convenient access
  #
  # @return [Array<Reviewer::Tool>] collection of all currently enabled tools
  def tools
    @tools ||= Reviewer.tools
  end

  # Syntactic sugar for finding intersections with valid keywords
  # @param values [Array<String>] the collection to use for finding intersecting values
  #
  # @return [Array<String>] the list of intersecting values
  def intersection_with(values)
    (values & provided).uniq.sort
  end
end

Instance Method Details

#configured_tagsArray<String>

Provides the complete list of all configured tags for enabled tools

Returns:

  • (Array<String>)

    all unique configured tags



101
102
103
# File 'lib/reviewer/arguments/keywords.rb', line 101

def configured_tags
  tools.enabled.map(&:tags).flatten.uniq.sort
end

#configured_tool_namesArray<String>

Provides the complete list of all configured tool names for enabled tools

Returns:

  • (Array<String>)

    all unique configured tools



108
109
110
111
112
113
# File 'lib/reviewer/arguments/keywords.rb', line 108

def configured_tool_names
  # We explicitly don't sort the tool names list because Reviewer uses the configuration order
  # to determine the execution order. So not sorting maintains the predicted order it will run
  # in and leaves the option to sort to the consuming code if needed
  tools.all.map { |tool| tool.key.to_s }
end

#for_tagsArray<String>

Extracts keywords that match configured tags for enabled tools

Returns:

  • (Array<String>)

    intersection of provided arguments and configured tags for tools



66
67
68
# File 'lib/reviewer/arguments/keywords.rb', line 66

def for_tags
  intersection_with configured_tags
end

#for_tool_namesArray<String>

Extracts keywords that match configured tool keys

Returns:

  • (Array<String>)

    intersection of provided arguments and configured tool names



73
74
75
# File 'lib/reviewer/arguments/keywords.rb', line 73

def for_tool_names
  intersection_with configured_tool_names
end

#possibleArray<String>

Provides the complete list of all recognized keywords based on configuration

Returns:

  • (Array<String>)

    all keywords that Reviewer can recognized



94
95
96
# File 'lib/reviewer/arguments/keywords.rb', line 94

def possible
  (RESERVED + configured_tags + configured_tool_names).uniq.sort
end

#recognizedArray<String>

Extracts keywords that match any possible recognized keyword values

Returns:

  • (Array<String>)

    intersection of provided arguments and recognizable keywords



80
81
82
# File 'lib/reviewer/arguments/keywords.rb', line 80

def recognized
  intersection_with possible
end

#reservedArray<String>

Extracts reserved keywords from the provided arguments

Returns:

  • (Array<String>)

    intersection of provided arguments and reserved keywords



59
60
61
# File 'lib/reviewer/arguments/keywords.rb', line 59

def reserved
  intersection_with RESERVED
end

#to_aArray

Proves the full list of raw keyword arguments explicitly passed via command-line as an array

Returns:

  • (Array)

    full collection of the provided keyword arguments as a string



28
29
30
# File 'lib/reviewer/arguments/keywords.rb', line 28

def to_a
  provided
end

#to_hHash Also known as: inspect

Summary of the state of keyword arguments based on how Reviewer parsed them

Returns:

  • (Hash)

    represents the summary of the keyword values parsed from the command-line and grouped based on how they were parsed



44
45
46
47
48
49
50
51
52
53
# File 'lib/reviewer/arguments/keywords.rb', line 44

def to_h
  {
    provided: provided,
    recognized: recognized,
    unrecognized: unrecognized,
    reserved: reserved,
    for_tags: for_tags,
    for_tool_names: for_tool_names
  }
end

#to_sString

Provides the full list of raw keyword arguments explicitly passed via command-line as a

comma-separated string

Returns:

  • (String)

    comma-separated list of the file arguments as a string



36
37
38
# File 'lib/reviewer/arguments/keywords.rb', line 36

def to_s
  to_a.join(',')
end

#unrecognizedArray<String>

Extracts keywords that don’t match any possible recognized keyword values

Returns:

  • (Array<String>)

    leftover keywords that weren’t recognized



87
88
89
# File 'lib/reviewer/arguments/keywords.rb', line 87

def unrecognized
  (provided - recognized).uniq.sort
end