Abstract, modules and inheritance question

I have to import some csv files into the database. Each file had a
corresponding model and table in the system.
So I was thinking that there will be some common methods for all the
importers (log, spent_time_in_miliseconds, run, …) and some non common
methods (convert_line_to_parameters, …). Some common methods will end
up
calling particular methods (i.e. run will call
convert_line_to_parameters
for each line in the csv file).

I’m new to Ruby so the first idea that I got in mind was using one
abstract
class and the create one class for each model that has to be imported.
Then I realize that didn’t look like a Ruby way to solve this problem.
My
second thought was to create a module with all the common methods and
include that module in the different importers, but not sure its the
best
way either.

It will be great to get some ideas from people with more experience to
get
this done in the best way.

Thanks in advance.

Piter F. wrote in post #1014813:

I’m new to Ruby so the first idea that I got in mind was using one
abstract
class and the create one class for each model that has to be imported.
Then I realize that didn’t look like a Ruby way to solve this problem.
My
second thought was to create a module with all the common methods and
include that module in the different importers, but not sure its the
best
way either.

I get the impression that you’re coming from Java, or maybe some other
statically typed language with a more “closed” class architecture.

That being said, it sounds to me like a pattern based on modules (a.k.a
mix-ins) would be the way to go here. Keep in mind that Ruby has an
“open” class architecture. This mean that you can re-open and add new
stuff to existing classes, even the standard Ruby classes.

For example is String doesn’t have everything you would like it to have
then you can simply re-open the String class and add whatever you want
to it, and even override existing methods on String. This is sometimes
called “monkey-patching.”

Here’s a really simple example of Rails re-opening the String class and
adding stuff to it:

https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/string/starts_ends_with.rb

Modules are another way to extend existing classes. Rails uses modules
extensively. I would recommend cloning the Rails project from Github and
browsing the code.

Here’s another example from Rails showing the module used to implement
the callback methods of ActiveModel:

https://github.com/rails/rails/blob/3270c58ebb3143b3ab3b349fe339cdd4587468ee/activemodel/lib/active_model/callbacks.rb