Breaking Ruby Module Across Several Files

How to split a module over multiple files in Rails?

If you put your classes in modules, as per convention you should define them within folders named with the module_name. That's the whole point of organising. You can do something like this and give it a go. First organize your code and file like this ->

models/
zway/
main.rb
zway.rb

And inside your main.rb you namespace it like this ->

class Zway::Main
...
end

and inside your zway.rb you define the module

module Zway
...
end

In this way, when you're trying to access the main class it, since its namespaced, it will look it up inside a folder zway by convention. And all your models are organized neatly within their respective folders.

Splitting a ruby class into multiple files

I often do this in my gem files to manage documentation and avoid long files.

The only issues I found (which are solvable) are:

  1. Initialization of class or module data should be thoughtfully managed. As each 'compartment' (file) is updated, the initialization data might need updating, but that data is often in a different file and we are (after all) fallible.

    In my GReactor project (edit: deprecated), I wrote a different initialization method for each section and called all of them in the main initialization method.

  2. Since each 'compartment' of the class or module is in a different file, it is easy to forget that they all share the same namespace, so more care should be taken when naming variables and methods.

  3. The code in each file is executed in the order of the files being loaded (much like it would be if you were writing one long file)... but since you 'close' the class/module between each file, than your method declaration order might be important. Care should be taken when requiring the files, so that the desired order of the code execution is preserved.

The GReactor is a good example for managing a Mega-Module with a large API by compartmentalizing the different aspects of the module in different files.

There are no other pitfalls or issues that I have experienced.

Splitting a class into multiple files in Ruby on Rails

I know I'm answering this a little late, but I've just done this in one of my apps so thought I'd post the solution I used.

Let's this was my model:

class Model1 < ActiveRecord::Base

# Stuff you'd like to keep in here
before_destroy :destroying
has_many :things, :dependent => :destroy

def method1
end
def method2
end

# Stuff you'd like to extract
before_create :to_creation_stuff
scope :really_great_ones, #...

def method3
end
def method4
end
end

You can refactor it to:

# app/models/model1.rb
require 'app/models/model1_mixins/extra_stuff'
class Model1 < ActiveRecord::Base

include Model1Mixins::ExtraStuff

# Stuff you'd like to keep in here
before_destroy :destroying
has_many :things, :dependent => :destroy

def method1
end
def method2
end
end

and:

# app/models/model1_mixins/extra_stuff.rb
module Model1Mixins::ExtraStuff

extend ActiveSupport::Concern

included do
before_create :to_creation_stuff
scope :really_great_ones, #...
end

def method3
end
def method4
end
end

It works perfectly thanks to the extra cleanliness that ActiveSupport::Concern gives you. Hope this solves this old question.

To break up Ruby class into separate files by mixins or plain definitions?

Are your support methods general enough that they might be useful for other totally unrelated classes? If so, a mixin is the proper way to do this, since it lets you easily reuse the support code.

However if your support methods are very specific to ReallyBigClass and are unlikely to work if included somewhere else, reopening the class is the way to go. Using a mixin there could give the appearance that the methods are more general than they really are, when really they should only be used with instances of a specific class.

That being said, I think your question indicates a larger design problem. If you are in the former case (general methods), you should be designing more general modules in the first place to avoid tight coupling. A module called ReallyBigClassFileB gives off some strong code smell. In the latter case (very specific methods) if your class is so big that its file is unmanageably large you probably need to refactor something. Maybe your class is responsible for too much? Maybe it could use some subclasses (which make sense in separate files)?

rails 4: split routes.rb into multiple smaller files

This was removed from Rails 4 in June of 2012. 5e7d6bba reverts an earlier commit, removing support for loading multiple external route files as part of config.rb.

For further read, check out the comments on this commit.

Is there is way to autoload multiple file for one module in ruby?

The closer approach is to reference using autoload a file that represents the main definition for the module and add a require statement in that file to each additional file where the module is further redefined.

You should also ask yourself why you are splitting a definition into multiple files and if that's the best approach, if you are assuming that each file is equivalent relevant for the definition.



Related Topics



Leave a reply



Submit