Unspace’s own Mike Ferrier has a gem announcement to make. Scoped Struct is a delicious morsel of meta-programming magic designed to help you organize the functionality of Ruby classes. You can now provide access to a group of related methods using association-style syntax.
Mike uses the example of a class representing an NFL player to illustrate the concept. In a vanilla class, you might see:
class Player
def fumbles_dropped; end
def fumbles_lost; end
def fumbles_recovered; end
def passes_attempted; end
def passes_completed; end
def passes_incomplete; end
def pass_completion_percentage; end
end
However, with scoped_struct, you could re-organize like this:
class Player
scope :fumbles do
def dropped; end
def lost; end
def recovered; end
end
scope :passes do
def attempted; end
def completed; end
def incomplete; end
def completion_percentage; end
end
end
You can then access your methods like this:
my_player.passes.attempted
my_player.passes.completed
my_player.fumbles.recovered
Pretty neat, huh? It’s just a gem install scoped_struct away.