Yes, you read it correctly, we are jumping from 5.7 to 8.0 (that sounds familiar, doesn't it?). The new version doesn't only change the number, but also change how you write your own SQL: Recursive queries will allow you to generate Series and work with Hierarchical data. New JSON functions and performance improvements were also added to 8.0 to help you work on non-relational data. Expect to see what are new and what is improved in this talk to power up your application even more.
In Ruby, you can work with modules and mixins, giving characteristics to classes and instances that otherwise could only happen through inheritance. One can argue that there are many similarities between Ruby's mixins and PHP's traits.
But first things first: what is a mixin? A mixin is a module that you include in a class, because you are "mixing in" the module methods with the instance methods of a class.
When learning about Ruby's modules behavior, specifically the include functionality, I couldn't help but think how similar it was to PHP traits. It reminded me how a class can override an inherited method from a trait, or just plain inheritance of new features that class didn't have before.
What I learned was the following (feel free to correct me):
| module Paintable | |
| def color | |
| puts "Blue" | |
| end | |
| end | |
| class Vehicle | |
| def color | |
| puts "Green" | |
| end |
| module Paintable | |
| def color | |
| puts "Blue" | |
| super | |
| end | |
| end | |
| class Vehicle | |
| def color | |
| puts "Green" |
| # without the prepend, extend or include | |
| Car.ancestors | |
| # => [Car, Vehicle, Object, PP::ObjectMixin, Kernel, BasicObject] | |
| # with the extend | |
| Car.ancestors | |
| # => [Car, Vehicle, Object, PP::ObjectMixin, Kernel, BasicObject] | |
| # with the include | |
| Car.ancestors |
| ### app/models/boat.rb ### | |
| class Boat < ActiveRecord::Base | |
| belongs_to :captain | |
| has_many :boat_classifications | |
| has_many :classifications, through: :boat_classifications | |
| end | |
| ### app/models/captain.rb ### | |
| class Captain < ActiveRecord::Base | |
| has_many :boats |
Developers in general love when stuff works. Having a solution that can solve about 80% of your problems can leave time for you to deal with the other 20%.
But this post is not about Active Record vs. Data Mapper or any thing like it. Each one has its use case where it's best applicable and it depends on you (or your team) to decide which to use. Keep in mind that with Active Record (AR), domain concerns and persistence concerns are mixed together and that with Data Mapper (DM), domain concerns and persistence concerns are kept separate.
Let's talk about magic. How magical AR can be and how it can make your life easier. The beauty of programming is that two different individuals can reach the same result using different routes even if using the same tools. The convention over configuration that some frameworks like Laravel and [Rails](http
We learn that classes should have Single Responsibility, and sometimes depending the way you code it, you won't have that. Sandi Metz says in her book that if when you describe your class and your description contains an and word, you are most likely to have a class with multiple responsibilities.
Lets take a look in the class Storage:
class Storage
attr_accessor :brand, :speed, :capacity, :files| class MyFile | |
| attr_accessor :name, :size, :content, :extension | |
| def initialize(args) | |
| @name = args[:name] | |
| @size = args[:size] | |
| @content = args[:content] | |
| @extension = args[:extension] | |
| end |
| SELECT `boats`.* | |
| FROM `boats` | |
| WHERE `boats`.`captain_id` IS NULL |