From OOP Ruby to ORM & Active Record (Part 1)
This small project began as an attempt to solidify my understanding of object oriented programming in Ruby. As I continued learning about object relational mapping (ORM), and then, Active Record (AR), I realized that I needed some way to take what I understood at a basic level (OOP) and relate it to the more complex topics (ORM & AR).
My OOP project is a 5 point model containing the relations between Person, Company, Blog, Github_Project & Language. A person has one blog, belongs to a company, has many Github projects & has many programming languages.
After working out the relationships, I drew a schematic diagram so as to have a more easy grasp of these relationships.
Notice the person has many Github projects & languages through classes PG & PL. PG is a join class denoting the relational data between each person and the projects he is working on. PL denotes each person and the languages they know.
The Company class initializes with a name, address & revenue. It can return the company data & employee count for an instance of that company.
The GitProject class initializes with a project name & can return all persons working on a specific project.
The Blog class initializes with a series of string data regarding the blog (website, bio, blog title, etc.) & a person instance associated with a particular blog. This class has some query methods including finding a post by its title or author, finding a blog by the author & listing all titles of all blog posts.
The Language class is fairly simply. It initializes with a programming language name & can return all available languages.
The Person class initializes with a name & company instance that is associated with that person instance. The person is capable of listing all languages that it knows & learn a new language. Learning a new language is achieved by first accepting an argument of language name, validating that it is a valid language, then checking whether the person already knows that language, before associating the person and language via the PL join class.
The PG class initializes with a person & Github project instances. This is how each person is associated with each project. In this way, a person can have many associations with different projects. A person can have many projects.
The PL class initializes with a person & language instances. Similarly to the PG class, this is how a person can have many languages.
It is important to note that, thus far, all data has been seeded through a console. For all that data to exist, for all persons to belong to a company, have a blog, have many languages & projects, the instances & their relationships need to be repeatedly generated. This brings us to ORM, a solution to the problem of data persistence.