git-read

Extract Commit model

Throughout the application, there are several occurrences of logic that extract’s a commit message’s subject:

commit.message.lines.first.strip

In reaction to this repeated logic, we have several solutions at our disposal.

Helper method

One potential solution is to extract subject method in the helpers block in our config.rb:

helpers do
  def subject(commit)
    commit.message.lines.first
  end
end

Methods declared in the helpers block are made available within our application templates (namely, source/layouts/layout.erb.

This would resolve the instances of duplication within that template, but doesn’t resolve instances within the config.rb itself.

Model Instance method

As an alternative to a helper method, this commit extracts the Commit class, and declares it in the new lib/models directory.

Similar to a helper method, an instance method centralizes the logic to a single place. In the current implementation, the Commit class inherits from SimpleDelegator, which makes the methods declared in it an expansion on the existing Git::Object interface.

An additional benefit of an instance method on a model is that contexts that have access to the commit template-local variable can invoke subject, without requiring knowledge of the internals of the commit reference. This style of message sending adheres to the “Tell, Don’t Ask” principle of software design.

Centralizing the logic of transforming the Git::Object instances into git-read-specific models will simplify this project’s future improvements.