class ConceptQL::Operators::TemporalOperator

Base class for all temporal operators

Subclasses must implement the where_clause method which should probably return a Sequel expression to use for filtering.

Public Class Methods

within_skip(type) click to toggle source
# File lib/conceptql/operators/temporal_operator.rb, line 22
def self.within_skip(type)
  define_method(:"within_check_#{type}?"){false}
end

Public Instance Methods

add_occurrences_condition(ds, occurrences) click to toggle source
# File lib/conceptql/operators/temporal_operator.rb, line 62
def add_occurrences_condition(ds, occurrences)
  occurrences_col = occurrences_column
  ds.distinct.from_self
    .select_append{row_number{}.over(:partition => :person_id, :order => occurrences_col).as(:occurrence)}
    .from_self
    .select(*query_columns(ds))
    .where{occurrence > occurrences.to_i}
end
add_option_conditions(ds) click to toggle source
# File lib/conceptql/operators/temporal_operator.rb, line 36
def add_option_conditions(ds)
  if within = options[:within]
    ds = add_within_condition(ds, within)
  end

  if at_least = options[:at_least]
    ds = add_within_condition(ds, at_least, :exclude)
  end

  if occurrences = options[:occurrences]
    ds = add_occurrences_condition(ds, occurrences)
  end

  ds
end
add_within_condition(ds, within, meth=:where) click to toggle source
# File lib/conceptql/operators/temporal_operator.rb, line 52
def add_within_condition(ds, within, meth=:where)
  within = DateAdjuster.new(within)
  after = within.adjust(:r__start_date, true)
  before = within.adjust(:r__end_date)
  within_col = Sequel.expr(within_column)
  ds = ds.send(meth){within_col >= after} if within_check_after?
  ds = ds.send(meth){within_col <= before} if within_check_before?
  ds.distinct
end
inclusive?() click to toggle source
# File lib/conceptql/operators/temporal_operator.rb, line 87
def inclusive?
  options[:inclusive]
end
left_stream(db) click to toggle source
# File lib/conceptql/operators/temporal_operator.rb, line 91
def left_stream(db)
  Sequel.expr(left.evaluate(db).from_self).as(:l)
end
occurrences_column() click to toggle source
# File lib/conceptql/operators/temporal_operator.rb, line 75
def occurrences_column
  :start_date
end
query(db) click to toggle source
# File lib/conceptql/operators/temporal_operator.rb, line 26
def query(db)
  ds = db.from(left_stream(db))
         .join(right_stream(db), l__person_id: :r__person_id)
         .where(where_clause)
         .select_all(:l)

  ds = add_option_conditions(ds)
  ds.from_self
end
right_stream(db) click to toggle source
# File lib/conceptql/operators/temporal_operator.rb, line 95
def right_stream(db)
  Sequel.expr(right.evaluate(db).from_self).as(:r)
end
within_check_after?() click to toggle source
# File lib/conceptql/operators/temporal_operator.rb, line 79
def within_check_after?
  true
end
within_check_before?() click to toggle source
# File lib/conceptql/operators/temporal_operator.rb, line 83
def within_check_before?
  true
end
within_column() click to toggle source
# File lib/conceptql/operators/temporal_operator.rb, line 71
def within_column
  :l__start_date
end