Class: DateFilteredDataset

Inherits:
RolePlaying::Role
  • Object
show all
Defined in:
app/roles/date_filtered_dataset.rb

Overview

Filters dataset by starting date and end date.

Instance Method Summary collapse

Instance Method Details

#filter_by_date(from: nil, to: nil, with_original_date: false) ⇒ Sequel::Dataset

Returns a filtered dataset.

Parameters:

  • from (Date, DateTime, String) (defaults to: nil)
  • to (Date, DateTime, String) (defaults to: nil)
  • with_original_date (Boolean) (defaults to: false)

    whether to include original +starts_at- and +ends_at+ into the range.

Returns:

  • (Sequel::Dataset)

    a filtered dataset.


13
14
15
16
17
18
19
20
21
22
23
# File 'app/roles/date_filtered_dataset.rb', line 13

def filter_by_date(from: nil, to: nil, with_original_date: false)

  condition = ->(col, op, date) do
    sql = "#{col} #{op} :date"
    sql += " OR original_#{col} #{op} :date" if with_original_date
    Sequel.lit(sql, date: date)
  end

  self.then_if(from) { |d| d.where(condition['starts_at', '>=', from]) }
      .then_if(to) { |d| d.where(condition['ends_at', '<=', to]) }
end