I’ve been struggling with a design problem, and I admit that I am new to both OOP and RoR so I’m sure this is going to be very basic. I have an application where I am reading from text files in various formats in order to parse information related to hands of poker. So what I have are three entities:
-
A file object. This stores the name and path of the file and some other attributes, and has functions related to reading from the files. This is MVC because I can add a file and have it be auto-updated, or I can just parse the info from a file on the fly.
-
The poker hand object. This essentially just stores information about who played the hand of poker and what the actions and results were.
-
A parser. This reads external JSON files with different regex patterns depending on the type of file that is being read. It also has some basic state machine info in the JSON file so that alot of the logic is removed from the parser.
So my initial feeling about the parser was that it should be its own object. But then I realized that it didn’t have a V or a C and so possibly didn’t fit with the Rails way of doing things. And it also doesn’t have any functionality that is needed by any object other than the file object, and so seemed to fit within the file. But at the same time it’s so distinctly different than a file object, that it didn’t seem to fit. I thought of a module, but the point of modules seems to be if multiple objects share the need for some functions, and in this case only the file does.
So should it be its own object, be within the file object, or is there some other alternative I’m not seeing?
The decision about whether something should be an “M” in MVC should be based on whether it has any persistent (database-driven) data.
Models don’t need a controller or views, and controllers don’t have to map one-to-one with models. However, the common “RESTful API” approach does result in a strong model <=> controller correspondence.
In your case it sounds like it’s just a chunk of code which takes input and returns some other already-defined model, so it probably sits best as a module in your
lib/folder which you can call from some of your other models or controllers