Dealing with durations

Whenever business logic is about times, it quite often is not only about simple time comparisons. Especially statistics and filters tend to need durations.
Durations consist of a beginning and an ending.
Ruby offers the Range class for dealing with durations.

A naive duration implementation

Creating a Task model:

rails g model Task name:string && rake db:migrate

By default the attribute created_at is migrated.. The timestamp is used for staving the objects creation time.
Besides there are two more methods implemented in the model:

The named scope finds all tasks, being created in certain period.
Additionally it should be possible the assert, if the task was created in a certain period.
Both cases demand to assign the timestamps for begin and end directly:

Though the implementation works, it is quite naive one.
A better solution is to use range objects whenever it is about durations. On the one hand it easier to assign just a single parameter instead and on the other hand The Range class provides some convenience methods:

Since the ActiveRecord uses Range objects for generating “BETWEEN” SQL, there is no need for writing plain SQL anymore. And by letting the Range object care about the decision, if a timestamp is within a duration, the complexity decreases.
Assigning a duration is easy: