Are there rules in Rubocop to enforce ordering of different types of elements in a class?
Given I have a class
class MyClass
include MyModule
MY_CONSTANT = "Constant value".freeze
class << self
def find_by_name(name)
end
end
def class.find_or_create_by(name)
end
attr_reader :name
def initialize(name)
end
def full_name
end
NotFoundError = Class.new(ArgumentError)
end
That is:
all class methods must be defined in a common place (customizable order)
There are two forms class methods can be included, and both must be positioned here
Of course, in the proper Rubocop-spirit, the ordering could probably be configurable.
Also, if in Rails, the ordering of elements could probably also include
Two questions:
Can Rubocop help me to enforce a proper ordering?
Would this idea help you to better organized code?
There isn't, but it is our oldest open issue, and I know it's also at the top of @bbatsov's list of cops he'd like to have himself. 馃槈 (#1575)
Closing this as a duplicate for now. If you want to try and take a stab at this, feel free to open a PR. I imagine an initial implementation could be as simple as just checking that include and extend are at the top of the class. Just to get something to build on.
Thanks @Drenmi for linking the issues. I had a feeling it was somewhere there... just couldnt find it.
Most helpful comment
There isn't, but it is our oldest open issue, and I know it's also at the top of @bbatsov's list of cops he'd like to have himself. 馃槈 (#1575)
Closing this as a duplicate for now. If you want to try and take a stab at this, feel free to open a PR. I imagine an initial implementation could be as simple as just checking that
includeandextendare at the top of the class. Just to get something to build on.