Advanced Class Methods
Objectives
Build class finders
Build class constructors
Build class operators
Advanced Class Methods
Consider the method .all
on the Song
classββSong.all
. This method acts as a reader for the @@all
class variable. This method exposes this piece of data to the rest of our application. Class methods provide an interface for the data held within a class. This data, stored in a class variable, would otherwise be inaccessible outside of the class:
self.all
is a class method for reading the data stored in the class variable@@all
. This is a class reader, very similar to an instance reader method that reads an instance property:
What else can class methods help us with? What other common class-level functionality can be exposed through class methods?
Class Finders
Imagine a Person
class that provides access to all of its instances through Person.all
How might you find a specific person by name given this Person
model?
There's Gotta Be a Better Way!
Instead of writing #find
every time we want to search for an object, we can encapsulate this logic into a class method, like Person.find_by_name
Instead of writing
every single time we need to search, we can simply teach our Person
class how to search by defining a class method:
We call class methods like Person.find_by_name
'finders'. Finder class methods are responsible for finding instances based on some property or condition.
Slight Digression on Abstraction:
But we can improve the code above slightly. Code that relies on abstraction is more maintainable and extendable over time. In general, we advance as a species and a civilization when technology provides an abstraction for us to use instead of the literal implementation. When you want light, you don't need to start a fire, you can just flick a light switch. This is an abstraction. We promise. If creating and using abstractions have gotten people this far, we should probably continue embracing that design principle in our code.
Cool Tangent but What Can We Abstract Away Here?
Our current implementation of Person.find_by_name
reads the instance data for the class directly out of the class variable @@all
. Would this break if we need to rename the @@all
variable? What if it makes more sense to call it @@people
? Every method that relies on that literal variable nameββ Person.all
, Person.find_by_name
, etc.ββ would break, and we'd have to update all of our methods to read from the new variable:
Variable names are a very low-level abstraction. They are like making light by fire. Methods that read out of a variable provide an abstraction for the literal variable name. Using a reader method is almost always better and more reliable than using the variable.
We already have a method to read @@people
, Person.all
, so why not use that method in Person.find_by_name
? Within a class method, how do we call another class method? What is the scope of the class method? What is self? The class itself. Consider:
(Within #initialize
, an instance method, self
will refer to an instance, not the entire class. In order to access Person.all
, we need to go from the instance, self
, to the classββself.class
ββreturning Person
, and then invoke the Person.all
method).
If the variable @@people
changes names, we only have to update it in one place, the Person.all
reader. All code that relies on that method still works. 1 conceptual change -> 1 line-of-code (LOC) change. Nice.
In addition to improving the maintainability of our code, class methods also provide a more readable API for the rest of our application. Consider just one more time the difference in seeing the following two lines littered throughout your code:
Whenever we use Person.find_by_name
the intention of our code is clear. Instead of iterating over an array, our code reads clearly. Instead of describing the implementation of finding a person by name, our code simply says what it is doing, not how. You want to build objects that provide a semantic and obvious API, or interface. Methods that reveal what the object will do, not how it does that. Always hide the how and show the what.
Finders are just one example of a more semantic API for our classes. Let's look at another way class methods can improve the readability of our code.
Custom Class Constructors
Our marketing team has provided us with a list of people in comma-separated values (CSV), a common formatting convention when exporting from spreadsheets. The raw data looks like:
They tell us that they will often need to upload CSVs of people data. Let's look at how we'd create a person instance from a CSV:
Pretty complex. We don't want to do that throughout our application. In an ideal world, every time we got CSV data we'd just want the Person
class to be responsible for parsing it. Could we build something like Person.new_from_csv
? Of course! Let's look at how we might implement a custom constructor.
We can see that, when needing to parse multiple sets of CSV data, having a Person.new_from_csv
class method greatly simplifies our code. Let's take a closer look at how that class method works:
Like in any class method, self
refers to the class itself so we can call self.new
to piggyback, wrap, or extend the functionality of Person.new
ββwhen I call Person.new_from_csv
, who is receiving the method call? It's the Person
class itself. Therefore, self
in this context is Person
. We parse the raw data, create an instance, and assign the data to the corresponding instance properties.
Why do this? If we need to be able to create people from CSVs, why not just build that directly into #initialize
? Well, the honest answer is because we don't always want to create people from CSV data. Anything we build into initialize will happen always. Another key to writing maintainable code is designing functionality that is closed to modification but open to extension.
Initialize should be closed to modification. It should only handle the most often required and common cases of initializing an object. Anything we add to initialize should be permanent and never modified. If we need more functionality when making an instance, instead of modifying initialize, we can extend it by wrapping it within a custom constructor.
If we ever need to make people from xml or json we can continue to extend the object with custom constructors instead of constantly modifying initialize with complex logic.
Let's look at a somewhat simpler example of a custom constructor that wraps .new
. When building objects that can be saved into a class variable @@all
, we might not always want to save the newly instantiated instance.
With that code, no matter what, person instances will always be saved. We could instead implement a simple .create
class method to provide the functionality of instantiating and creating the instance, leaving .new
to function as normal.
Class Operators
Beyond finders and custom constructors that return existing instances or create new instances, class methods can also manipulate class-level data.
A basic case of this might be printing all the people in our application.
Even that logic is worth encapsulating within a class method .print_all
.
Way nicer.
Class Operators
Additionally, class methods might provide a global operation on data. Imagine that one of the csvs we were provided with has people's names in lowercase letters. We want proper capitalization. We can build a class method Person.normalize_names
The logic for actually normalizing a person's name is pretty complex. person.name.split(" ").collect{|w| w.capitalize}.join(" ")
What we're doing is splitting a name, like "ada lovelace"
, into an array at the space, " "
, returning ["ada", "lovelace"]
. With that array we collect each word into a new array after it has been capitalized, returning ["Ada", "Lovelace"]
. We then join the elements in that array with a " "
returning the final capitalized name, "Ada Lovelace"
.
Given how complex normalizing a person's name is, we should actually encapsulate that into the Person
instance.
With #normalize_name
, we've taught a Person
instance how to properly convert its name into a capitalized version. The class method that acts on the global data of all people is simplified and delegates the actual normalization to the original instances. This is a common pattern for global class operators.
Another example of this type of global data manipulation might be deleting all the people. We would build a Person.destroy_all
class method that will clear out the @@all
array.
Resources
Last updated