Skip to content

Fix silenced Style/OpenStructUse offenses

Goal

Fix all silenced offenses of the Style/OpenStructUse cop added in !73373 (merged)

Supersedes #325744 (closed).

Recommendations

  • Replace OpenStruct in tests with doubles, or test-specific Structs or Hashes
    • OpenStruct and hash differ slightly in API, ensure good integration coverage and be mindful that keys should not have the name of any hash method or built-in method
  • Replace OpenStruct outside of tests with custom Structs or Hashes
  • Delegate method calls via method_missing when keys are unknown or configurable by users
  • Use ActiveSupport::InheritableOptions in cases where both method call (object.foo) and Hash syntax (object['foo']) is used deliberately - for example, !75993 (merged)
    • Please beware when using it with keys like default, keys, and all predefined Hash and Object methods.
    • See !75993 (comment 895124425)

Gotchas

  • RSpec would reject using double in let_it_be (e.g !74192 (comment 732802890)). Consider if let_it_be can be changed to a plain let without slowing down the test, for example if the let_it_be block is not hitting ActiveRecord.

Offenses

After fixing the offense please don't forget to remove this file from the Style/OpenStructUse configuration in .rubocop_todo/style/open_struct_use.yml in this same MR.

Edited by Peter Leitzen