How to split a module over multiple files in Rails?

Discussions about RaZberry - Z-Wave board for Raspberry computer
Post Reply
Bennie84
Posts: 1
Joined: 13 Jun 2019 13:23

How to split a module over multiple files in Rails?

Post by Bennie84 » 15 Jun 2019 12:15

In my Rails app I have a module, which holds all the classes that make up an API wrapper. Here's the module's structure:

Code: Select all

module Zway
  class API
  # instantiates API object
  end

  class Main
  # holds top level functions
  end

  class Controller
  # holds controller functions
  end

  class Error < StandardError
  end
  class BadRequestError < Error
  end
end
Now that module is getting too big to keep it in one file so I want to split it up. As there are several classes in the module, I figured that every big class should be one file. So I didn't think a lot but tried to take out one class to see if it would work. Like so:

Code: Select all

module Zway
  class API
  # instantiates API object
  end

  class Controller
  # holds controller functions
  end

  class Error < StandardError
  end
  class BadRequestError < Error
  end
end

Code: Select all

module Zway
  class Main
  end
end
But now I am getting this error which doesn't sound right to me as I do exactly what the error complains about: defining class Main in main.rb :

Code: Select all

Unable to autoload constant Main, expected /bla/homer/app/models/main.rb to define it
I've searched on the net how to use modules but mostly what I found was about multiple inheritance and namespacing. But not about the practics of using modules to organize code.

Post Reply