murb/workbook

View on GitHub
lib/workbook/template.rb

Summary

Maintainability
A
25 mins
Test Coverage

Method has too many lines. [11/10]
Open

    def create_or_find_format_by name, variant = :default
      fs = @formats[name]
      fs = @formats[name] = {} if fs.nil?
      f = fs[variant]
      if f.nil?
Severity: Minor
Found in lib/workbook/template.rb by rubocop

This cop checks if the length of a method exceeds some maximum value. Comment lines can optionally be ignored. The maximum allowed length is configurable.

You can set literals you want to fold with CountAsOne. Available are: 'array', 'hash', and 'heredoc'. Each literal will be counted as one line regardless of its actual size.

NOTE: The ExcludedMethods configuration is deprecated and only kept for backwards compatibility. Please use IgnoredMethods instead.

Example: CountAsOne: ['array', 'heredoc']

def m
  array = [       # +1
    1,
    2
  ]

  hash = {        # +3
    key: 'value'
  }

  <<~HEREDOC      # +1
    Heredoc
    content.
  HEREDOC
end               # 5 points

Method create_or_find_format_by has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
Open

    def create_or_find_format_by name, variant = :default
      fs = @formats[name]
      fs = @formats[name] = {} if fs.nil?
      f = fs[variant]
      if f.nil?
Severity: Minor
Found in lib/workbook/template.rb - About 25 mins to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Line is too long. [196/120]
Open

  # Workbook::Template is a container for different Workbook::Format's and the storage of raw template data that isn't really supported by Workbook, but should survive a typical read/write cyclus.
Severity: Minor
Found in lib/workbook/template.rb by rubocop

This cop checks the length of lines in the source code. The maximum length is configurable. The tab size is configured in the IndentationWidth of the Layout/IndentationStyle cop. It also ignores a shebang line by default.

This cop has some autocorrection capabilities. It can programmatically shorten certain long lines by inserting line breaks into expressions that can be safely split across lines. These include arrays, hashes, and method calls with argument lists.

If autocorrection is enabled, the following Layout cops are recommended to further format the broken lines. (Many of these are enabled by default.)

  • ArgumentAlignment
  • BlockAlignment
  • BlockDelimiters
  • BlockEndNewline
  • ClosingParenthesisIndentation
  • FirstArgumentIndentation
  • FirstArrayElementIndentation
  • FirstHashElementIndentation
  • FirstParameterIndentation
  • HashAlignment
  • IndentationWidth
  • MultilineArrayLineBreaks
  • MultilineBlockLayout
  • MultilineHashBraceLayout
  • MultilineHashKeyLineBreaks
  • MultilineMethodArgumentLineBreaks
  • ParameterAlignment

Together, these cops will pretty print hashes, arrays, method calls, etc. For example, let's say the max columns is 25:

Example:

# bad
{foo: "0000000000", bar: "0000000000", baz: "0000000000"}

# good
{foo: "0000000000",
bar: "0000000000", baz: "0000000000"}

# good (with recommended cops enabled)
{
  foo: "0000000000",
  bar: "0000000000",
  baz: "0000000000",
}

Line is too long. [140/120]
Open

    # @param [String] name of the format (e.g. whatever you want, in diff names such as 'destroyed', 'updated' and 'created' are being used)
Severity: Minor
Found in lib/workbook/template.rb by rubocop

This cop checks the length of lines in the source code. The maximum length is configurable. The tab size is configured in the IndentationWidth of the Layout/IndentationStyle cop. It also ignores a shebang line by default.

This cop has some autocorrection capabilities. It can programmatically shorten certain long lines by inserting line breaks into expressions that can be safely split across lines. These include arrays, hashes, and method calls with argument lists.

If autocorrection is enabled, the following Layout cops are recommended to further format the broken lines. (Many of these are enabled by default.)

  • ArgumentAlignment
  • BlockAlignment
  • BlockDelimiters
  • BlockEndNewline
  • ClosingParenthesisIndentation
  • FirstArgumentIndentation
  • FirstArrayElementIndentation
  • FirstHashElementIndentation
  • FirstParameterIndentation
  • HashAlignment
  • IndentationWidth
  • MultilineArrayLineBreaks
  • MultilineBlockLayout
  • MultilineHashBraceLayout
  • MultilineHashKeyLineBreaks
  • MultilineMethodArgumentLineBreaks
  • ParameterAlignment

Together, these cops will pretty print hashes, arrays, method calls, etc. For example, let's say the max columns is 25:

Example:

# bad
{foo: "0000000000", bar: "0000000000", baz: "0000000000"}

# good
{foo: "0000000000",
bar: "0000000000", baz: "0000000000"}

# good (with recommended cops enabled)
{
  foo: "0000000000",
  bar: "0000000000",
  baz: "0000000000",
}

Use def with parentheses when there are parameters.
Open

    def create_or_find_format_by name, variant = :default
Severity: Minor
Found in lib/workbook/template.rb by rubocop

This cop checks for parentheses around the arguments in method definitions. Both instance and class/singleton methods are checked.

This cop does not consider endless methods, since parentheses are always required for them.

Example: EnforcedStyle: require_parentheses (default)

# The `require_parentheses` style requires method definitions
# to always use parentheses

# bad
def bar num1, num2
  num1 + num2
end

def foo descriptive_var_name,
        another_descriptive_var_name,
        last_descriptive_var_name
  do_something
end

# good
def bar(num1, num2)
  num1 + num2
end

def foo(descriptive_var_name,
        another_descriptive_var_name,
        last_descriptive_var_name)
  do_something
end

Example: EnforcedStyle: requirenoparentheses

# The `require_no_parentheses` style requires method definitions
# to never use parentheses

# bad
def bar(num1, num2)
  num1 + num2
end

def foo(descriptive_var_name,
        another_descriptive_var_name,
        last_descriptive_var_name)
  do_something
end

# good
def bar num1, num2
  num1 + num2
end

def foo descriptive_var_name,
        another_descriptive_var_name,
        last_descriptive_var_name
  do_something
end

Example: EnforcedStyle: requirenoparenthesesexceptmultiline

# The `require_no_parentheses_except_multiline` style prefers no
# parentheses when method definition arguments fit on single line,
# but prefers parentheses when arguments span multiple lines.

# bad
def bar(num1, num2)
  num1 + num2
end

def foo descriptive_var_name,
        another_descriptive_var_name,
        last_descriptive_var_name
  do_something
end

# good
def bar num1, num2
  num1 + num2
end

def foo(descriptive_var_name,
        another_descriptive_var_name,
        last_descriptive_var_name)
  do_something
end

Use def with parentheses when there are parameters.
Open

    def add_format format
Severity: Minor
Found in lib/workbook/template.rb by rubocop

This cop checks for parentheses around the arguments in method definitions. Both instance and class/singleton methods are checked.

This cop does not consider endless methods, since parentheses are always required for them.

Example: EnforcedStyle: require_parentheses (default)

# The `require_parentheses` style requires method definitions
# to always use parentheses

# bad
def bar num1, num2
  num1 + num2
end

def foo descriptive_var_name,
        another_descriptive_var_name,
        last_descriptive_var_name
  do_something
end

# good
def bar(num1, num2)
  num1 + num2
end

def foo(descriptive_var_name,
        another_descriptive_var_name,
        last_descriptive_var_name)
  do_something
end

Example: EnforcedStyle: requirenoparentheses

# The `require_no_parentheses` style requires method definitions
# to never use parentheses

# bad
def bar(num1, num2)
  num1 + num2
end

def foo(descriptive_var_name,
        another_descriptive_var_name,
        last_descriptive_var_name)
  do_something
end

# good
def bar num1, num2
  num1 + num2
end

def foo descriptive_var_name,
        another_descriptive_var_name,
        last_descriptive_var_name
  do_something
end

Example: EnforcedStyle: requirenoparenthesesexceptmultiline

# The `require_no_parentheses_except_multiline` style prefers no
# parentheses when method definition arguments fit on single line,
# but prefers parentheses when arguments span multiple lines.

# bad
def bar(num1, num2)
  num1 + num2
end

def foo descriptive_var_name,
        another_descriptive_var_name,
        last_descriptive_var_name
  do_something
end

# good
def bar num1, num2
  num1 + num2
end

def foo(descriptive_var_name,
        another_descriptive_var_name,
        last_descriptive_var_name)
  do_something
end

Use a guard clause (raise ArgumentError, "format should be a Workboot::Format" unless format.is_a? Workbook::Format) instead of wrapping the code inside a conditional expression.
Open

      if format.is_a? Workbook::Format
Severity: Minor
Found in lib/workbook/template.rb by rubocop

Use a guard clause instead of wrapping the code inside a conditional expression

Example:

# bad
def test
  if something
    work
  end
end

# good
def test
  return unless something

  work
end

# also good
def test
  work if something
end

# bad
if something
  raise 'exception'
else
  ok
end

# good
raise 'exception' if something
ok

# bad
if something
  foo || raise('exception')
else
  ok
end

# good
foo || raise('exception') if something
ok

Prefer single-quoted strings when you don't need string interpolation or special symbols.
Open

require "workbook/modules/raw_objects_storage"
Severity: Minor
Found in lib/workbook/template.rb by rubocop

Checks if uses of quotes match the configured preference.

Example: EnforcedStyle: single_quotes (default)

# bad
"No special symbols"
"No string interpolation"
"Just text"

# good
'No special symbols'
'No string interpolation'
'Just text'
"Wait! What's #{this}!"

Example: EnforcedStyle: double_quotes

# bad
'Just some text'
'No special chars or interpolation'

# good
"Just some text"
"No special chars or interpolation"
"Every string in #{project} uses double_quotes"

end at 53, 8 is not aligned with if at 49, 34.
Open

        end
Severity: Minor
Found in lib/workbook/template.rb by rubocop

This cop checks whether the end keywords are aligned properly.

Three modes are supported through the EnforcedStyleAlignWith configuration parameter:

If it's set to keyword (which is the default), the end shall be aligned with the start of the keyword (if, class, etc.).

If it's set to variable the end shall be aligned with the left-hand-side of the variable assignment, if there is one.

If it's set to start_of_line, the end shall be aligned with the start of the line where the matching keyword appears.

This Layout/EndAlignment cop aligns with keywords (e.g. if, while, case) by default. On the other hand, Layout/BeginEndAlignment cop aligns with EnforcedStyleAlignWith: start_of_line by default due to ||= begin tends to align with the start of the line. These style can be configured by each cop.

Example: EnforcedStyleAlignWith: keyword (default)

# bad

variable = if true
    end

# good

variable = if true
           end

variable =
  if true
  end

Example: EnforcedStyleAlignWith: variable

# bad

variable = if true
    end

# good

variable = if true
end

variable =
  if true
  end

Example: EnforcedStyleAlignWith: startofline

# bad

variable = if true
    end

puts(if true
     end)

# good

variable = if true
end

puts(if true
end)

variable =
  if true
  end

Rename has_header? to header?.
Open

    def has_header?
Severity: Minor
Found in lib/workbook/template.rb by rubocop

This cop makes sure that predicates are named properly.

Example:

# bad
def is_even(value)
end

def is_even?(value)
end

# good
def even?(value)
end

# bad
def has_value
end

def has_value?
end

# good
def value?
end

Align else with if.
Open

        else
Severity: Minor
Found in lib/workbook/template.rb by rubocop

This cop checks the alignment of else keywords. Normally they should be aligned with an if/unless/while/until/begin/def/rescue keyword, but there are special cases when they should follow the same rules as the alignment of end.

Example:

# bad
if something
  code
 else
  code
end

# bad
if something
  code
 elsif something
  code
end

# good
if something
  code
else
  code
end

Prefer single-quoted strings when you don't need string interpolation or special symbols.
Open

      header_fmt[:font_weight] = "bold"
Severity: Minor
Found in lib/workbook/template.rb by rubocop

Checks if uses of quotes match the configured preference.

Example: EnforcedStyle: single_quotes (default)

# bad
"No special symbols"
"No string interpolation"
"Just text"

# good
'No special symbols'
'No string interpolation'
'Just text'
"Wait! What's #{this}!"

Example: EnforcedStyle: double_quotes

# bad
'Just some text'
'No special chars or interpolation'

# good
"Just some text"
"No special chars or interpolation"
"Every string in #{project} uses double_quotes"

Use 2 (not -24) spaces for indentation.
Open

          fs[:default].clone
        else
Severity: Minor
Found in lib/workbook/template.rb by rubocop

This cop checks for indentation that doesn't use the specified number of spaces.

See also the IndentationConsistency cop which is the companion to this one.

Example:

# bad
class A
 def test
  puts 'hello'
 end
end

# good
class A
  def test
    puts 'hello'
  end
end

Example: IgnoredPatterns: ['^\s*module']

# bad
module A
class B
  def test
  puts 'hello'
  end
end
end

# good
module A
class B
  def test
    puts 'hello'
  end
end
end

Prefer single-quoted strings when you don't need string interpolation or special symbols.
Open

        raise ArgumentError, "format should be a Workboot::Format"
Severity: Minor
Found in lib/workbook/template.rb by rubocop

Checks if uses of quotes match the configured preference.

Example: EnforcedStyle: single_quotes (default)

# bad
"No special symbols"
"No string interpolation"
"Just text"

# good
'No special symbols'
'No string interpolation'
'Just text'
"Wait! What's #{this}!"

Example: EnforcedStyle: double_quotes

# bad
'Just some text'
'No special chars or interpolation'

# good
"Just some text"
"No special chars or interpolation"
"Every string in #{project} uses double_quotes"

There are no issues that match your filters.

Category
Status